From 5aca8a1a9bcc539957e3eee339ea5b72e27e5c7f Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 16 Sep 2015 14:03:35 +0200 Subject: Added problems Fast Fingers and Fast Fingers 2. --- .../problems/introduction/fast_fingers/common.py | 61 ++++++++++++ python/problems/introduction/fast_fingers/en.py | 16 ++++ python/problems/introduction/fast_fingers/sl.py | 91 ++++++++++++++++++ .../problems/introduction/fast_fingers_2/common.py | 67 +++++++++++++ python/problems/introduction/fast_fingers_2/en.py | 16 ++++ python/problems/introduction/fast_fingers_2/sl.py | 104 +++++++++++++++++++++ 6 files changed, 355 insertions(+) create mode 100644 python/problems/introduction/fast_fingers/common.py create mode 100644 python/problems/introduction/fast_fingers/en.py create mode 100644 python/problems/introduction/fast_fingers/sl.py create mode 100644 python/problems/introduction/fast_fingers_2/common.py create mode 100644 python/problems/introduction/fast_fingers_2/en.py create mode 100644 python/problems/introduction/fast_fingers_2/sl.py diff --git a/python/problems/introduction/fast_fingers/common.py b/python/problems/introduction/fast_fingers/common.py new file mode 100644 index 0000000..3c1b138 --- /dev/null +++ b/python/problems/introduction/fast_fingers/common.py @@ -0,0 +1,61 @@ +# coding=utf-8 + +from python.util import has_token_sequence +from server.hints import Hint, HintSequence + +id = 190 +group = 'introduction' +number = 2 +visible = True + +solution = '''\ +from time import time +zacetek = time() +rezultat = float(input("Koliko je 7*6? ")) +konec = time() +cas = konec­zacetek +print ("Porabil si ", cas, "s.") +''' + +hint_type = { + 'plan': Hint('plan'), + 'name_error': HintSequence('name_error', 4), + 'type_error': HintSequence('name_error', 4), + 'error': HintSequence('error', 2), + 'time': Hint('time'), + 'time_diff': Hint('time_diff'), +} + +def test(python, code): + return 0, 0 + +def hint(python, code): + # run one test first to see if there are any exceptions + test_in = [(None, '5\n')] + answer = python(code=code, inputs=test_in, timeout=2.0) + exc = answer[0][3] + # if have an exception! + if exc: + if 'NameError' in exc: + return [{'id':'name_error', 'args': {'message': exc}}] + elif 'TypeError' in exc: + return [{'id':'type_error', 'args': {'message': exc}}] + else: + return [{'id':'error', 'args': {'message': exc}}] + + # First: if student does not import time, tell him about that module + if not has_token_sequence(code, ['time']): + return [{'id': 'time'}] + + # show plan if student is lost + # a) empty progam or + # b) there is not input (we can do it here, since we have no input hint) + if len(code.strip()) < 5 or (not has_token_sequence(code, ['input'])): + return [{'id': 'plan'}] + + # Student will have to compute time difference and they will need + # the substraction (-) operator to do this. Check for the minus operator. + if not has_token_sequence(code, ['-']): + return [{'id' : 'time_diff'}] + + return None diff --git a/python/problems/introduction/fast_fingers/en.py b/python/problems/introduction/fast_fingers/en.py new file mode 100644 index 0000000..72e83aa --- /dev/null +++ b/python/problems/introduction/fast_fingers/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 1000 +name = 'Pythagorean theorem' +slug = 'Pythagorean theorem' + +description = '''\ +

(translation missing)''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/introduction/fast_fingers/sl.py b/python/problems/introduction/fast_fingers/sl.py new file mode 100644 index 0000000..f24718b --- /dev/null +++ b/python/problems/introduction/fast_fingers/sl.py @@ -0,0 +1,91 @@ +# coding=utf-8 + +id = 190 +name = 'Hitri prsti' +slug = 'Hitri prsti' + +description = '''\ +

Napiši program, ki uporabnika vpraša, koliko je 6 krat 7. Uporabnik bo premislil in vpisal odgovor. +Program naj se ne ukvarja z odgovorom ter tem, ali je pravilen ali ne, temveč naj izpiše, koliko +sekund je človek potreboval za razmišljanje.

+

+Koliko je 6 krat 7? UPORABNIK VTIPKA 42
+Za razmišljanje ste porabili 2.503019332885742 s.
+
+

Pri tej nalogi ni testnih primerov.

+''' + +general_exception = { + 'error_head' : '''\ +

Napaka:

+
+[%=message%]
+
+''', + + 'general': ''' +

Pri razumevanju napake sta pomembni dve vrstici. V predzadnji vrstici je napisana lokacija +napake (line ...), v zadnji vrstici pa izvemo za kakšno napako gre.

+ ''', + + 'name_error' : ''' +

Napaka NameError pomeni, da uporabljate nedefinirano vrednost: +ali vrednost spremenljivke ni določena ali uporabljate funkcijo, ki ni uvožena.

+ ''', + + 'type_error': ''' +

TypeError napaka pomeni, da želite izvesti operacijo na nedovoljenih tipih. +Npr., če želite sešteti niz in število ali klicati funkcijo, čeprav tisto ni funkcija, itd. .

+ ''', +} + +hint = { + 'time': '''

V modulu time imate funkcijo time, +ki vrača čas v sekundah od 1.januarja 1970 (ta datum označujemo tudi kot epoch oz. začete časa) +do trenutka, ko smo to funkcijo klicali. Poskusite:

+
+from time import *
+trenutno = time()
+print("Od začetka časa je minilo že", trenutno, "sekund.")
+
+ ''', + + 'plan': '''\ +

Če hočemo izračunati, koliko časa smo razmišljali, moramo poznati uro pred klicem funkcije input in +po klicu funkcije input:

+
    +
  1. Izmeri trenutni čas
  2. +
  3. Vprašaj za rezultat
  4. +
  5. Izmeri trenutni čas
  6. +
  7. Izračunaj porabljen čas
  8. +
  9. Izpiši
  10. +
+''', + + 'time_diff': ''' +

Porabljen čas lahko izračunamo tako, da od izmerjenega časa po vprašanju (konec) odštejemo +izmerjen čas pred vprašanjem (zacetek):

+
+zacetek = time()
+...
+konec = time()
+cas = konec – zacetek
+
+''', + 'name_error' : [general_exception['error_head'], general_exception['general'], + general_exception['name_error'], ''' +

Verjetno uporabljate spremenljivko, ki nima vrednosti. Ali v izrazu za izračun +uporabljate napačno spremenljivko? Ali pri izpisu morda poskušate +izpisati napačno spremenljivko?

'''], + + 'type_error' : [general_exception['error_head'], general_exception['general'], + general_exception['type_error'], ''' +

Verjetni razlog: funkcija input vrača vrednost tipa niz, +ki jo moramo najprej pretvoriti v tip float, če želimo z njo računati:

+
+v = float(input(" ...
+
+'''], + + 'error' : [general_exception['error_head'], general_exception['general'],] +} diff --git a/python/problems/introduction/fast_fingers_2/common.py b/python/problems/introduction/fast_fingers_2/common.py new file mode 100644 index 0000000..9c8772f --- /dev/null +++ b/python/problems/introduction/fast_fingers_2/common.py @@ -0,0 +1,67 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal +from server.hints import Hint, HintSequence + +id = 191 +group = 'introduction' +number = 2 +visible = True + +solution = '''\ +import random +import time +x = random.randint(1, 10) +y = random.randint(1, 10) +t = time.time() +print('Koliko je ', x, ' krat ', y, '? ') +z = float(input()) +if x * y == z: + print('Odgovor je pravilen.') +else: + print('Odgovor ni pravilen.') +print('Za razmišljanje ste porabili', time.time() ­ t, 's.') +''' + +hint_type = { + 'plan': HintSequence('plan', 2), + 'random': Hint('random'), + 'name_error': HintSequence('name_error', 4), + 'type_error': HintSequence('type_error', 4), + 'error': HintSequence('error', 2), + 'if_clause': HintSequence('if_clause', 2), +} + +def test(python, code): + return 0,0 + +def hint(python, code): + # run one test first to see if there are any exceptions + test_in = [(None, '5\n')] + answer = python(code=code, inputs=test_in, timeout=1.0) + exc = answer[0][3] + # if have an exception! + if exc: + if 'NameError' in exc: + return [{'id':'name_error', 'args': {'message': exc}}] + elif 'TypeError' in exc: + return [{'id':'type_error', 'args': {'message': exc}}] + else: + return [{'id':'error', 'args': {'message': exc}}] + + # First: if student does not import random, tell him about that module + if not has_token_sequence(code, ['random']) or \ + not has_token_sequence(code, ['randint']): + return [{'id': 'random'}] + + # show plan if student is lost + # a) empty progam or + # b) there is not input (we can do it here, since we have no input hint) + if len(code.strip()) < 5 or (not has_token_sequence(code, ['input'])): + return [{'id': 'plan'}] + + # Student will have to test whether result is correct or not + if not has_token_sequence(code, ['if']): + return [{'id' : 'if_clause'}] + + return None diff --git a/python/problems/introduction/fast_fingers_2/en.py b/python/problems/introduction/fast_fingers_2/en.py new file mode 100644 index 0000000..72e83aa --- /dev/null +++ b/python/problems/introduction/fast_fingers_2/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 1000 +name = 'Pythagorean theorem' +slug = 'Pythagorean theorem' + +description = '''\ +

(translation missing)''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/introduction/fast_fingers_2/sl.py b/python/problems/introduction/fast_fingers_2/sl.py new file mode 100644 index 0000000..da2751c --- /dev/null +++ b/python/problems/introduction/fast_fingers_2/sl.py @@ -0,0 +1,104 @@ +# coding=utf-8 + +id = 191 +name = 'Hitri prsti 2' +slug = 'Hitri prsti 2' + +description = '''\ +

Napiši program, podoben prejšnjemu, vendar naj ne vpraša vedno, koliko je 6 krat 7, temveč naj si +izmišlja naključna vprašanja iz poštevanke. Program bo torej izžrebal dve števili med 1 in 10 in izpisal +račun s tema dvema številoma namesto s 6 in 7. Tokrat naj program tudi preveri, ali je uporabnik +pravilno izračunal produkt.

+
+Koliko je 6 krat 3? UPORABNIK VTIPKA 18
+Odgovor je pravilen.
+Za razmišljanje ste porabili 2.1922357082366943 s.
+
+

Pri tej nalogi prav tako ni testnih primerov.

+''' + +general_exception = { + 'error_head' : '''\ +

Napaka:

+
+[%=message%]
+
+''', + + 'general': ''' +

Pri razumevanju napake sta pomembni dve vrstici. V predzadnji vrstici je napisana lokacija +napake (line ...), v zadnji vrstici pa izvemo za kakšno napako gre.

+''', + + 'name_error' : ''' +

Napaka NameError pomeni, da uporabljate nedefinirano vrednost: +ali vrednost spremenljivke ni določena ali uporabljate funkcijo, ki ni uvožena.

+''', + + 'type_error': ''' +

TypeError napaka pomeni, da želite izvesti operacijo na nedovoljenih tipih. +Npr., če želite sešteti niz in število ali klicati funkcijo, čeprav tisto ni funkcija, itd. .

+''', + +} + +hint = { + 'random': '''\ +

Če napišemo na začetek programa

+
+from random import *
+
+

dobimo (med drugim) tudi funkcijo randint(x,y), ki vrne naključno +celo število med x in y:

+
+st = randint(1, 10)
+
''', + + 'plan': ['''\ +

Razširimo plan iz prejšnje naloge:

+
    +
  1. Izmisli si dve naključni števili
  2. +
  3. Izmeri trenutni čas
  4. +
  5. Vprašaj za rezultat produkta
  6. +
  7. Izmeri trenutni čas
  8. +
  9. Ali je rezultat množenja pravilen?
  10. +
  11. Izračunaj porabljen čas
  12. +
  13. Izpiši
  14. +
+''', +'''

Pri vprašanju za rezultat produkta moramo navesti tudi vrednosti +dveh spremenljivk. Najlažje bo, če uporabite dve vrstici:

+

+print("Koliko je", x, " * ", y, "?")
+rezultat = float(input())
+
'''], + + 'if_clause': [''' +

Program se mora obnašati ustrezno rezultatu. Če izračunamo pravilno, bo napisal "Odgovor je pravilen", drugače +"Odgovor ni pravilen". To omogoča pogojni stavek.

''', +'''

Primer pogojnega stavka if: +

+if a == b: # dvopičje na koncu pogoja!
+    print("a je enak b")
+else:
+    print("a ni enak b")
+
''' + ], + + 'name_error' : [general_exception['error_head'], general_exception['general'], + general_exception['name_error'], ''' +

Verjetno uporabljate spremenljivko, ki nima vrednosti. Ali v izrazu za izračun + uporabljate napačno spremenljivko? Ali pri izpisu morda poskušate + izpisati napačno spremenljivko?

'''], + + 'type_error' : [general_exception['error_head'], general_exception['general'], + general_exception['type_error'], ''' +

Verjetni razlog: funkcija input vrača vrednost tipa niz, +ki jo moramo najprej pretvoriti v tip float, če želimo z njo računati:

+
+v = float(input(" ...
+
+'''], + + 'error' : [general_exception['error_head'], general_exception['general'],] +} -- cgit v1.2.1