diff options
Diffstat (limited to 'python/problems/functions')
40 files changed, 1173 insertions, 10 deletions
diff --git a/python/problems/functions/assign_numbers/common.py b/python/problems/functions/assign_numbers/common.py new file mode 100644 index 0000000..effd061 --- /dev/null +++ b/python/problems/functions/assign_numbers/common.py @@ -0,0 +1,64 @@ +# 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 + +id = 225 +number = 4 +visible = True + +solution = '''\ +def numbers(xs): + ys = [] + for i in range(len(xs)): + ys.append((i, xs[i])) + return ys +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'numbers' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + ([], []), + ([1], [(0, 1)]), + ([5, 1, 4, 2, 3], [(0, 5), (1, 1), (2, 4), (3, 2), (4, 3)]), + ] + + test_in = [(func_name+'(%s)'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + 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_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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 passed: + hints.append({'id': 'final_hint'}) + return passed, hints + +def hint(python, code): + tokens = get_tokens(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[0][3]) + if exc: return exc + + return None diff --git a/python/problems/functions/assign_numbers/en.py b/python/problems/functions/assign_numbers/en.py new file mode 100644 index 0000000..a68c6c8 --- /dev/null +++ b/python/problems/functions/assign_numbers/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 225 +name = 'Assign numbers' +slug = 'Assign numbers' + +description = '''\ +<p>(translation missing)</p>''' + +hint = { + 'plan': '''\ +<p>(translation missing)</p>''', + + 'no_input_call': '''\ +<p>(translation missing)</p>''', +} diff --git a/python/problems/functions/assign_numbers/sl.py b/python/problems/functions/assign_numbers/sl.py new file mode 100644 index 0000000..97c0c3a --- /dev/null +++ b/python/problems/functions/assign_numbers/sl.py @@ -0,0 +1,34 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 225 +name = 'Oštevilči' +slug = 'Oštevilči' + + +description = '''\ +<p> +Napišite funkcijo <code>numbers(xs)</code>, ki vrne seznam oblike <code>[(0, xs[0]), (1, xs[1]), ..., (n, xs[n])]</code>. +Število <code>n</code> je enako dolžini seznama <code>xs</code> minus ena. +<pre> +>>> numbers([4, 4, 4]) +[(0, 4), (1, 4), (2, 4)] +>>> numbers([5, 1, 4, 2, 3]) +[(0, 5), (1, 1), (2, 4), (3, 2), (4, 3)] +</pre> +</p>''' + +plan = ['''\ +<p></p> +''', + '''\ +<p></p>'''] + +hint = { + 'final_hint': ['''\ +<p>Program je pravilen! <br> +</p> +'''], +} diff --git a/python/problems/functions/body_mass_index/common.py b/python/problems/functions/body_mass_index/common.py new file mode 100644 index 0000000..8e774be --- /dev/null +++ b/python/problems/functions/body_mass_index/common.py @@ -0,0 +1,67 @@ +# 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 + +id = 226 +number = 5 +visible = True + +solution = '''\ +def bmi(xs): + ys = [] + for name, weight, height in xs: + ys.append((name, weight / (height / 100)**2)) + return ys +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'bmi' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + ([], []), + ([('Ana', 55, 165), ('Berta', 60, 153)], + [('Ana', 20.202020202020204), ('Berta', 25.63116749967961)]), + ([('Ana', 55, 165), ('Berta', 60, 153), ('Cilka', 70, 183)], + [('Ana', 20.202020202020204), ('Berta', 25.63116749967961), ('Cilka', 20.902385858042937)]), + ] + + test_in = [(func_name+'(%s)'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + 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_in[i][0] + tout = to + + passed = n_correct == len(test_in) + + 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 passed: + hints.append({'id': 'final_hint'}) + return passed, hints + +def hint(python, code): + tokens = get_tokens(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[0][3]) + if exc: return exc + + return None diff --git a/python/problems/functions/body_mass_index/en.py b/python/problems/functions/body_mass_index/en.py new file mode 100644 index 0000000..fb4663f --- /dev/null +++ b/python/problems/functions/body_mass_index/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 226 +name = 'Body mass index' +slug = 'Body mass index' + +description = '''\ +<p>(translation missing)</p>''' + +hint = { + 'plan': '''\ +<p>(translation missing)</p>''', + + 'no_input_call': '''\ +<p>(translation missing)</p>''', +} diff --git a/python/problems/functions/body_mass_index/sl.py b/python/problems/functions/body_mass_index/sl.py new file mode 100644 index 0000000..fd67c88 --- /dev/null +++ b/python/problems/functions/body_mass_index/sl.py @@ -0,0 +1,35 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 226 +name = 'Indeks telesne teže' +slug = 'Indeks telesne teže' + + +description = '''\ +<p> +Podan imamo seznam trojk: ime osebe, teža, višina, na primer: +<pre> +>>> osebe = [('Ana', 55, 165), ('Berta', 60, 153)] +</pre> +Napišite funkcijo <code>bmi(osebe)</code>, ki na podlagi podanega seznama osebe, sestavi seznam dvojk: ime osebe, indeks telesne teže: +<pre> +>>> bmi(osebe) +[('Ana', 20.202020202020204), ('Berta', 25.63116749967961)] +</pre> +</p>''' + +plan = ['''\ +<p></p> +''', + '''\ +<p></p>'''] + +hint = { + 'final_hint': ['''\ +<p>Program je pravilen! <br> +</p> +'''], +} diff --git a/python/problems/functions/body_mass_index_2/common.py b/python/problems/functions/body_mass_index_2/common.py new file mode 100644 index 0000000..c6cf2b3 --- /dev/null +++ b/python/problems/functions/body_mass_index_2/common.py @@ -0,0 +1,66 @@ +# 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 + +id = 227 +number = 6 +visible = True + +solution = '''\ +def bmi2(names, weights, heights): + ys = [] + for name, weight, height in zip(names, weights, heights): + ys.append((name, weight / (height / 100)**2)) + return ys +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'bmi2' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + (([], [], []), []), + ((['Ana', 'Berta'], [55, 60], [165, 153]), + [('Ana', 20.202020202020204), ('Berta', 25.63116749967961)]), + ((['Ana', 'Berta', 'Cilka'], [55, 60, 70], [165, 153, 183]), + [('Ana', 20.202020202020204), ('Berta', 25.63116749967961), ('Cilka', 20.902385858042937)]), + ] + + test_in = [(func_name+'(*%s)'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + 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_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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 passed: + hints.append({'id': 'final_hint'}) + return passed, hints + +def hint(python, code): + tokens = get_tokens(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[0][3]) + if exc: return exc + + return None diff --git a/python/problems/functions/body_mass_index_2/en.py b/python/problems/functions/body_mass_index_2/en.py new file mode 100644 index 0000000..b34718b --- /dev/null +++ b/python/problems/functions/body_mass_index_2/en.py @@ -0,0 +1,15 @@ +# coding=utf-8 + +name = 'Body mass index 2' +slug = 'Body mass index 2' + +description = '''\ +<p>(translation missing)</p>''' + +hint = { + 'plan': '''\ +<p>(translation missing)</p>''', + + 'no_input_call': '''\ +<p>(translation missing)</p>''', +} diff --git a/python/problems/functions/body_mass_index_2/sl.py b/python/problems/functions/body_mass_index_2/sl.py new file mode 100644 index 0000000..8cd56c7 --- /dev/null +++ b/python/problems/functions/body_mass_index_2/sl.py @@ -0,0 +1,31 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + +name = 'Indeks telesne teže 2' +slug = 'Indeks telesne teže 2' + +description = '''\ +<p> +Naloga je podobna prejšnji, le da imamo tokrat podatke v drugačni obliki: +<pre> +>>> imena = ['Ana', 'Berta'] +>>> teze = [55, 60] +>>> visine = [165, 153] +>>> bmi2(imena, teze, visine) +[('Ana', 20.202020202020204), ('Berta', 25.63116749967961)] +</pre> +</p>''' + +plan = ['''\ +<p></p> +''', + '''\ +<p></p>'''] + +hint = { + 'final_hint': ['''\ +<p>Program je pravilen! <br> +</p> +'''], +} diff --git a/python/problems/functions/common.py b/python/problems/functions/common.py new file mode 100644 index 0000000..ede78e6 --- /dev/null +++ b/python/problems/functions/common.py @@ -0,0 +1,2 @@ +id = 17 +number = 4
\ No newline at end of file diff --git a/python/problems/functions/divisors/common.py b/python/problems/functions/divisors/common.py new file mode 100644 index 0000000..afff7c5 --- /dev/null +++ b/python/problems/functions/divisors/common.py @@ -0,0 +1,69 @@ +# 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 + +id = 230 +number = 9 +visible = True + +solution = '''\ +def divisors(n): + for i in range(1, n): + if n % i == 0: + print(i) +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'divisors' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + (8, [1,2,4]), + (6, [1,2,3]), + (5, [1]), + (1, []), + (2, [1]), + (15, [1,3,5]), + (20, [1,2,4,5,10]) + ] + + test_in = [(func_name+'(%s)'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + 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)): + numbers = sorted(int(n) for n in get_numbers(ans[1])) + if numbers == to: + n_correct += 1 + else: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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 passed: + hints.append({'id': 'final_hint'}) + return passed, hints + +def hint(python, code): + tokens = get_tokens(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[0][3]) + if exc: return exc + + return None diff --git a/python/problems/functions/divisors/en.py b/python/problems/functions/divisors/en.py new file mode 100644 index 0000000..d1e71c2 --- /dev/null +++ b/python/problems/functions/divisors/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 230 +name = 'Divisors' +slug = 'Divisors' + +description = '''\ +<p>(translation missing)</p>''' + +hint = { + 'plan': '''\ +<p>(translation missing)</p>''', + + 'no_input_call': '''\ +<p>(translation missing)</p>''', +} diff --git a/python/problems/functions/divisors/sl.py b/python/problems/functions/divisors/sl.py new file mode 100644 index 0000000..76b0917 --- /dev/null +++ b/python/problems/functions/divisors/sl.py @@ -0,0 +1,26 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 230 +name = 'Delitelji' +slug = 'Delitelji' + + +description = '''\ +<p> +Napiši funkcijo <code>divisors(n)</code>, ki izpiše vse delitelje števila (brez samega sebe), ki ga vnese uporabnik.</p>''' + +plan = ['''\ +<p></p> +''', + '''\ +<p></p>'''] + +hint = { + 'final_hint': ['''\ +<p>Program je pravilen! <br> +</p> +'''], +} diff --git a/python/problems/functions/divisors_sum/common.py b/python/problems/functions/divisors_sum/common.py new file mode 100644 index 0000000..142a448 --- /dev/null +++ b/python/problems/functions/divisors_sum/common.py @@ -0,0 +1,69 @@ +# 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 + +id = 231 +number = 10 +visible = True + +solution = '''\ +def divisors_sum(n): + s = 0 + for i in range(1, n): + if n % i == 0: + s += i + return s +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'divisors_sum' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + (8, 7), + (6, 6), + (5, 1), + (1, 0), + (2, 1), + (15, 9), + (20, 22) + ] + + test_in = [(func_name+'(%s)'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + 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_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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 passed: + hints.append({'id': 'final_hint'}) + return passed, hints + +def hint(python, code): + tokens = get_tokens(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[0][3]) + if exc: return exc + + return None diff --git a/python/problems/functions/divisors_sum/en.py b/python/problems/functions/divisors_sum/en.py new file mode 100644 index 0000000..67806f7 --- /dev/null +++ b/python/problems/functions/divisors_sum/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 231 +name = 'Divisors sum' +slug = 'Divisors sum' + +description = '''\ +<p>(translation missing)</p>''' + +hint = { + 'plan': '''\ +<p>(translation missing)</p>''', + + 'no_input_call': '''\ +<p>(translation missing)</p>''', +} diff --git a/python/problems/functions/divisors_sum/sl.py b/python/problems/functions/divisors_sum/sl.py new file mode 100644 index 0000000..47b8719 --- /dev/null +++ b/python/problems/functions/divisors_sum/sl.py @@ -0,0 +1,27 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 231 +name = 'Vsota deliteljev' +slug = 'Vsota deliteljev' + + +description = '''\ +<p> +Napiši funkcijo <code>divisors_sum(n)</code>, ki vrne vsoto vseh deliteljev števila, ki ga vnese uporabnik. +</p>''' + +plan = ['''\ +<p></p> +''', + '''\ +<p></p>'''] + +hint = { + 'final_hint': ['''\ +<p>Program je pravilen! <br> +</p> +'''], +} diff --git a/python/problems/functions/friendly_numbers/common.py b/python/problems/functions/friendly_numbers/common.py new file mode 100644 index 0000000..fe121dc --- /dev/null +++ b/python/problems/functions/friendly_numbers/common.py @@ -0,0 +1,75 @@ +# 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 + +id = 233 +number = 12 +visible = True + +solution = '''\ +def divisors_sum(n): + s = 0 + for i in range(1, n): + if n % i == 0: + s += i + return s + +def amicable_number(n): + s = divisors_sum(n) + if n == divisors_sum(s): + return s +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'amicable_number' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + (220, 284), + (284, 220), + (1010, None), + (6, None), + (2620, 2924), + (1, None), + (10, None), + (1210, 1184) + ] + + test_in = [(func_name+'(%s)'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + 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_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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 passed: + hints.append({'id': 'final_hint'}) + return passed, hints + +def hint(python, code): + tokens = get_tokens(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[0][3]) + if exc: return exc + + return None diff --git a/python/problems/functions/friendly_numbers/en.py b/python/problems/functions/friendly_numbers/en.py new file mode 100644 index 0000000..c77dca1 --- /dev/null +++ b/python/problems/functions/friendly_numbers/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 233 +name = 'Amicable numbers' +slug = 'Amicable numbers' + +description = '''\ +<p>(translation missing)</p>''' + +hint = { + 'plan': '''\ +<p>(translation missing)</p>''', + + 'no_input_call': '''\ +<p>(translation missing)</p>''', +} diff --git a/python/problems/functions/friendly_numbers/sl.py b/python/problems/functions/friendly_numbers/sl.py new file mode 100644 index 0000000..51d47ae --- /dev/null +++ b/python/problems/functions/friendly_numbers/sl.py @@ -0,0 +1,37 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 233 +name = 'Prijateljska števila' +slug = 'Prijateljska števila' + + +description = '''\ +<p> +220 in 284 sta prijateljski števili. Delitelji 220 so 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 in 110. +Če jih seštejemo, dobimo 284. Delitelji 284 pa so 1, 2, 4, 71 in 142. Vsota teh števil pa je 220. +Napiši funkcijo <code>amicable_number(n)</code>, ki vrne prijateljsko število številu <code>n</code>, če ga ima, oz. +vrne <code>None</code>, če ga nima. Primer: +<pre> +>>> amicable_number(220) +284 +>>> amicable_number(222) +None +</pre> +</p> +<p> Uporabite funkcijo za vsoto deliteljev!</p>''' + +plan = ['''\ +<p></p> +''', + '''\ +<p></p>'''] + +hint = { + 'final_hint': ['''\ +<p>Program je pravilen! <br> +</p> +'''], +} diff --git a/python/problems/functions/greatest/common.py b/python/problems/functions/greatest/common.py index 6857603..726fa88 100644 --- a/python/problems/functions/greatest/common.py +++ b/python/problems/functions/greatest/common.py @@ -6,7 +6,6 @@ from python.util import has_token_sequence, string_almost_equal, \ from server.hints import Hint id = 195 -group = 'functions' number = 1 visible = True @@ -32,6 +31,10 @@ hint_type = { } def test(python, code): + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', 'max_val']): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : 'max_val'}}] + test_lists = [[6, 4, 2, 0], [4, 6, 2, 0], [4, 2, 6, 0], diff --git a/python/problems/functions/greatest/en.py b/python/problems/functions/greatest/en.py index 8a0c883..acf4672 100644 --- a/python/problems/functions/greatest/en.py +++ b/python/problems/functions/greatest/en.py @@ -1,6 +1,5 @@ # coding=utf-8 -id = 195 name = 'Maximum number' slug = 'Maximum number' diff --git a/python/problems/functions/greatest/sl.py b/python/problems/functions/greatest/sl.py index a7916a5..12e07fe 100644 --- a/python/problems/functions/greatest/sl.py +++ b/python/problems/functions/greatest/sl.py @@ -3,7 +3,6 @@ import server mod = server.problems.load_language('python', 'sl') -id = 195 name = 'Največji' slug = 'Največji' diff --git a/python/problems/functions/greatest_absolutist/common.py b/python/problems/functions/greatest_absolutist/common.py index 2d80097..3226b87 100644 --- a/python/problems/functions/greatest_absolutist/common.py +++ b/python/problems/functions/greatest_absolutist/common.py @@ -6,7 +6,6 @@ from python.util import has_token_sequence, string_almost_equal, \ from server.hints import Hint id = 196 -group = 'functions' number = 2 visible = True @@ -35,6 +34,10 @@ hint_type = { def test(python, code): + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', 'max_abs']): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : 'max_abs'}}] + test_lists = [[6, 4, 2, 0], [4, 6, 2, 0], [4, 2, 6, 0], diff --git a/python/problems/functions/greatest_absolutist/en.py b/python/problems/functions/greatest_absolutist/en.py index 03614e7..1ede4a9 100644 --- a/python/problems/functions/greatest_absolutist/en.py +++ b/python/problems/functions/greatest_absolutist/en.py @@ -1,6 +1,5 @@ # coding=utf-8 -id = 196 name = 'Greatest absolutist' slug = 'Greatest absolutist' diff --git a/python/problems/functions/greatest_absolutist/sl.py b/python/problems/functions/greatest_absolutist/sl.py index d51cd96..a45f483 100644 --- a/python/problems/functions/greatest_absolutist/sl.py +++ b/python/problems/functions/greatest_absolutist/sl.py @@ -3,7 +3,6 @@ import server mod = server.problems.load_language('python', 'sl') -id = 196 name = 'Največji absolutist' slug = 'Največji absolutist' @@ -101,7 +100,7 @@ V tem primeru se lahko zgodi, da se zanka ne izteče do konca.</p>''', 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)) + return max(xs, key = abs(x)) </pre>''', } diff --git a/python/problems/functions/greatest_negative/common.py b/python/problems/functions/greatest_negative/common.py index 5d65438..72754e2 100644 --- a/python/problems/functions/greatest_negative/common.py +++ b/python/problems/functions/greatest_negative/common.py @@ -6,7 +6,6 @@ from python.util import has_token_sequence, string_almost_equal, \ from server.hints import Hint id = 197 -group = 'functions' number = 3 visible = True @@ -35,6 +34,10 @@ hint_type = { def test(python, code): + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', 'max_neg']): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : 'max_neg'}}] + test_lists = [[6, 4, 2, 0], [4, 6, 2, -1], [4, -2, -6, 0], diff --git a/python/problems/functions/greatest_negative/en.py b/python/problems/functions/greatest_negative/en.py index a57cb1f..5c2919a 100644 --- a/python/problems/functions/greatest_negative/en.py +++ b/python/problems/functions/greatest_negative/en.py @@ -1,6 +1,5 @@ # coding=utf-8 -id = 197 name = 'Greatest negativist' slug = 'Greatest negativist' diff --git a/python/problems/functions/greatest_negative/sl.py b/python/problems/functions/greatest_negative/sl.py index ea1e47c..31f1fd6 100644 --- a/python/problems/functions/greatest_negative/sl.py +++ b/python/problems/functions/greatest_negative/sl.py @@ -3,7 +3,6 @@ import server mod = server.problems.load_language('python', 'sl') -id = 197 name = 'Največji negativec' slug = 'Največji negativec' diff --git a/python/problems/functions/palindrome/common.py b/python/problems/functions/palindrome/common.py new file mode 100644 index 0000000..eec8211 --- /dev/null +++ b/python/problems/functions/palindrome/common.py @@ -0,0 +1,68 @@ +# 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 + +id = 228 +number = 7 +visible = True + +solution = '''\ +def palindrome(s): + return s == s[::-1] +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'palindrome' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + ('', True), + ('a', True), + ('aa', True), + ('ab', False), + ('aba', True), + ('abc', False), + ('abcdefedcba', True), + ('abcdefgedcba', False), + ('pericarezeracirep', True), + ('perica', False), + ] + + test_in = [(func_name+'("%s")'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + 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_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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 passed: + hints.append({'id': 'final_hint'}) + return passed, hints + +def hint(python, code): + tokens = get_tokens(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[0][3]) + if exc: return exc + + return None diff --git a/python/problems/functions/palindrome/en.py b/python/problems/functions/palindrome/en.py new file mode 100644 index 0000000..784c395 --- /dev/null +++ b/python/problems/functions/palindrome/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 228 +name = 'Palindrome' +slug = 'Palindrome' + +description = '''\ +<p>(translation missing)</p>''' + +hint = { + 'plan': '''\ +<p>(translation missing)</p>''', + + 'no_input_call': '''\ +<p>(translation missing)</p>''', +} diff --git a/python/problems/functions/palindrome/sl.py b/python/problems/functions/palindrome/sl.py new file mode 100644 index 0000000..e005057 --- /dev/null +++ b/python/problems/functions/palindrome/sl.py @@ -0,0 +1,33 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 228 +name = 'Palindrom' +slug = 'Palindrom' + + +description = '''\ +<p> +Napišite funkcijo <code>palindrome(s)</code>, ki preveri, ali je niz s palindrom. +<pre> +>>> palindrome('pericarezeracirep') +True +>>> palindrome('perica') +False +</pre> +</p>''' + +plan = ['''\ +<p></p> +''', + '''\ +<p></p>'''] + +hint = { + 'final_hint': ['''\ +<p>Program je pravilen! <br> +</p> +'''], +} diff --git a/python/problems/functions/palindromic_numbers/common.py b/python/problems/functions/palindromic_numbers/common.py new file mode 100644 index 0000000..6f49896 --- /dev/null +++ b/python/problems/functions/palindromic_numbers/common.py @@ -0,0 +1,70 @@ +# 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 + +id = 229 +number = 8 +visible = True + +solution = '''\ +def palindromic_numbers(): + xs = [] + for i in range(100, 1000): + for j in range(100, 1000): + if str(i * j) == str(i * j)[::-1]: + xs.append(i * j) + return max(xs) +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'numbers' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + (906609, True), + (123456, False), + (1, True), + (11, True), + (12, False), + (113, False), + (131, True) + ] + + test_in = [(func_name+'(%s)'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + 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_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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 passed: + hints.append({'id': 'final_hint'}) + return passed, hints + +def hint(python, code): + tokens = get_tokens(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[0][3]) + if exc: return exc + + return None diff --git a/python/problems/functions/palindromic_numbers/en.py b/python/problems/functions/palindromic_numbers/en.py new file mode 100644 index 0000000..5f4ea76 --- /dev/null +++ b/python/problems/functions/palindromic_numbers/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 229 +name = 'Palindromic numbers' +slug = 'Palindromic numbers' + +description = '''\ +<p>(translation missing)</p>''' + +hint = { + 'plan': '''\ +<p>(translation missing)</p>''', + + 'no_input_call': '''\ +<p>(translation missing)</p>''', +} diff --git a/python/problems/functions/palindromic_numbers/sl.py b/python/problems/functions/palindromic_numbers/sl.py new file mode 100644 index 0000000..8d64bab --- /dev/null +++ b/python/problems/functions/palindromic_numbers/sl.py @@ -0,0 +1,31 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 229 +name = 'Palindromska števila' +slug = 'Palindromska števila' + + +description = '''\ +<p> +Največje palindromsko število, ki ga lahko dobimo kot produkt dveh dvomestnih števil je <code>9009 = 91 * 99</code>. +Napišite funkcijo <code>palindromic_number()</code>, ki poišče in vrne največje palindromsko število, +ki ga lahko dobimo kot produkt dveh tromestnih števil. + +Vir: Project Euler, <a href="http://projecteuler.net/index.php?section=problems&id=4">Problem 4</a>. +</p>''' + +plan = ['''\ +<p></p> +''', + '''\ +<p></p>'''] + +hint = { + 'final_hint': ['''\ +<p>Program je pravilen! <br> +</p> +'''], +} diff --git a/python/problems/functions/perfect_numbers/common.py b/python/problems/functions/perfect_numbers/common.py new file mode 100644 index 0000000..72267e6 --- /dev/null +++ b/python/problems/functions/perfect_numbers/common.py @@ -0,0 +1,71 @@ +# 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 + +id = 232 +number = 11 +visible = True + +solution = '''\ +def divisors_sum(n): + s = 0 + for i in range(1, n): + if n % i == 0: + s += i + return s + +def perfect(n): + return n == divisors_sum(n) +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'perfect' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + (6, True), + (28, True), + (1, False), + (5, False), + (496, True), + (497, False), + ] + + test_in = [(func_name+'(%s)'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + 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_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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 passed: + hints.append({'id': 'final_hint'}) + return passed, hints + +def hint(python, code): + tokens = get_tokens(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[0][3]) + if exc: return exc + + return None diff --git a/python/problems/functions/perfect_numbers/en.py b/python/problems/functions/perfect_numbers/en.py new file mode 100644 index 0000000..a7cb94e --- /dev/null +++ b/python/problems/functions/perfect_numbers/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 232 +name = 'Perfect numbers' +slug = 'Perfect numbers' + +description = '''\ +<p>(translation missing)</p>''' + +hint = { + 'plan': '''\ +<p>(translation missing)</p>''', + + 'no_input_call': '''\ +<p>(translation missing)</p>''', +} diff --git a/python/problems/functions/perfect_numbers/sl.py b/python/problems/functions/perfect_numbers/sl.py new file mode 100644 index 0000000..a354a17 --- /dev/null +++ b/python/problems/functions/perfect_numbers/sl.py @@ -0,0 +1,30 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 232 +name = 'Popolna števila' +slug = 'Popolna števila' + + +description = '''\ +<p> +Napiši funkcijo <code>perfect(n)</code>, ki vrne <code>True</code>, če je podano število popolno, oz. <code>False</code>, če ni. +Popolna števila so števila, ki so enaka vsoti svojih deliteljev (brez samega sebe). +Primer popolnega števila je 28, saj so njegovi delitelji 1, 2, 4, 7, 14, njihova vsota, 1+2+4+7+14 pa je spet enaka 28. +</p> +<p> Uporabite funkcijo za vsoto deliteljev!</p>''' + +plan = ['''\ +<p></p> +''', + '''\ +<p></p>'''] + +hint = { + 'final_hint': ['''\ +<p>Program je pravilen! <br> +</p> +'''], +} diff --git a/python/problems/functions/prime_numbers/common.py b/python/problems/functions/prime_numbers/common.py new file mode 100644 index 0000000..33eb134 --- /dev/null +++ b/python/problems/functions/prime_numbers/common.py @@ -0,0 +1,69 @@ +# 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 + +id = 234 +number = 13 +visible = True + +solution = '''\ +def prime(n): + for i in range(2, n): + for j in range(2, i): + if i % j == 0: + break + else: + print(i) +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'prime' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + (3, [2]), + (10, [2, 3, 5, 7]), + (100, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]), + ] + + test_in = [(func_name+'(%s)'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + 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)): + numbers = sorted([int(n) for n in get_numbers(ans[1])]) + + if numbers == to: + n_correct += 1 + else: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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 passed: + hints.append({'id': 'final_hint'}) + return passed, hints + +def hint(python, code): + tokens = get_tokens(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[0][3]) + if exc: return exc + + return None diff --git a/python/problems/functions/prime_numbers/en.py b/python/problems/functions/prime_numbers/en.py new file mode 100644 index 0000000..87c01a2 --- /dev/null +++ b/python/problems/functions/prime_numbers/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 234 +name = 'Prime numbers' +slug = 'Prime numbers' + +description = '''\ +<p>(translation missing)</p>''' + +hint = { + 'plan': '''\ +<p>(translation missing)</p>''', + + 'no_input_call': '''\ +<p>(translation missing)</p>''', +} diff --git a/python/problems/functions/prime_numbers/sl.py b/python/problems/functions/prime_numbers/sl.py new file mode 100644 index 0000000..f6c6b24 --- /dev/null +++ b/python/problems/functions/prime_numbers/sl.py @@ -0,0 +1,27 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 234 +name = 'Praštevila' +slug = 'Praštevila' + + +description = '''\ +<p> +Napišite funkcijo <code>prime(n)</code>, ki izpiše vsa praštevila manjša od <code>n</code>. +</p>''' + +plan = ['''\ +<p></p> +''', + '''\ +<p></p>'''] + +hint = { + 'final_hint': ['''\ +<p>Program je pravilen! <br> +</p> +'''], +} |