summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--python/problems/functions/greatest/common.py82
-rw-r--r--python/problems/functions/greatest/sl.py36
-rw-r--r--python/problems/functions/greatest_absolutist/common.py67
-rw-r--r--python/problems/functions/greatest_absolutist/sl.py20
-rw-r--r--python/problems/functions/greatest_negative/common.py2
-rw-r--r--python/problems/functions/greatest_negative/sl.py1
6 files changed, 146 insertions, 62 deletions
diff --git a/python/problems/functions/greatest/common.py b/python/problems/functions/greatest/common.py
index 91f773e..74fb15e 100644
--- a/python/problems/functions/greatest/common.py
+++ b/python/problems/functions/greatest/common.py
@@ -11,12 +11,12 @@ number = 1
visible = True
solution = '''\
-def naj(xs):
- naj = xs[0]
+def max_val(xs):
+ mv = xs[0]
for x in xs:
- if x > naj:
- naj = x
- return naj
+ if x > mv:
+ mv = x
+ return mv
'''
hint_type = {
@@ -24,35 +24,55 @@ hint_type = {
'no_return': Hint('no_return'),
'for_loop': Hint('for_loop'),
'if_clause': Hint('if_clause'),
+ 'final_hint': Hint('final_hint'),
+ 'not_int': Hint('not_int'),
+ 'return_first': Hint('return_first'),
+ 'return_last': Hint('return_last'),
+ 'return_indent': Hint('return_indent')
}
def test(python, code):
- test_in = [('naj([6, 4, 2, 0])', None),
- ('naj([4, 6, 2, 0])', None),
- ('naj([4, 2, 6, 0])', None),
- ('naj([4, 2, 0, 6])', None),
- ('naj([6, -8, 2, 0])', None),
- ('naj([-8, 6, 2, 0])', None),
- ('naj([-8, -6, -2, 0])', None),
- ('naj([-8, -6, -2, -1])', None),
- ('naj([-8, -1, -6, -2])', None),
- ('naj([-1, -8, -6, -2])', None),
- ('naj([42])', None),
- ('naj([-42])', None)]
-
+ test_lists = [[6, 4, 2, 0],
+ [4, 6, 2, 0],
+ [4, 2, 6, 0],
+ [4, 2, 0, 6],
+ [6, -8, 2, 0],
+ [-8, 6, 2, 0],
+ [-8, -6, -2, 0],
+ [-8, -6, -2, -1],
+ [-8, -1, -6, -2],
+ [-1, -8, -6, -2],
+ [42],
+ [-42]]
+ test_in = [('max_val(%s)'%str(l), None) for l in test_lists]
test_out = [6, 6, 6, 6, 6, 6, 0, -1, -1, -1, 42, -42]
answers = python(code=code, inputs=test_in, timeout=1.0)
- n_correct = sum(ans[0] == to for ans, to in zip(answers, test_out))
- return n_correct, len(test_in)
+ n_correct = 0
+ tin, tout = None, None
+ for i, (ans, to) in enumerate(zip(answers, test_out)):
+ n_correct += ans[0] == to
+ if ans[0] != to:
+ tin = test_lists[i]
+ tout = to
+ hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
+ if tin:
+ hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})
+ if n_correct == len(test_in): # and an iteresting hint
+ hints.append({'id': 'final_hint'})
+ return n_correct == len(test_in), hints
def hint(python, code):
+ tokens = get_tokens(code)
+ # having function max_val is necessary!
+ if not has_token_sequence(tokens, ['max_val']):
+ return [{'id' : 'no_func_name', 'args' : {'func_name' : 'max_val'}}]
+
# run one test first to see if there are any exceptions
- answer = python(code=code, inputs=[('naj([1, 2, 3, 6, 4, 2, 0])', None)], timeout=1.0)
- exc = get_exception_desc(answer)
+ answer = python(code=code, inputs=[('max_val([1, 2, 3, 6, 4, 2, 5])', None)], timeout=1.0)
+ exc = get_exception_desc(answer[0][3])
if exc: return exc
- tokens = get_tokens(code)
# if has no def, tell him to define the function
if not has_token_sequence(tokens, ['def']):
return [{'id' : 'no_def'}]
@@ -70,4 +90,20 @@ def hint(python, code):
if not has_token_sequence(tokens, ['if']):
return [{'id' : 'if_clause'}]
+ # test for wrong answers
+ res = answer[0][0]
+ if not isinstance(res, int):
+ return [{'id' : 'not_int'}]
+ cases = {
+ 1: [{'id' : 'return_first'}],
+ 5: [{'id' : 'return_last'}],
+ }
+ if res in cases:
+ return cases[res]
+
+ # if has no return at the beginning of block,
+ # tell not to return from for
+ if not has_token_sequence(tokens, ['\n', 'return']):
+ return [{'id' : 'return_indent'}]
+
return None
diff --git a/python/problems/functions/greatest/sl.py b/python/problems/functions/greatest/sl.py
index 0321d23..d018503 100644
--- a/python/problems/functions/greatest/sl.py
+++ b/python/problems/functions/greatest/sl.py
@@ -9,21 +9,21 @@ slug = 'Največji'
description = '''\
-<p>Napišite funkcijo <code>naj(xs)</code>, ki vrne največje število v seznamu <code>xs</code>.
+<p>Napišite funkcijo <code>max_val(xs)</code>, ki vrne največje število v seznamu <code>xs</code>.
<pre>
->>> naj([5, 1, -6, -7, 2])
+>>> max_val([5, 1, -6, -7, 2])
5
</pre></p>
'''
function = ['''\
-<p>Napišite definicijo funckcije <code>naj(xs)</code></p>''',
+<p>Napišite definicijo funckcije <code>max_val(xs)</code></p>''',
'''\
<p>Definicijo funkcije začnemo z <code>def</code>, temu sledi ime, potem oklepaji,
v katerih naštejemo argumente funkcije, nato zaklepaj in na koncu dvopičje</p>''',
'''\
<pre>
-def naj(xs):
+def max_val(xs):
# program, ki poišče največjega v xs
</pre>''']
@@ -64,14 +64,14 @@ xs = [5, 1, -6, -7, 2]
'''\
<pre>
# najprej definicija funkcije
-def naj...
+def max_val...
s1 = [5, 1, -6, -7, 2]
-naj_elt = naj(s1)
+naj_elt = max_val(s1)
print(naj_elt)
s2 = [-5, -1, 6, 7, -2]
-naj_elt = naj(s2)
+naj_elt = max_val(s2)
print(naj_elt)
</pre>'''],
function,
@@ -103,5 +103,25 @@ hint = {
'for_loop': for_loop,
- 'if_clause': if_clause
+ 'if_clause': if_clause,
+
+ 'final_hint': '''\
+<p><b>Odlično, naloga rešena! Še zanimivost:</b> Python ima funkcijo <code>max</code> že vgrajeno:</p>
+<pre>
+def max_val(xs):
+ return max(xs)
+</pre>''',
+
+ 'not_int': '''\
+<p>Funkcija ne vrača števila</p>''',
+
+ 'return_first': '''\
+<p>Funkcija vrača prvi element v seznamu''',
+
+ 'return_last': '''\
+<p>Funkcija vrača zadnji element v seznamu.''',
+
+ 'return_indent': '''\
+<p>Ali imaš stavek <code>return</code> znotraj zanke?
+V tem primeru se lahko zgodi, da se zanka ne izteče do konca.</p>'''
}
diff --git a/python/problems/functions/greatest_absolutist/common.py b/python/problems/functions/greatest_absolutist/common.py
index 7871243..8174a63 100644
--- a/python/problems/functions/greatest_absolutist/common.py
+++ b/python/problems/functions/greatest_absolutist/common.py
@@ -11,12 +11,12 @@ number = 2
visible = True
solution = '''\
-def naj_abs(xs):
- naj = xs[0]
+def max_abs(xs):
+ ma = xs[0]
for x in xs:
- if abs(x) > abs(naj):
- naj = x
- return naj
+ if abs(x) > abs(ma):
+ ma = x
+ return ma
'''
hint_type = {
@@ -29,37 +29,56 @@ hint_type = {
'return_last': Hint('return_last'),
'return_greatest': Hint('return_greatest'),
'return_positive': Hint('return_positive'),
+ 'return_indent': Hint('return_indent'),
+ 'final_hint': Hint('final_hint')
}
def test(python, code):
- test_in = [('naj_abs([6, 4, 2, 0])', None),
- ('naj_abs([4, 6, 2, 0])', None),
- ('naj_abs([4, 2, 6, 0])', None),
- ('naj_abs([4, 2, 0, 6])', None),
- ('naj_abs([6, -8, 2, 0])', None),
- ('naj_abs([-8, 6, 2, 0])', None),
- ('naj_abs([8, -6, -2, 0])', None),
- ('naj_abs([-5, -6, -2, -1])', None),
- ('naj_abs([-8, -1, -6, -2])', None),
- ('naj_abs([-1, -8, -6, -2])', None),
- ('naj_abs([42])', None),
- ('naj_abs([-42])', None)]
-
+ test_lists = [[6, 4, 2, 0],
+ [4, 6, 2, 0],
+ [4, 2, 6, 0],
+ [4, 2, 0, 6],
+ [6, -8, 2, 0],
+ [-8, 6, 2, 0],
+ [8, -6, -2, 0],
+ [-5, -6, -2, -1],
+ [-8, -1, -6, -2],
+ [-1, -8, -6, -2],
+ [42],
+ [-42]]
+ test_in = [('max_abs(%s)'%str(l), None) for l in test_lists]
test_out = [6, 6, 6, 6, -8, -8, 8, -6, -8, -8, 42, -42]
answers = python(code=code, inputs=test_in, timeout=1.0)
- n_correct = sum(ans[0] == to for ans, to in zip(answers, test_out))
- return n_correct, len(test_in)
+ n_correct = 0
+ tin, tout = None, None
+ for i, (ans, to) in enumerate(zip(answers, test_out)):
+ n_correct += ans[0] == to
+ if ans[0] != to:
+ tin = test_lists[i]
+ tout = to
+ hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
+ if tin:
+ hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})
+ if n_correct == len(test_in): # add an iteresting hint
+ tokens = get_tokens(code)
+ if not has_token_sequence(tokens, ['lambda']):
+ hints.append({'id': 'final_hint'})
+ return n_correct == len(test_in), hints
+
def hint(python, code):
+ tokens = get_tokens(code)
+ # having function max_val is necessary!
+ if not has_token_sequence(tokens, ['max_abs']):
+ return [{'id' : 'no_func_name', 'args' : {'func_name' : 'max_abs'}}]
+
# run one test first to see if there are any exceptions
- answer = python(code=code, inputs=[('naj_abs([1, 3, 3, -6, 4, 3, 2])', None)], timeout=1.0)
- exc = get_exception_desc(answer)
+ answer = python(code=code, inputs=[('max_abs([1, 3, 3, -6, 4, 3, 2])', None)], timeout=1.0)
+ exc = get_exception_desc(answer[0][3])
if exc: return exc
- tokens = get_tokens(code)
- print(tokens)
# if has no def, tell him to define the function
if not has_token_sequence(tokens, ['def']):
return [{'id' : 'no_def'}]
diff --git a/python/problems/functions/greatest_absolutist/sl.py b/python/problems/functions/greatest_absolutist/sl.py
index 77815c0..d51cd96 100644
--- a/python/problems/functions/greatest_absolutist/sl.py
+++ b/python/problems/functions/greatest_absolutist/sl.py
@@ -9,26 +9,26 @@ slug = 'Največji absolutist'
description = '''\
-<p>Napišite funkcijo <code>naj_abs(xs)</code>, ki vrne največje število po
+<p>Napišite funkcijo <code>max_abs(xs)</code>, ki vrne največje število po
absolutni vrednosti v seznamu <code>xs</code>.</p>
<pre>
->>> naj_abs([5, 1, -6, -7, 2])
+>>> max_abs([5, 1, -6, -7, 2])
-7
</pre>
'''
function = ['''\
-<p>Napišite <b>definicijo funkcije</b> <code>naj_abs(xs)</code>.</p>''',
+<p>Napišite <b>definicijo funkcije</b> <code>max_abs(xs)</code>.</p>''',
'''\
<p>Definicijo funkcije začnemo z <code>def</code>, temu sledi ime, potem oklepaji,
v katerih naštejemo argumente funkcije, nato zaklepaj in na koncu dvopičje</p>''',
'''\
<pre>
-def naj_abs(xs):
+def max_abs(xs):
# poišči največje absolutno število v xs
s = [5, 1, -6, -7, 2]
-print (naj_abs(s))
+print (max_abs(s))
</pre>''']
main_plan = ['''\
@@ -94,7 +94,15 @@ hint = {
'return_indent': '''\
<p>Ali imate stavek <code>return</code> znotraj zanke?
-V tem primeru se lahko zgodi, da se zanka ne izteče do konca.</p>'''
+V tem primeru se lahko zgodi, da se zanka ne izteče do konca.</p>''',
+
+ 'final_hint': '''\
+<p><b>Odlično, naloga rešena! Še zanimivost:</b>
+Tudi to nalogo lahko rešimo s funkcijo <code>max</code>:</p>
+<pre>
+def max_abs(xs):
+ return max(xs, key = lambda x: abs(x))
+</pre>''',
}
diff --git a/python/problems/functions/greatest_negative/common.py b/python/problems/functions/greatest_negative/common.py
index 01dfce3..2425bbc 100644
--- a/python/problems/functions/greatest_negative/common.py
+++ b/python/problems/functions/greatest_negative/common.py
@@ -30,6 +30,7 @@ hint_type = {
'return_greatest': Hint('return_greatest'),
'return_positive': Hint('return_positive'),
'return_absolute': Hint('return_absolute'),
+ 'return_indent': Hint('return_indent')
}
@@ -61,7 +62,6 @@ def test(python, code):
hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
if tin:
hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})
-
return n_correct == len(test_in), hints
def hint(python, code):
diff --git a/python/problems/functions/greatest_negative/sl.py b/python/problems/functions/greatest_negative/sl.py
index c0b7515..ea1e47c 100644
--- a/python/problems/functions/greatest_negative/sl.py
+++ b/python/problems/functions/greatest_negative/sl.py
@@ -93,5 +93,6 @@ hint = {
'return_indent': '''\
<p>Ali imaš stavek <code>return</code> znotraj zanke?
V tem primeru se lahko zgodi, da se zanka ne izteče do konca.</p>'''
+
}