summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--python/problems/lists_and_for/contains_42/common.py97
-rw-r--r--python/problems/lists_and_for/contains_42/en.py16
-rw-r--r--python/problems/lists_and_for/contains_42/sl.py81
-rw-r--r--python/problems/lists_and_for/contains_string/common.py96
-rw-r--r--python/problems/lists_and_for/contains_string/en.py16
-rw-r--r--python/problems/lists_and_for/contains_string/sl.py68
6 files changed, 374 insertions, 0 deletions
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 = '''\
+<p>(translation missing)</p>'''
+
+hint = {
+ 'plan': '''\
+<p>(translation missing)</p>''',
+
+ 'no_input_call': '''\
+<p>(translation missing)</p>''',
+}
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 = '''\
+<p>Napiši program, ki ugotovi ali seznam števil vsebuje število 42.
+Seznam z imenom <code>xs</code> definiraj na vrhu programa. Primer:</p>
+<pre>
+xs = [42, 5, 4, -7, 2, 12, -3, -4, 11, 42, 2]
+</pre>
+<p>Program naj izpiše le <code>True</code> ali <code>False</code>.
+Seveda mora program delati za poljubne sezname in ne samo za seznam iz primera.</p>
+'''
+
+for_loop = ['''\
+<p>Čez elemente v seznamu se najlažje sprehodimo s <b>for</b> zanko.
+''',
+ '''\
+<p>Poskusi, kaj naredita naslednji dve vrstici:</p>
+<pre>
+for x in xs:
+ print (x)
+</pre>''',
+ '''\
+<p>V zgornjem primeru z zanko <code>for</code> Pythonu naročimo naj se sprehodi čez seznam <code>xs</code>
+in na vsakem koraku trenutni element seznama shrani v spremenljivko <code>x</code>.
+Kaj naj Python naredi s to spremenljivko, mu naročimo v zamaknjenih vrsticah.
+Tokrat vrednost le izpišemo.</p>''']
+
+if_clause = ['''\
+<p><code>Poglej, ali je število 42?</code> Uporabite pogojni stavek <b>if</b>!</p>''',
+ '''\
+<pre>
+if x == 42:
+</pre>''']
+
+seen_42 = ['''\
+<p>Kako si lahko zapomnimo, da smo <b>videli 42</b>? Uporabi novo spremenljivko!</p>
+''',
+ '''\
+<p>Spremenljivko na začetku nastavimo na False, npr.:</p>
+<pre>
+videl42 = False
+</pre>
+<p>in jo tekom zanke ustrezno spremenimo.''']
+
+plan = ['''\
+<p><b>Plan.</b> Če bi morali nekomu povedati, kako naj se loti te naloge, bi mu lahko rekli:</p>
+<pre>
+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.
+</pre>
+<p>Zdaj pa je potrebno le še slovenščino prevesti v Python.</p>
+''',
+ for_loop,
+ if_clause,
+ seen_42]
+
+hint = {
+ 'no_xs': ['''\
+<p>Program mora imeti na začetku definiran seznam <code>xs</code>.</p>'''],
+
+ 'for_loop': for_loop,
+
+ 'if_clause': if_clause,
+
+ 'printing': ['''\
+<p>Izpišite rezultat.</p>'''],
+
+ 'print_out_for': ['''\
+<p>Pazite, da izpišete rezultat izven zanke!</p>''']
+
+}
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 = '''\
+<p>(translation missing)</p>'''
+
+hint = {
+ 'plan': '''\
+<p>(translation missing)</p>''',
+
+ 'no_input_call': '''\
+<p>(translation missing)</p>''',
+}
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 = '''\
+<p>Enako kot naloga vsebuje, le da bomo tokrat v seznamu nizov iskali niz 'Waldo'.
+Program naj se začne z </p>
+<pre>
+xs = ['foo', 'bar', 'baz', 'Waldo', 'foobar']
+</pre>
+<p> in izpiše <code>True</code> ali <code>False</code>.</p>
+'''
+
+for_loop = ['''\
+<p>Čez elemente v seznamu se najlažje sprehodimo s <b>for</b> zanko.
+''',
+ '''\
+<p>Poskusi, kaj naredita naslednji dve vrstici:</p>
+<pre>
+for x in xs:
+ print (x)
+</pre>''',
+ '''\
+<p>V zgornjem primeru z zanko <code>for</code> Pythonu naročimo naj se sprehodi čez seznam <code>xs</code>
+in na vsakem koraku trenutni element seznama shrani v spremenljivko <code>x</code>.
+Kaj naj Python naredi s to spremenljivko, mu naročimo v zamaknjenih vrsticah.
+Tokrat vrednost le izpišemo.</p>''']
+
+if_clause = ['''\
+<p><code>Poglej, ali je element Waldo?</code> Uporabite pogojni stavek <b>if</b>!</p>''',
+ '''\
+<pre>
+if x == 'Waldo':
+</pre>''']
+
+plan = ['''\
+<p><b>Plan.</b> je enak kot pri prvi nalogi:
+<pre>
+Za vsak element v seznamu
+ Poglej, ali je element enak 'Waldo'?
+ Če je, si to zapomni.
+Izpiši, ali si videl 'Waldo' ali ne.
+</pre>
+''',
+ for_loop,
+ if_clause]
+
+hint = {
+ 'no_xs': ['''\
+<p>Program mora imeti na začetku definiran seznam <code>xs</code>.</p>'''],
+
+ 'for_loop': for_loop,
+
+ 'if_clause': if_clause,
+
+ 'printing': ['''\
+<p>Izpišite rezultat!</p>'''],
+
+ 'print_out_for': ['''\
+<p>Pazite, da izpišete rezultat izven zanke!</p>''']
+
+}