From 0d45ae47238808ac4659a81c7dc94aba88c810f5 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 22 Sep 2015 10:37:24 +0200 Subject: Added first two problems of Lists and For section. --- .../problems/lists_and_for/contains_42/common.py | 97 ++++++++++++++++++++++ python/problems/lists_and_for/contains_42/en.py | 16 ++++ python/problems/lists_and_for/contains_42/sl.py | 81 ++++++++++++++++++ .../lists_and_for/contains_string/common.py | 96 +++++++++++++++++++++ .../problems/lists_and_for/contains_string/en.py | 16 ++++ .../problems/lists_and_for/contains_string/sl.py | 68 +++++++++++++++ 6 files changed, 374 insertions(+) create mode 100644 python/problems/lists_and_for/contains_42/common.py create mode 100644 python/problems/lists_and_for/contains_42/en.py create mode 100644 python/problems/lists_and_for/contains_42/sl.py create mode 100644 python/problems/lists_and_for/contains_string/common.py create mode 100644 python/problems/lists_and_for/contains_string/en.py create mode 100644 python/problems/lists_and_for/contains_string/sl.py (limited to 'python') diff --git a/python/problems/lists_and_for/contains_42/common.py b/python/problems/lists_and_for/contains_42/common.py new file mode 100644 index 0000000..3a5a905 --- /dev/null +++ b/python/problems/lists_and_for/contains_42/common.py @@ -0,0 +1,97 @@ +# coding=utf-8 + +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 193 +group = 'lists_and_for' +number = 1 +visible = True + +solution = '''\ +xs = [42, 5, 4, -7, 2, 12, -3, -4, 11, 42, 2] + +vsebuje = False +for x in xs: + if x == 42: + vsebuje = True +print(vsebuje) +''' + +hint_type = { + 'plan': HintSequence('plan', 1), + 'no_xs': Hint('no_xs'), + 'for_loop': Hint('for_loop'), + 'if_clause': Hint('if_clause'), + 'printing': Hint('printing'), + 'print_out_for': Hint('print_out_for') +} + +def test(python, code): + test_xs = [[42, 5, 4, -7, 2, 12, -3, -4, 11, 42, 2], + [42, 5, 4, -7, 2, 12, -3, -4, 11, 2], + [5, 4, -7, 2, 12, -3, -4, 11, 2], + [], + [42], + [1, 2, 3, -42], + [1, 2, 3, 42]] + test_out = [ + True, + True, + False, + False, + True, + False, + True + ] + + n_correct = 0 + for xs_i, xs in enumerate(test_xs): + # change code to contain new xs instead of the one + # given by user + tcode = re.sub(r'^xs\s*=\s*\[.*?\]', + 'xs = ' + str(xs), + code, + flags = re.DOTALL | re.MULTILINE) + + # use python session to call tcode + answers = python(code=tcode, inputs=[(None, None)], timeout=1.0) + output = answers[0][1] + + if str(test_out[xs_i]) in output and \ + str(not test_out[xs_i]) not in output: + n_correct += 1 + return n_correct, len(test_out) + +def hint(python, code): + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer) + if exc: return exc + + tokens = get_tokens(code) + + # if has no xs, tell him to ask for values + if not has_token_sequence(tokens, ['xs', '=', '[']): + return [{'id' : 'no_xs'}] + + # student does not have while or for: instruct him on loops + if not has_token_sequence(tokens, ['while']) and \ + not has_token_sequence(tokens, ['for']): + return [{'id' : 'for_loop'}] + + if not has_token_sequence(tokens, ['if']): + return [{'id' : 'if_clause'}] + + # student is not using print function + if not has_token_sequence(tokens, ['print']): + return [{'id' : 'printing'}] + + # student is not using print at the beginning of line + if not has_token_sequence(tokens, ['\n', 'print']): + return [{'id' : 'print_out_for'}] + + + return None diff --git a/python/problems/lists_and_for/contains_42/en.py b/python/problems/lists_and_for/contains_42/en.py new file mode 100644 index 0000000..878e4ca --- /dev/null +++ b/python/problems/lists_and_for/contains_42/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 193 +name = '(translation missing)' +slug = '(translation missing)' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

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

(translation missing)

''', +} diff --git a/python/problems/lists_and_for/contains_42/sl.py b/python/problems/lists_and_for/contains_42/sl.py new file mode 100644 index 0000000..eaf9609 --- /dev/null +++ b/python/problems/lists_and_for/contains_42/sl.py @@ -0,0 +1,81 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 193 +name = 'Vsebuje' +slug = 'Vsebuje' + + +description = '''\ +

Napiši program, ki ugotovi ali seznam števil vsebuje število 42. +Seznam z imenom xs definiraj na vrhu programa. Primer:

+
+xs = [42, 5, 4, -7, 2, 12, -3, -4, 11, 42, 2]
+
+

Program naj izpiše le True ali False. +Seveda mora program delati za poljubne sezname in ne samo za seznam iz primera.

+''' + +for_loop = ['''\ +

Čez elemente v seznamu se najlažje sprehodimo s for zanko. +''', + '''\ +

Poskusi, kaj naredita naslednji dve vrstici:

+
+for x in xs:
+    print (x)
+
''', + '''\ +

V zgornjem primeru z zanko for Pythonu naročimo naj se sprehodi čez seznam xs +in na vsakem koraku trenutni element seznama shrani v spremenljivko x. +Kaj naj Python naredi s to spremenljivko, mu naročimo v zamaknjenih vrsticah. +Tokrat vrednost le izpišemo.

'''] + +if_clause = ['''\ +

Poglej, ali je število 42? Uporabite pogojni stavek if!

''', + '''\ +
+if x == 42:
+
'''] + +seen_42 = ['''\ +

Kako si lahko zapomnimo, da smo videli 42? Uporabi novo spremenljivko!

+''', + '''\ +

Spremenljivko na začetku nastavimo na False, npr.:

+
+videl42 = False
+
+

in jo tekom zanke ustrezno spremenimo.'''] + +plan = ['''\ +

Plan. Če bi morali nekomu povedati, kako naj se loti te naloge, bi mu lahko rekli:

+
+Za vsak element v seznamu
+    Poglej, ali je 42?
+        Če je, si zapomni, da si videl 42.
+Izpiši, ali si videl 42 ali ne.
+
+

Zdaj pa je potrebno le še slovenščino prevesti v Python.

+''', + for_loop, + if_clause, + seen_42] + +hint = { + 'no_xs': ['''\ +

Program mora imeti na začetku definiran seznam xs.

'''], + + 'for_loop': for_loop, + + 'if_clause': if_clause, + + 'printing': ['''\ +

Izpišite rezultat.

'''], + + 'print_out_for': ['''\ +

Pazite, da izpišete rezultat izven zanke!

'''] + +} diff --git a/python/problems/lists_and_for/contains_string/common.py b/python/problems/lists_and_for/contains_string/common.py new file mode 100644 index 0000000..b69a4ea --- /dev/null +++ b/python/problems/lists_and_for/contains_string/common.py @@ -0,0 +1,96 @@ +# coding=utf-8 + +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 194 +group = 'lists_and_for' +number = 2 +visible = True + +solution = '''\ +xs = ['foo', 'bar', 'baz', 'Waldo', 'foobar'] + +vsebuje = False +for x in xs: + if x == 'Waldo': + vsebuje = True +print(vsebuje) +''' + +hint_type = { + 'plan': HintSequence('plan', 1), + 'no_xs': Hint('no_xs'), + 'for_loop': Hint('for_loop'), + 'if_clause': Hint('if_clause'), + 'printing': Hint('printing'), + 'print_out_for': Hint('print_out_for') +} + +def test(python, code): + test_xs = [['foo', 'bar', 'baz', 'Waldo', 'foobar'], + ['foo', 'bar', 'baz', 'Waldo'], + ['Waldo', 'foo', 'bar', 'baz'], + [], + ['Waldo'], + ['waldo', 'foo', 'bar', 'baz']] + test_out = [ + True, + True, + True, + False, + True, + False + ] + + n_correct = 0 + for xs_i, xs in enumerate(test_xs): + # change code to contain new xs instead of the one + # given by user + tcode = re.sub(r'^xs\s*=\s*\[.*?\]', + 'xs = ' + str(xs), + code, + flags = re.DOTALL | re.MULTILINE) + + # use python session to call tcode + answers = python(code=tcode, inputs=[(None, None)], timeout=1.0) + output = answers[0][1] + + if str(test_out[xs_i]) in output and \ + str(not test_out[xs_i]) not in output: + n_correct += 1 + return n_correct, len(test_out) + +def hint(python, code): + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer) + if exc: return exc + + tokens = get_tokens(code) + print(tokens) + + # if has no xs, tell him to ask for values + if not has_token_sequence(tokens, ['xs', '=', '[']): + return [{'id' : 'no_xs'}] + + # student does not have while or for: instruct him on loops + if not has_token_sequence(tokens, ['while']) and \ + not has_token_sequence(tokens, ['for']): + return [{'id' : 'for_loop'}] + + if not has_token_sequence(tokens, ['if']): + return [{'id' : 'if_clause'}] + + # student is not using print function + if not has_token_sequence(tokens, ['print']): + return [{'id' : 'printing'}] + + # student is not using print function + if not has_token_sequence(tokens, ['\n', 'print']): + return [{'id' : 'print_out_for'}] + + + return None diff --git a/python/problems/lists_and_for/contains_string/en.py b/python/problems/lists_and_for/contains_string/en.py new file mode 100644 index 0000000..20b395b --- /dev/null +++ b/python/problems/lists_and_for/contains_string/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 194 +name = '(translation missing)' +slug = '(translation missing)' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

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

(translation missing)

''', +} diff --git a/python/problems/lists_and_for/contains_string/sl.py b/python/problems/lists_and_for/contains_string/sl.py new file mode 100644 index 0000000..5d42784 --- /dev/null +++ b/python/problems/lists_and_for/contains_string/sl.py @@ -0,0 +1,68 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 194 +name = 'Vsebuje niz' +slug = 'Vsebuje niz' + + +description = '''\ +

Enako kot naloga vsebuje, le da bomo tokrat v seznamu nizov iskali niz 'Waldo'. +Program naj se začne z

+
+xs = ['foo', 'bar', 'baz', 'Waldo', 'foobar']
+
+

in izpiše True ali False.

+''' + +for_loop = ['''\ +

Čez elemente v seznamu se najlažje sprehodimo s for zanko. +''', + '''\ +

Poskusi, kaj naredita naslednji dve vrstici:

+
+for x in xs:
+    print (x)
+
''', + '''\ +

V zgornjem primeru z zanko for Pythonu naročimo naj se sprehodi čez seznam xs +in na vsakem koraku trenutni element seznama shrani v spremenljivko x. +Kaj naj Python naredi s to spremenljivko, mu naročimo v zamaknjenih vrsticah. +Tokrat vrednost le izpišemo.

'''] + +if_clause = ['''\ +

Poglej, ali je element Waldo? Uporabite pogojni stavek if!

''', + '''\ +
+if x == 'Waldo':
+
'''] + +plan = ['''\ +

Plan. je enak kot pri prvi nalogi: +

+Za vsak element v seznamu
+    Poglej, ali je element enak 'Waldo'?
+        Če je, si to zapomni.
+Izpiši, ali si videl 'Waldo' ali ne.
+
+''', + for_loop, + if_clause] + +hint = { + 'no_xs': ['''\ +

Program mora imeti na začetku definiran seznam xs.

'''], + + 'for_loop': for_loop, + + 'if_clause': if_clause, + + 'printing': ['''\ +

Izpišite rezultat!

'''], + + 'print_out_for': ['''\ +

Pazite, da izpišete rezultat izven zanke!

'''] + +} -- cgit v1.2.1