From 73c22b77730970bcfa13eedec409c2477453dc5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matev=C5=BE=20Pober=C5=BEnik?= Date: Mon, 6 Nov 2017 17:15:27 +0100 Subject: FKKT: for loop group --- python/problems/for-fkkt/all_multiple/common.py | 88 ++++++++++++++++++ python/problems/for-fkkt/all_multiple/sl.py | 40 ++++++++ python/problems/for-fkkt/all_primes/common.py | 83 +++++++++++++++++ python/problems/for-fkkt/all_primes/sl.py | 34 +++++++ python/problems/for-fkkt/any_multiple/common.py | 88 ++++++++++++++++++ python/problems/for-fkkt/any_multiple/sl.py | 39 ++++++++ python/problems/for-fkkt/common.py | 2 + python/problems/for-fkkt/en.py | 3 + python/problems/for-fkkt/is_prime/common.py | 82 +++++++++++++++++ python/problems/for-fkkt/is_prime/sl.py | 35 +++++++ python/problems/for-fkkt/min/common.py | 101 +++++++++++++++++++++ python/problems/for-fkkt/min/sl.py | 62 +++++++++++++ python/problems/for-fkkt/sl.py | 3 + python/problems/for-fkkt/star_tree/common.py | 73 +++++++++++++++ python/problems/for-fkkt/star_tree/sl.py | 34 +++++++ python/problems/for-fkkt/star_triangle/common.py | 73 +++++++++++++++ python/problems/for-fkkt/star_triangle/sl.py | 34 +++++++ python/problems/for-fkkt/sum_and_average/common.py | 84 +++++++++++++++++ python/problems/for-fkkt/sum_and_average/sl.py | 33 +++++++ python/problems/for-fkkt/sum_to_n/common.py | 74 +++++++++++++++ python/problems/for-fkkt/sum_to_n/sl.py | 31 +++++++ 21 files changed, 1096 insertions(+) create mode 100644 python/problems/for-fkkt/all_multiple/common.py create mode 100644 python/problems/for-fkkt/all_multiple/sl.py create mode 100644 python/problems/for-fkkt/all_primes/common.py create mode 100644 python/problems/for-fkkt/all_primes/sl.py create mode 100644 python/problems/for-fkkt/any_multiple/common.py create mode 100644 python/problems/for-fkkt/any_multiple/sl.py create mode 100644 python/problems/for-fkkt/common.py create mode 100644 python/problems/for-fkkt/en.py create mode 100644 python/problems/for-fkkt/is_prime/common.py create mode 100644 python/problems/for-fkkt/is_prime/sl.py create mode 100644 python/problems/for-fkkt/min/common.py create mode 100644 python/problems/for-fkkt/min/sl.py create mode 100644 python/problems/for-fkkt/sl.py create mode 100644 python/problems/for-fkkt/star_tree/common.py create mode 100644 python/problems/for-fkkt/star_tree/sl.py create mode 100644 python/problems/for-fkkt/star_triangle/common.py create mode 100644 python/problems/for-fkkt/star_triangle/sl.py create mode 100644 python/problems/for-fkkt/sum_and_average/common.py create mode 100644 python/problems/for-fkkt/sum_and_average/sl.py create mode 100644 python/problems/for-fkkt/sum_to_n/common.py create mode 100644 python/problems/for-fkkt/sum_to_n/sl.py (limited to 'python/problems/for-fkkt') diff --git a/python/problems/for-fkkt/all_multiple/common.py b/python/problems/for-fkkt/all_multiple/common.py new file mode 100644 index 0000000..18087dc --- /dev/null +++ b/python/problems/for-fkkt/all_multiple/common.py @@ -0,0 +1,88 @@ +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 = 25016 +number = 4 +visible = True + +solution = '''\ +seznam = eval(input("Vpišite seznam števil: ")) +st = int(input("Vnesite število: ")) + +samo_veckratniki = True +for elt in seznam: + if elt % st != 0: + samo_veckratniki = False + break +if samo_veckratniki: + print("Vsebuje.") +else: + print("Ne vsebuje.") +''' + +hint_type = { + 'no_input_call' : Hint('no_input_call'), + 'no_eval_call' : Hint('no_eval_call'), + 'printing': Hint('printing'), + 'final_hint': Hint('final_hint') +} + +def test(python, code, aux_code=''): + tokens = get_tokens(code) + + test_in = [ + (None, '[]\n42\n'), + (None, '[3, 0]\n3\n'), + (None, '[27, 21, 3, 33, 60, -3]\n3\n'), + (None, '[27, 21, 3, 33, 60, -3]\n9\n') + ] + test_out = [ + 'Vsebuje samo večkratnike', + 'Vsebuje samo večkratnike', + 'Vsebuje samo večkratnike', + 'Ne vsebuje samo večkratnikov' + ] + + # List of outputs: (expression result, stdout, stderr, exception). + answers = python(code=aux_code+code, inputs=test_in, timeout=1.0) + outputs = [ans[1] for ans in answers] + + n_correct = 0 + tin = None + for i, (output, correct) in enumerate(zip(outputs, test_out)): + if "Ne vsebuje" in correct and "ne vsebuje" in output.lower() \ + or "Ne vsebuje" not in correct and "ne vsebuje" not in output.lower() and "vsebuje" in output.lower(): + n_correct += 1 + else: + tin = test_in[i][1] + tout = correct + + passed = n_correct == len(test_in) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}] + if tin != None: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) + else: + hints.append({'id' : 'final_hint'}) + return passed, hints + +def hint(python, code, aux_code=''): + # run one test first to see if there are any exceptions + answer = python(code=aux_code+code, inputs=[(None, '[12]\n12\n')], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + tokens = get_tokens(code) + + if not has_token_sequence(tokens, ['input']): + return [{'id': 'no_input_call'}] + + if not has_token_sequence(tokens, ['eval']): + return [{'id': 'no_eval_call'}] + + # student is not using print function + if not has_token_sequence(tokens, ['print']): + return [{'id' : 'printing'}] + + return None diff --git a/python/problems/for-fkkt/all_multiple/sl.py b/python/problems/for-fkkt/all_multiple/sl.py new file mode 100644 index 0000000..8aa2333 --- /dev/null +++ b/python/problems/for-fkkt/all_multiple/sl.py @@ -0,0 +1,40 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +name = 'Samo večkratniki' +slug = 'Samo večkratniki' + + +description = '''\ +

Napišite program, ki preveri, če seznam vsebuje le večkratnike števila, ki ga vnese uporabnik. Poskusite uporabiti tudi break.
+Seznam števil naj vpiše uporabnik. Za pretvorbo vhodnega niza v seznam uporabite funkcijo eval().
+1. primer uporabe:

+ +
Vpišite seznam števil: [27, 21, 3, 33, 60, -3]
+Vnesite število: 3
+Vsebuje samo večkratnike.
+
+ +

2. primer uporabe:

+
Vpišite seznam števil: [27, 21, 3, 33, 60, -3]
+Vnesite število: 9
+Ne vsebuje samo večkratnikov.
+
+''' + + +plan = [] + +hint = { + 'no_input_call': '''

Za branje uporabi funkcijo input

''', + + 'no_eval_call': '''

Za pretvorbo niza v seznam uporabi funkcijo eval

''', + + 'printing': ['''\ +

Izpiši rezultat.

'''], + + 'final_hint': ['''\ +

Program deluje pravilno!

'''], + +} diff --git a/python/problems/for-fkkt/all_primes/common.py b/python/problems/for-fkkt/all_primes/common.py new file mode 100644 index 0000000..3c121d8 --- /dev/null +++ b/python/problems/for-fkkt/all_primes/common.py @@ -0,0 +1,83 @@ +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 = 25021 +number = 9 +visible = True + +solution = '''\ +n = int(input("Vpiši število: ")) +stevec = 0 +for i in range(2, n + 1): + prastevilo = True + for j in range(2, int(i**(1/2))+1): + if i % j == 0: + prastevilo = False + break + if prastevilo: + stevec += 1 +print(stevec) + +''' + +hint_type = { + 'no_input_call' : Hint('no_input_call'), + 'printing': Hint('printing'), + 'final_hint': Hint('final_hint') +} + +def test(python, code, aux_code=''): + tokens = get_tokens(code) + + test_in = [ + (None, '2\n'), + (None, '3\n'), + (None, '7\n'), + (None, '12\n') + ] + test_out = [ + '0', + '1', + '3', + '5' + ] + + # List of outputs: (expression result, stdout, stderr, exception). + answers = python(code=aux_code+code, inputs=test_in, timeout=1.0) + outputs = [ans[1] for ans in answers] + + n_correct = 0 + tin = None + for i, (output, correct) in enumerate(zip(outputs, test_out)): + if output.rstrip().endswith(correct): + n_correct += 1 + else: + tin = test_in[i][1] + tout = correct + + passed = n_correct == len(test_in) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}] + if tin != None: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) + else: + hints.append({'id' : 'final_hint'}) + return passed, hints + +def hint(python, code, aux_code=''): + # run one test first to see if there are any exceptions + answer = python(code=aux_code+code, inputs=[(None, '2\n')], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + tokens = get_tokens(code) + + if not has_token_sequence(tokens, ['input']): + return [{'id': 'no_input_call'}] + + # student is not using print function + if not has_token_sequence(tokens, ['print']): + return [{'id' : 'printing'}] + + return None diff --git a/python/problems/for-fkkt/all_primes/sl.py b/python/problems/for-fkkt/all_primes/sl.py new file mode 100644 index 0000000..b86dbae --- /dev/null +++ b/python/problems/for-fkkt/all_primes/sl.py @@ -0,0 +1,34 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +name = 'Praštevila' +slug = 'Praštevila' + + +description = '''\ +

Napišite program, ki izpiše, koliko praštevil je manjših od števila n. +
+Primer uporabe:

+ +
Vpiši število: 12
+5
+
+ + +

Če si bo program za število 50000 vzel preveč časa (beri: se ne bo izvedel dovolj hitro), poskusi optimizirati rešitev. Za še večja števila (npr. 1000000) se pa poskusi poigrati z Eratostenovim sitom.

+''' + + +plan = [] + +hint = { + 'no_input_call': '''

Za branje uporabi funkcijo input

''', + + 'printing': ['''\ +

Izpiši rezultat.

'''], + + 'final_hint': ['''\ +

Program deluje pravilno!

'''], + +} diff --git a/python/problems/for-fkkt/any_multiple/common.py b/python/problems/for-fkkt/any_multiple/common.py new file mode 100644 index 0000000..7ebf397 --- /dev/null +++ b/python/problems/for-fkkt/any_multiple/common.py @@ -0,0 +1,88 @@ +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 = 25015 +number = 3 +visible = True + +solution = '''\ +seznam = eval(input("Vpišite seznam števil: ")) +st = int(input("Vnesite število: ")) + +vsebuje = False +for elt in seznam: + if elt % st == 0: + vsebuje = True + break +if vsebuje: + print("Vsebuje.") +else: + print("Ne vsebuje.") +''' + +hint_type = { + 'no_input_call' : Hint('no_input_call'), + 'no_eval_call' : Hint('no_eval_call'), + 'printing': Hint('printing'), + 'final_hint': Hint('final_hint') +} + +def test(python, code, aux_code=''): + tokens = get_tokens(code) + + test_in = [ + (None, '[]\n42\n'), + (None, '[3, 0]\n3\n'), + (None, '[23, 42, 87, 34, 1, -3, 2]\n3\n'), + (None, '[23, 42, 87, 34, 1, -3, 2]\n8\n') + ] + test_out = [ + 'Ne vsebuje', + 'Vsebuje', + 'Vsebuje', + 'Ne vsebuje' + ] + + # List of outputs: (expression result, stdout, stderr, exception). + answers = python(code=aux_code+code, inputs=test_in, timeout=1.0) + outputs = [ans[1] for ans in answers] + + n_correct = 0 + tin = None + for i, (output, correct) in enumerate(zip(outputs, test_out)): + if "Ne vsebuje" in correct and "ne vsebuje" in output.lower() \ + or "Ne vsebuje" not in correct and "ne vsebuje" not in output.lower() and "vsebuje" in output.lower(): + n_correct += 1 + else: + tin = test_in[i][1] + tout = correct + + passed = n_correct == len(test_in) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}] + if tin != None: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) + else: + hints.append({'id' : 'final_hint'}) + return passed, hints + +def hint(python, code, aux_code=''): + # run one test first to see if there are any exceptions + answer = python(code=aux_code+code, inputs=[(None, '[12]\n12\n')], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + tokens = get_tokens(code) + + if not has_token_sequence(tokens, ['input']): + return [{'id': 'no_input_call'}] + + if not has_token_sequence(tokens, ['eval']): + return [{'id': 'no_eval_call'}] + + # student is not using print function + if not has_token_sequence(tokens, ['print']): + return [{'id' : 'printing'}] + + return None diff --git a/python/problems/for-fkkt/any_multiple/sl.py b/python/problems/for-fkkt/any_multiple/sl.py new file mode 100644 index 0000000..ba24187 --- /dev/null +++ b/python/problems/for-fkkt/any_multiple/sl.py @@ -0,0 +1,39 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +name = 'Iskanje večkratnikov' +slug = 'Iskanje večkratnikov' + + +description = '''\ +

Napišite program, ki preveri, če seznam vsebuje vsaj en večkratnik števila, ki ga vnese uporabnik. Poskusite uporabiti tudi break.
+Seznam števil naj vpiše uporabnik. Za pretvorbo vhodnega niza v seznam uporabite funkcijo eval().
+1. primer uporabe:

+ +
Vpišite seznam števil: [23, 42, 87, 34, 1, -3, 2]
+Vnesite število: 3
+Vsebuje.
+ +

2. primer uporabe:

+
Vpišite seznam števil: [23, 42, 87, 34, 1, -3, 2]
+Vnesite število: 8
+Ne vsebuje.
+
+''' + + +plan = [] + +hint = { + 'no_input_call': '''

Za branje uporabi funkcijo input

''', + + 'no_eval_call': '''

Za pretvorbo niza v seznam uporabi funkcijo eval

''', + + 'printing': ['''\ +

Izpiši rezultat.

'''], + + 'final_hint': ['''\ +

Program deluje pravilno!

'''], + +} diff --git a/python/problems/for-fkkt/common.py b/python/problems/for-fkkt/common.py new file mode 100644 index 0000000..ab348a2 --- /dev/null +++ b/python/problems/for-fkkt/common.py @@ -0,0 +1,2 @@ +id = 2502 +number = 12 diff --git a/python/problems/for-fkkt/en.py b/python/problems/for-fkkt/en.py new file mode 100644 index 0000000..5757341 --- /dev/null +++ b/python/problems/for-fkkt/en.py @@ -0,0 +1,3 @@ +name = 'For loop - FKKT' +description = 'For loop' + diff --git a/python/problems/for-fkkt/is_prime/common.py b/python/problems/for-fkkt/is_prime/common.py new file mode 100644 index 0000000..1b80247 --- /dev/null +++ b/python/problems/for-fkkt/is_prime/common.py @@ -0,0 +1,82 @@ +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 = 25020 +number = 8 +visible = True + +solution = '''\ +n = int(input("Vpiši število: ")) + +if n == 1: + print(False) +else: + prastevilo = True + for j in range(2, n): + if n % j == 0: + prastevilo = False + break + print(prastevilo) +''' + +hint_type = { + 'no_input_call' : Hint('no_input_call'), + 'printing': Hint('printing'), + 'final_hint': Hint('final_hint') +} + +def test(python, code, aux_code=''): + tokens = get_tokens(code) + + test_in = [ + (None, '1\n'), + (None, '191\n'), + (None, '22\n'), + (None, '13\n') + ] + test_out = [ + 'False', + 'True', + 'False', + 'True' + ] + + # List of outputs: (expression result, stdout, stderr, exception). + answers = python(code=aux_code+code, inputs=test_in, timeout=1.0) + outputs = [ans[1] for ans in answers] + + n_correct = 0 + tin = None + for i, (output, correct) in enumerate(zip(outputs, test_out)): + if output.rstrip().endswith(correct): + n_correct += 1 + else: + tin = test_in[i][1] + tout = correct + + passed = n_correct == len(test_in) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}] + if tin != None: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) + else: + hints.append({'id' : 'final_hint'}) + return passed, hints + +def hint(python, code, aux_code=''): + # run one test first to see if there are any exceptions + answer = python(code=aux_code+code, inputs=[(None, '2\n')], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + tokens = get_tokens(code) + + if not has_token_sequence(tokens, ['input']): + return [{'id': 'no_input_call'}] + + # student is not using print function + if not has_token_sequence(tokens, ['print']): + return [{'id' : 'printing'}] + + return None diff --git a/python/problems/for-fkkt/is_prime/sl.py b/python/problems/for-fkkt/is_prime/sl.py new file mode 100644 index 0000000..34316be --- /dev/null +++ b/python/problems/for-fkkt/is_prime/sl.py @@ -0,0 +1,35 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +name = 'Praštevilo' +slug = 'Praštevilo' + + +description = '''\ +

Napišite program, ki za vnešeno število preveri, ali je praštevilo. +
+1. primer uporabe:

+
Vpiši število: 13
+True
+
+ +

2. primer uporabe:

+
Vpiši število: 22
+False
+
+''' + + +plan = [] + +hint = { + 'no_input_call': '''

Za branje uporabi funkcijo input

''', + + 'printing': ['''\ +

Izpiši rezultat.

'''], + + 'final_hint': ['''\ +

Program deluje pravilno!

'''], + +} diff --git a/python/problems/for-fkkt/min/common.py b/python/problems/for-fkkt/min/common.py new file mode 100644 index 0000000..ede7619 --- /dev/null +++ b/python/problems/for-fkkt/min/common.py @@ -0,0 +1,101 @@ +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 = 25013 +number = 1 +visible = True + +solution = '''\ +seznam = [23, 42, 87, 34, 1, -3, 2] +minimum = seznam[0] +for elt in seznam: + if elt < minimum: + minimum = elt +print(minimum) +''' + +hint_type = { + 'no_xs': Hint('no_xs'), + 'for_loop': Hint('for_loop'), + 'if_clause': Hint('if_clause'), + 'printing': Hint('printing'), + 'no_min': Hint('no_min'), + 'final_hint': Hint('final_hint') +} + +def test(python, code, aux_code=''): + tokens = get_tokens(code) + + if has_token_sequence(tokens, ['min', '(']): + return False, [{'id': 'no_min'}] + + test_xs = [[5, 4, -7, 2, 12, -3, -4, 11, 2], + [0], + [3, 1, 2, 8], + [1, 2, 3, 42], + [23, 42, 87, 34, 1, -3, 2]] + test_out = [ + '-7', + '0', + '1', + '1', + '-3' + ] + + n_correct = 0 + tin = None + 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'^seznam\s*=\s*\[.*?\]', + 'seznam = ' + str(xs), + code, + flags = re.DOTALL | re.MULTILINE) + + # use python session to call tcode + answers = python(code=aux_code+tcode, inputs=[(None, None)], timeout=1.0) + output = answers[0][1] + + if output.rstrip().endswith(test_out[xs_i]): + n_correct += 1 + else: + tin = test_xs[xs_i] + tout = test_out[xs_i] + + passed = n_correct == len(test_xs) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_xs)}}] + if tin != None: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) + else: + hints.append({'id' : 'final_hint'}) + return passed, hints + +def hint(python, code, aux_code=''): + # run one test first to see if there are any exceptions + answer = python(code=aux_code+code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + tokens = get_tokens(code) + + # if has no xs, tell him to ask for values + if not has_token_sequence(tokens, ['seznam', '=', '[']): + return [{'id' : 'no_xs'}] + + # student does not have for: instruct him on loops + if not has_token_sequence(tokens, ['for']): + return [{'id' : 'for_loop'}] + + if has_token_sequence(tokens, ['min', '(']): + return [{'id': 'no_min'}] + + 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'}] + + return None diff --git a/python/problems/for-fkkt/min/sl.py b/python/problems/for-fkkt/min/sl.py new file mode 100644 index 0000000..58b5e11 --- /dev/null +++ b/python/problems/for-fkkt/min/sl.py @@ -0,0 +1,62 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +name = 'Najmanjše število v seznamu' +slug = 'Najmanjše število v seznamu' + + +description = '''\ +

Napišite program, ki poišče in izpiše najmanjši element v seznamu brez uporabe funkcije min()!
+Seznam definirajte na vrhu programa seznam = [23, 42, 87, 34, 1, -3, 2]
+Izpis:

+ +
-3
+ +

Seveda mora program delovati tudi za vse druge sezname!

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

Uporabi pogojni stavek if!

''', + '''\ +
+if elt < minimum:
+
'''] + +for_loop = ['''\ +

Pregledati bo treba vse elemente v seznamu seznam''', + '''\ +

Najlažje bo s for zanko. +''', + '''\ +

Poskusi naslednji dve vrstici:

+
+for elt in seznam:
+    print (elt)
+
''', + '''\ +

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

'''] + + +plan = [] + +hint = { + 'no_xs': ['''\ +

Program mora imeti na začetku definiran seznam seznam.

'''], + + 'no_min': '''Funkcija min() ni dovoljena pri tej nalogi!''', + + 'for_loop': for_loop, + + 'if_clause': if_clause, + + 'printing': ['''\ +

Izpiši rezultat.

'''], + + 'final_hint': ['''\ +

Program deluje pravilno!

'''], + +} diff --git a/python/problems/for-fkkt/sl.py b/python/problems/for-fkkt/sl.py new file mode 100644 index 0000000..7a277c0 --- /dev/null +++ b/python/problems/for-fkkt/sl.py @@ -0,0 +1,3 @@ +name = 'For zanka - FKKT' +description = 'Naloge s for zanko' + diff --git a/python/problems/for-fkkt/star_tree/common.py b/python/problems/for-fkkt/star_tree/common.py new file mode 100644 index 0000000..607e0a9 --- /dev/null +++ b/python/problems/for-fkkt/star_tree/common.py @@ -0,0 +1,73 @@ +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 = 25019 +number = 7 +visible = True + +solution = '''\ +n = int(input('Vpiši višino: ')) + +for i in range(1, n + 1): + print(' ' * (n - i) + '*' * (2 * i - 1)) +''' + +hint_type = { + 'no_input_call' : Hint('no_input_call'), + 'printing': Hint('printing'), + 'final_hint': Hint('final_hint') +} + +def test(python, code, aux_code=''): + tokens = get_tokens(code) + + test_in = [ + (None, '1\n'), + (None, '3\n'), + (None, '4\n') + ] + test_out = [ + '*', + ' *\n ***\n*****', + ' *\n ***\n *****\n*******', + ] + + # List of outputs: (expression result, stdout, stderr, exception). + answers = python(code=aux_code+code, inputs=test_in, timeout=1.0) + outputs = [ans[1] for ans in answers] + + n_correct = 0 + tin = None + for i, (output, correct) in enumerate(zip(outputs, test_out)): + if output.rstrip().endswith(correct): + n_correct += 1 + else: + tin = test_in[i][1] + tout = correct + + passed = n_correct == len(test_in) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}] + if tin != None: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) + else: + hints.append({'id' : 'final_hint'}) + return passed, hints + +def hint(python, code, aux_code=''): + # run one test first to see if there are any exceptions + answer = python(code=aux_code+code, inputs=[(None, '2\n')], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + tokens = get_tokens(code) + + if not has_token_sequence(tokens, ['input']): + return [{'id': 'no_input_call'}] + + # student is not using print function + if not has_token_sequence(tokens, ['print']): + return [{'id' : 'printing'}] + + return None diff --git a/python/problems/for-fkkt/star_tree/sl.py b/python/problems/for-fkkt/star_tree/sl.py new file mode 100644 index 0000000..484632c --- /dev/null +++ b/python/problems/for-fkkt/star_tree/sl.py @@ -0,0 +1,34 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +name = 'Izris smrekice' +slug = 'Izris smrekice' + + +description = '''\ +

Napišite program, ki bo namesto trikotnikov izrisoval smrekice. +
+Primer uporabe:

+ +
Vpiši višino: 4
+   *
+  ***
+ *****
+*******
+
+''' + + +plan = [] + +hint = { + 'no_input_call': '''

Za branje uporabi funkcijo input

''', + + 'printing': ['''\ +

Izpiši rezultat.

'''], + + 'final_hint': ['''\ +

Program deluje pravilno!

'''], + +} diff --git a/python/problems/for-fkkt/star_triangle/common.py b/python/problems/for-fkkt/star_triangle/common.py new file mode 100644 index 0000000..ff4c5a4 --- /dev/null +++ b/python/problems/for-fkkt/star_triangle/common.py @@ -0,0 +1,73 @@ +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 = 25018 +number = 6 +visible = True + +solution = '''\ +n = int(input('Vpiši višino: ')) + +for i in range(1, n + 1): + print('*' * i) +''' + +hint_type = { + 'no_input_call' : Hint('no_input_call'), + 'printing': Hint('printing'), + 'final_hint': Hint('final_hint') +} + +def test(python, code, aux_code=''): + tokens = get_tokens(code) + + test_in = [ + (None, '1\n'), + (None, '3\n'), + (None, '4\n') + ] + test_out = [ + '*', + '*\n**\n***', + '*\n**\n***\n****', + ] + + # List of outputs: (expression result, stdout, stderr, exception). + answers = python(code=aux_code+code, inputs=test_in, timeout=1.0) + outputs = [ans[1] for ans in answers] + + n_correct = 0 + tin = None + for i, (output, correct) in enumerate(zip(outputs, test_out)): + if output.rstrip().endswith(correct): + n_correct += 1 + else: + tin = test_in[i][1] + tout = correct + + passed = n_correct == len(test_in) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}] + if tin != None: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) + else: + hints.append({'id' : 'final_hint'}) + return passed, hints + +def hint(python, code, aux_code=''): + # run one test first to see if there are any exceptions + answer = python(code=aux_code+code, inputs=[(None, '2\n')], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + tokens = get_tokens(code) + + if not has_token_sequence(tokens, ['input']): + return [{'id': 'no_input_call'}] + + # student is not using print function + if not has_token_sequence(tokens, ['print']): + return [{'id' : 'printing'}] + + return None diff --git a/python/problems/for-fkkt/star_triangle/sl.py b/python/problems/for-fkkt/star_triangle/sl.py new file mode 100644 index 0000000..9ed5a5e --- /dev/null +++ b/python/problems/for-fkkt/star_triangle/sl.py @@ -0,0 +1,34 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +name = 'Izris trikotnika' +slug = 'Izris trikotnika' + + +description = '''\ +

Po programerski tradiciji eden prvih programov, ki jih napišemo v določenem programskem jeziku, nariše trikotnik iz zvezdic. Napišite program, ki vpraša uporabnika po višini trikotnika, nato pa izpiše takšen trikotnik iz zvezdic. +
+Primer uporabe:

+ +
Vpiši višino: 4
+*
+**
+***
+****
+
+''' + + +plan = [] + +hint = { + 'no_input_call': '''

Za branje uporabi funkcijo input

''', + + 'printing': ['''\ +

Izpiši rezultat.

'''], + + 'final_hint': ['''\ +

Program deluje pravilno!

'''], + +} diff --git a/python/problems/for-fkkt/sum_and_average/common.py b/python/problems/for-fkkt/sum_and_average/common.py new file mode 100644 index 0000000..44815ec --- /dev/null +++ b/python/problems/for-fkkt/sum_and_average/common.py @@ -0,0 +1,84 @@ +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 = 25014 +number = 2 +visible = True + +solution = '''\ +seznam = eval(input("Vpišite seznam števil: ")) +vsota = 0 +for elt in seznam: + vsota += elt +print(vsota) +if len(seznam) != 0: + print(vsota/len(seznam)) +else: + print(0) +''' + +hint_type = { + 'no_input_call' : Hint('no_input_call'), + 'no_eval_call' : Hint('no_eval_call'), + 'printing': Hint('printing'), + 'final_hint': Hint('final_hint') +} + +def test(python, code, aux_code=''): + tokens = get_tokens(code) + + test_in = [ + (None, '[]\n'), + (None, '[-1, -2, -3]\n'), + (None, '[2, 4, 6, 8, 10]\n'), + (None, '[23, 42, 87, 34, 1, -3, 2]\n') + ] + test_out = [ + '0\n0', + '-6\n-2.0', + '30\n6.0', + '186\n26.571428571428573' + ] + + # List of outputs: (expression result, stdout, stderr, exception). + answers = python(code=aux_code+code, inputs=test_in, timeout=1.0) + outputs = [ans[1] for ans in answers] + + n_correct = 0 + tin = None + for i, (output, correct) in enumerate(zip(outputs, test_out)): + if output.rstrip().endswith(correct): + n_correct += 1 + else: + tin = test_in[i][1] + tout = correct + + passed = n_correct == len(test_in) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}] + if tin != None: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) + else: + hints.append({'id' : 'final_hint'}) + return passed, hints + +def hint(python, code, aux_code=''): + # run one test first to see if there are any exceptions + answer = python(code=aux_code+code, inputs=[(None, '[-1, -2, -3]\n')], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + tokens = get_tokens(code) + + if not has_token_sequence(tokens, ['input']): + return [{'id': 'no_input_call'}] + + if not has_token_sequence(tokens, ['eval']): + return [{'id': 'no_eval_call'}] + + # student is not using print function + if not has_token_sequence(tokens, ['print']): + return [{'id' : 'printing'}] + + return None diff --git a/python/problems/for-fkkt/sum_and_average/sl.py b/python/problems/for-fkkt/sum_and_average/sl.py new file mode 100644 index 0000000..4ed8b69 --- /dev/null +++ b/python/problems/for-fkkt/sum_and_average/sl.py @@ -0,0 +1,33 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +name = 'Vsota in povprečje' +slug = 'Vsota in povprečje' + + +description = '''\ +

Napišite program, ki za podani seznam izračuna vsoto in povprečje elementov.
+Seznam števil naj vpiše uporabnik. Za pretvorbo vhodnega niza v seznam uporabite funkcijo eval().
Primer uporabe:

+ +
Vpišite seznam števil: [23, 42, 87, 34, 1, -3, 2]
+186
+26.571428571428573
+
+''' + + +plan = [] + +hint = { + 'no_input_call': '''

Za branje uporabi funkcijo input

''', + + 'no_eval_call': '''

Za pretvorbo niza v seznam uporabi funkcijo eval

''', + + 'printing': ['''\ +

Izpiši rezultat.

'''], + + 'final_hint': ['''\ +

Program deluje pravilno!

'''], + +} diff --git a/python/problems/for-fkkt/sum_to_n/common.py b/python/problems/for-fkkt/sum_to_n/common.py new file mode 100644 index 0000000..d61612e --- /dev/null +++ b/python/problems/for-fkkt/sum_to_n/common.py @@ -0,0 +1,74 @@ +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 = 25017 +number = 5 +visible = True + +solution = '''\ +st = int(input("Vpiši število: ")) +vsota = 0 +for x in range(1, st + 1): + vsota += x +print(vsota) +''' + +hint_type = { + 'no_input_call' : Hint('no_input_call'), + 'printing': Hint('printing'), + 'final_hint': Hint('final_hint') +} + +def test(python, code, aux_code=''): + tokens = get_tokens(code) + + test_in = [ + (None, '0\n'), + (None, '120\n'), + (None, '7\n') + ] + test_out = [ + '0', + '7260', + '28' + ] + + # List of outputs: (expression result, stdout, stderr, exception). + answers = python(code=aux_code+code, inputs=test_in, timeout=1.0) + outputs = [ans[1] for ans in answers] + + n_correct = 0 + tin = None + for i, (output, correct) in enumerate(zip(outputs, test_out)): + if output.rstrip().endswith(correct): + n_correct += 1 + else: + tin = test_in[i][1] + tout = correct + + passed = n_correct == len(test_in) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}] + if tin != None: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) + else: + hints.append({'id' : 'final_hint'}) + return passed, hints + +def hint(python, code, aux_code=''): + # run one test first to see if there are any exceptions + answer = python(code=aux_code+code, inputs=[(None, '12\n')], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + tokens = get_tokens(code) + + if not has_token_sequence(tokens, ['input']): + return [{'id': 'no_input_call'}] + + # student is not using print function + if not has_token_sequence(tokens, ['print']): + return [{'id' : 'printing'}] + + return None diff --git a/python/problems/for-fkkt/sum_to_n/sl.py b/python/problems/for-fkkt/sum_to_n/sl.py new file mode 100644 index 0000000..c7c11de --- /dev/null +++ b/python/problems/for-fkkt/sum_to_n/sl.py @@ -0,0 +1,31 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +name = 'Vsota n naravnih števil' +slug = 'Vsota n naravnih števil' + + +description = '''\ +

Napišite program, ki izračuna vsoto prvih n naravnih števil. Število n poda uporabnik. Če uporabnik vpiše 7, mora program izpisati 28, saj je 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. +
+Primer uporabe:

+ +
Vpiši število: 7
+28
+
+''' + + +plan = [] + +hint = { + 'no_input_call': '''

Za branje uporabi funkcijo input

''', + + 'printing': ['''\ +

Izpiši rezultat.

'''], + + 'final_hint': ['''\ +

Program deluje pravilno!

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