summaryrefslogtreecommitdiff
path: root/python/problems/functions/greatest
diff options
context:
space:
mode:
Diffstat (limited to 'python/problems/functions/greatest')
-rw-r--r--python/problems/functions/greatest/common.py73
-rw-r--r--python/problems/functions/greatest/en.py16
-rw-r--r--python/problems/functions/greatest/sl.py107
3 files changed, 196 insertions, 0 deletions
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 = '''\
+<p>(translation missing)</p>'''
+
+hint = {
+ 'plan': '''\
+<p>(translation missing)</p>''',
+
+ 'no_input_call': '''\
+<p>(translation missing)</p>''',
+}
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 = '''\
+<p>Napišite funkcijo <code>naj(xs)</code>, ki vrne največje število v seznamu <code>xs</code>.
+<pre>
+>>> naj([5, 1, -6, -7, 2])
+5
+</pre></p>
+'''
+
+function = ['''\
+<p>Napišite definicijo funckcije <code>naj(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):
+ # program, ki poišče največjega v xs
+</pre>''']
+
+main_plan = ['''\
+<p><b>Ideja</b>: po vrsti pogledamo vse elemente in sproti hranimo največjega.</p>''',
+ '''\
+<ol>
+<li>Začnemo s prvim elementom in si ga zapomnimo kot največjega.</li>
+<li>Potem pogledamo drugi element, če je večji od največjega, ta postane največji.</li>
+<li>Pogledamo tretji element, če je večji od največjega, postane največji. </li>
+<li>Pogledamo četrti element, ... </li>
+<li>...</li>
+<li>Pogledamo zadnji element, ...
+</ol>''',
+ '''\
+<p>Potrebovali bomo zanko!</p>
+<pre>
+najvecji = prvi element od xs
+for x in xs:
+ ce je x > najvecji:
+ najvecji postane x
+izpisi najvecjega
+</pre>''']
+
+return_clause = ['''\
+<p>Namesto, da izpišemo rezultat, ga vračamo s stavkom <code>return</code>.</p>''']
+
+plan = [['''\
+<p>Najprej poskusite napisati <b>program</b> (brez funkcije), ki v <code>xs</code> poišče največji element</p>''',
+ '''\
+<pre>
+xs = [5, 1, -6, -7, 2]
+# sledi program, ki izpiše največjega v xs
+</pre>'''],
+ main_plan,
+ ['''\
+<p>Na vrhu napišite funkcijo, ki vsebuje program, in testirajte:</p>''',
+ '''\
+<pre>
+# 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)
+</pre>'''],
+ function,
+ return_clause]
+
+for_loop = ['''\
+<p>Preglejte elemente z zanko</p>.
+''',
+ '''\
+<pre>
+for x in xs:
+ print (x)
+</pre>''']
+
+
+if_clause = ['''\
+<p>Preverite, če je trenutni element večji od največjega</p>''',
+ '''\
+<pre>
+if x > najvecji:
+ najvecji = x
+</pre>''']
+
+
+hint = {
+ 'no_def': function,
+
+ 'no_return': return_clause,
+
+ 'for_loop': for_loop,
+
+ 'if_clause': if_clause
+}