From 4841519d3fb3a76b37529705b4a1ca96fbb6d6f6 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 22 Sep 2015 12:55:35 +0200 Subject: Added two problems with functions. Their description is not finished yet. --- python/problems/functions/greatest/common.py | 73 ++++++++++++++ python/problems/functions/greatest/en.py | 16 +++ python/problems/functions/greatest/sl.py | 107 +++++++++++++++++++++ .../functions/greatest_absolutist/common.py | 98 +++++++++++++++++++ .../problems/functions/greatest_absolutist/en.py | 16 +++ .../problems/functions/greatest_absolutist/sl.py | 100 +++++++++++++++++++ 6 files changed, 410 insertions(+) create mode 100644 python/problems/functions/greatest/common.py create mode 100644 python/problems/functions/greatest/en.py create mode 100644 python/problems/functions/greatest/sl.py create mode 100644 python/problems/functions/greatest_absolutist/common.py create mode 100644 python/problems/functions/greatest_absolutist/en.py create mode 100644 python/problems/functions/greatest_absolutist/sl.py diff --git a/python/problems/functions/greatest/common.py b/python/problems/functions/greatest/common.py new file mode 100644 index 0000000..91f773e --- /dev/null +++ b/python/problems/functions/greatest/common.py @@ -0,0 +1,73 @@ +# 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 = 195 +group = 'functions' +number = 1 +visible = True + +solution = '''\ +def naj(xs): + naj = xs[0] + for x in xs: + if x > naj: + naj = x + return naj +''' + +hint_type = { + 'no_def': Hint('no_def'), + 'no_return': Hint('no_return'), + 'for_loop': Hint('for_loop'), + 'if_clause': Hint('if_clause'), +} + +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_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) + +def hint(python, code): + # 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) + 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'}] + + # 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'}] + + return None diff --git a/python/problems/functions/greatest/en.py b/python/problems/functions/greatest/en.py new file mode 100644 index 0000000..004cd27 --- /dev/null +++ b/python/problems/functions/greatest/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 195 +name = '(translation missing)' +slug = '(translation missing)' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/functions/greatest/sl.py b/python/problems/functions/greatest/sl.py new file mode 100644 index 0000000..0321d23 --- /dev/null +++ b/python/problems/functions/greatest/sl.py @@ -0,0 +1,107 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 195 +name = 'Največji' +slug = 'Največji' + + +description = '''\ +

Napišite funkcijo naj(xs), ki vrne največje število v seznamu xs. +

+>>> naj([5, 1, -6, -7, 2])
+5
+

+''' + +function = ['''\ +

Napišite definicijo funckcije naj(xs)

''', + '''\ +

Definicijo funkcije začnemo z def, temu sledi ime, potem oklepaji, +v katerih naštejemo argumente funkcije, nato zaklepaj in na koncu dvopičje

''', + '''\ +
+def naj(xs):
+    # program, ki poišče največjega v xs
+
'''] + +main_plan = ['''\ +

Ideja: po vrsti pogledamo vse elemente in sproti hranimo največjega.

''', + '''\ +
    +
  1. Začnemo s prvim elementom in si ga zapomnimo kot največjega.
  2. +
  3. Potem pogledamo drugi element, če je večji od največjega, ta postane največji.
  4. +
  5. Pogledamo tretji element, če je večji od največjega, postane največji.
  6. +
  7. Pogledamo četrti element, ...
  8. +
  9. ...
  10. +
  11. Pogledamo zadnji element, ... +
''', + '''\ +

Potrebovali bomo zanko!

+
+najvecji = prvi element od xs
+for x in xs:
+    ce je x > najvecji:
+        najvecji postane x
+izpisi najvecjega
+
'''] + +return_clause = ['''\ +

Namesto, da izpišemo rezultat, ga vračamo s stavkom return.

'''] + +plan = [['''\ +

Najprej poskusite napisati program (brez funkcije), ki v xs poišče največji element

''', + '''\ +
+xs = [5, 1, -6, -7, 2]
+# sledi program, ki izpiše največjega v xs
+
'''], + main_plan, + ['''\ +

Na vrhu napišite funkcijo, ki vsebuje program, in testirajte:

''', + '''\ +
+# najprej definicija funkcije
+def naj...
+
+s1 = [5, 1, -6, -7, 2]
+naj_elt = naj(s1)
+print(naj_elt)
+
+s2 = [-5, -1, 6, 7, -2]
+naj_elt = naj(s2)
+print(naj_elt)
+
'''], + function, + return_clause] + +for_loop = ['''\ +

Preglejte elemente z zanko

. +''', + '''\ +
+for x in xs:
+    print (x)
+
'''] + + +if_clause = ['''\ +

Preverite, če je trenutni element večji od največjega

''', + '''\ +
+if x > najvecji:
+    najvecji = x
+
'''] + + +hint = { + 'no_def': function, + + 'no_return': return_clause, + + 'for_loop': for_loop, + + 'if_clause': if_clause +} diff --git a/python/problems/functions/greatest_absolutist/common.py b/python/problems/functions/greatest_absolutist/common.py new file mode 100644 index 0000000..7871243 --- /dev/null +++ b/python/problems/functions/greatest_absolutist/common.py @@ -0,0 +1,98 @@ +# 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 = 196 +group = 'functions' +number = 2 +visible = True + +solution = '''\ +def naj_abs(xs): + naj = xs[0] + for x in xs: + if abs(x) > abs(naj): + naj = x + return naj +''' + +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'), +} + + +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_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) + +def hint(python, code): + # 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) + 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'}] + + # 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'}], + 6: [{'id' : 'return_positive'}] + } + 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_absolutist/en.py b/python/problems/functions/greatest_absolutist/en.py new file mode 100644 index 0000000..8058c4f --- /dev/null +++ b/python/problems/functions/greatest_absolutist/en.py @@ -0,0 +1,16 @@ +# coding=utf-8 + +id = 196 +name = '(translation missing)' +slug = '(translation missing)' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/functions/greatest_absolutist/sl.py b/python/problems/functions/greatest_absolutist/sl.py new file mode 100644 index 0000000..77815c0 --- /dev/null +++ b/python/problems/functions/greatest_absolutist/sl.py @@ -0,0 +1,100 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'sl') + + +id = 196 +name = 'Največji absolutist' +slug = 'Največji absolutist' + + +description = '''\ +

Napišite funkcijo naj_abs(xs), ki vrne največje število po +absolutni vrednosti v seznamu xs.

+
+>>> naj_abs([5, 1, -6, -7, 2])
+-7
+
+''' + +function = ['''\ +

Napišite definicijo funkcije naj_abs(xs).

''', + '''\ +

Definicijo funkcije začnemo z def, temu sledi ime, potem oklepaji, +v katerih naštejemo argumente funkcije, nato zaklepaj in na koncu dvopičje

''', + '''\ +
+def naj_abs(xs):
+    # poišči največje absolutno število v xs
+
+s = [5, 1, -6, -7, 2]
+print (naj_abs(s))
+
'''] + +main_plan = ['''\ +

Plan: po vrsti pogledamo vse elemente in sproti hranimo največjega +(glede na absolutno vrednost).

''', + '''\ +
+najvecji = prvi element od xs
+for x in xs:
+    če je x večji od najvecji po absolutni vrednosti:
+        najvecji postane x
+vrni najvecji
+
'''] + +return_clause = ['''\ +

Namesto, da izpišemo rezultat, ga vračamo s stavkom return.

'''] + +plan = [function, + main_plan, + return_clause] + +for_loop = ['''\ +

Preglejte elemente z zanko

. +''', + '''\ +
+for x in xs:
+
'''] + + +if_clause = ['''\ +

Preverite, če je trenutni element večji od največjega (po absolutni vrednosti)

''', + '''\ +
+if abs(x) > abs(najvecji):
+    najvecji = x
+
'''] + + +hint = { + 'no_def': function, + + 'no_return': return_clause, + + 'for_loop': for_loop, + + 'if_clause': if_clause, + + 'not_int': '''\ +

Funkcija ne vrača števila

''', + + 'return_first': '''\ +

Funkcija vrača prvi element v seznamu''', + + 'return_last': '''\ +

Funkcija vrača zadnji element v seznamu.''', + + 'return_greatest': '''\ +

Funkcija vrača največji pozitivni element v seznamu (ne pa največji po absolutni vrednosti).''', + + 'return_positive': '''\ +

Funkcija napačno vrača absolutno vrednost največjega števila.''', + + 'return_indent': '''\ +

Ali imate stavek return znotraj zanke? +V tem primeru se lahko zgodi, da se zanka ne izteče do konca.

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