diff options
author | Martin <martin@leo.fri1.uni-lj.si> | 2015-09-22 17:28:25 +0200 |
---|---|---|
committer | Martin <martin@leo.fri1.uni-lj.si> | 2015-09-22 17:28:25 +0200 |
commit | ba4bee9a9a508eb1a686808241559b331f2d33ea (patch) | |
tree | 1e62fc15c369c6250fd8330306bbf85c5168a9d9 /python | |
parent | cb71b2083b2f87b2f0466577dbe3e11efe22410c (diff) |
Added Greatest Negative problem.
Diffstat (limited to 'python')
-rw-r--r-- | python/common.py | 2 | ||||
-rw-r--r-- | python/problems/functions/greatest_negative/common.py | 114 | ||||
-rw-r--r-- | python/problems/functions/greatest_negative/en.py | 16 | ||||
-rw-r--r-- | python/problems/functions/greatest_negative/sl.py | 97 | ||||
-rw-r--r-- | python/sl.py | 9 |
5 files changed, 238 insertions, 0 deletions
diff --git a/python/common.py b/python/common.py index 2b66aff..e4de997 100644 --- a/python/common.py +++ b/python/common.py @@ -13,6 +13,8 @@ hint_type = { 'error': Hint('error'), 'eof_error': Hint('eof_error'), 'timed_out': Hint('timed_out'), + 'problematic_test_case': Hint('problematic_test_case'), + 'no_func_name': Hint('no_func_name') } def hint(python, program): diff --git a/python/problems/functions/greatest_negative/common.py b/python/problems/functions/greatest_negative/common.py new file mode 100644 index 0000000..01dfce3 --- /dev/null +++ b/python/problems/functions/greatest_negative/common.py @@ -0,0 +1,114 @@ +# 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 = 197 +group = 'functions' +number = 3 +visible = True + +solution = '''\ +def max_neg(xs): + mn = None + for x in xs: + if x < 0 and (mn == None or x > mn): + mn = x + return mn +''' + +hint_type = { + 'no_def': Hint('no_def'), + 'no_return': Hint('no_return'), + 'return_indent': Hint('return_indent'), + 'for_loop': Hint('for_loop'), + 'not_int': Hint('not_int'), + 'return_first': Hint('return_first'), + 'return_last': Hint('return_last'), + 'return_greatest': Hint('return_greatest'), + 'return_positive': Hint('return_positive'), + 'return_absolute': Hint('return_absolute'), +} + + +def test(python, code): + test_lists = [[6, 4, 2, 0], + [4, 6, 2, -1], + [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, 0, -6, -2], + [-1, -8, -6, -2], + [42], + [-42]] + + test_in = [('max_neg(%s)'%str(l), None) for l in test_lists] + test_out = [None, -1, -2, None, -8, -8, -2, -1, -2, -1, None, -42] + + answers = python(code=code, inputs=test_in, timeout=1.0) + 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)}}) + + return n_correct == len(test_in), hints + +def hint(python, code): + tokens = get_tokens(code) + # having function max_neg is necessary! + if not has_token_sequence(tokens, ['max_neg']): + return [{'id' : 'no_func_name', 'args' : {'func_name' : 'max_neg'}}] + + # run one test to see if there are any exceptions + answer = python(code=code, inputs=[('max_neg([1, 3, -3, -6, 4, 3, 2])', None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + # if has no def, tell him to define the function + if not has_token_sequence(tokens, ['def']): + return [{'id' : 'no_def'}] + + # if has no return, tell him to return the value + if not has_token_sequence(tokens, ['return']): + return [{'id' : 'no_return'}] + + # if has no loop, tell him to use it + if not has_token_sequence(tokens, ['while']) and \ + not has_token_sequence(tokens, ['for']): + return [{'id' : 'for_loop'}] + + # if has no condition if, tell him to use it + 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'}], + 2: [{'id' : 'return_last'}], + 4: [{'id' : 'return_greatest'}], + 3: [{'id' : 'return_positive'}], + -6: [{'id' : 'return_absolute'}] + } + 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_negative/en.py b/python/problems/functions/greatest_negative/en.py new file mode 100644 index 0000000..92e5841 --- /dev/null +++ b/python/problems/functions/greatest_negative/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 197 +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/functions/greatest_negative/sl.py b/python/problems/functions/greatest_negative/sl.py new file mode 100644 index 0000000..c0b7515 --- /dev/null +++ b/python/problems/functions/greatest_negative/sl.py @@ -0,0 +1,97 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 197 +name = 'Največji negativec' +slug = 'Največji negativec' + + +description = '''\ +<p>Napiši funkcijo <code>max_neg(xs)</code>, ki vrne največje negativno število +v seznamu <code>xs</code>. Če seznam nima negativnih števil, naj funkcija vrne <code>None</code>.</p> +<pre> +>>> max_neg([5, 1, -6, -7, 2]) +-6 +</pre> +''' + +function = ['''\ +<p>Napiši <b>definicijo funkcije</b> <code>max_neg(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 max_neg(xs): + # poišči največje negativno število v xs + +s = [5, 1, -6, -7, 2] +print (max_neg(s)) +</pre>'''] + +main_plan = ['''\ +<p><b>Plan</b>: po vrsti pogledamo vse elemente in sproti hranimo največje negativno +število.</p>''', + '''\ +<p>Na začetku ne moremo največjemu nastaviti vrednosti prvega elementa v seznamu, +saj je lahko prvi element pozitiven.</p>''', + '''\ +</p>Nastavite ga na <code>None</code></p>''', + ] + +return_clause = ['''\ +<p>Namesto, da izpišemo rezultat, ga vračamo s stavkom <code>return</code>.</p>''', + '''\ +<p>Pazi, da vrneš rezultat, šele ko se zanka izteče.</p>'''] + +plan = [function, + main_plan, + return_clause] + +for_loop = ['''\ +<p>Preglej elemente z zanko</p>. +''', + '''\ +<pre> +for x in xs: +</pre>'''] + + +if_clause = ['''\ +<p>Preveri, če je trenutni element negativen in večji od trenutno največjega negativca.</p>''',] + + +hint = { + 'no_def': function, + + 'no_return': return_clause, + + 'for_loop': for_loop, + + 'if_clause': if_clause, + + '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_greatest': '''\ +<p>Funkcija vrača največji pozitivni element v seznamu (ne pa največji negativni).''', + + 'return_positive': '''\ +<p>Funkcija napačno vrača pozitivno vrednost največjega negativnega števila.''', + + 'return_absolute': '''\ +<p>Funkcija napačno vrača element z največjo absolutno vrednostjo.</p>''', + + '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/sl.py b/python/sl.py index e7bbed4..3f97964 100644 --- a/python/sl.py +++ b/python/sl.py @@ -44,6 +44,15 @@ hint = { <p>Program je opravil [%=passed%] / [%=total%] testov.</p> '''], + 'problematic_test_case': ['''\ +<p>Problematični testni primer: [%=testin%]<p> +<p>Pravilen rezultat: [%=testout%]</p> +'''], + + 'no_func_name': ['''\ +<p>Funkcija z imenom [%=func_name%] ni definirana. </p> +'''], + 'syntax_error': ['''\ <p>Program vsebuje sintaktično napako v vrstici [%=lineno%]:</p> <pre> |