summaryrefslogtreecommitdiff
path: root/python/problems/lists_and_for/perfect_numbers
diff options
context:
space:
mode:
Diffstat (limited to 'python/problems/lists_and_for/perfect_numbers')
-rw-r--r--python/problems/lists_and_for/perfect_numbers/common.py114
-rw-r--r--python/problems/lists_and_for/perfect_numbers/en.py16
-rw-r--r--python/problems/lists_and_for/perfect_numbers/sl.py96
3 files changed, 226 insertions, 0 deletions
diff --git a/python/problems/lists_and_for/perfect_numbers/common.py b/python/problems/lists_and_for/perfect_numbers/common.py
new file mode 100644
index 0000000..c1c7645
--- /dev/null
+++ b/python/problems/lists_and_for/perfect_numbers/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
+
+id = 217
+group = 'lists_and_for'
+number = 22
+visible = False
+
+solution = '''\
+n = int(input('Vnesi število: '))
+vsota = 0
+for i in range(1, n):
+ if n % i == 0:
+ vsota += i
+if vsota == n:
+ print('Je popolno')
+else:
+ print('Ni popolno')
+'''
+
+hint_type = {
+ 'input_clause': Hint('input_clause'),
+ 'range_function': Hint('range_function'),
+ 'printing': Hint('printing'),
+ 'divisor': Hint('divisor'),
+ 'final_hint': Hint('final_hint'),
+ 'zero_division': Hint('zero_division'),
+ 'last_number': Hint('last_number'),
+ 'summing': Hint('summing'),
+}
+
+def test(python, code):
+ test_in = [
+ (None, '8\n'),
+ (None, '6\n'),
+ (None, '5\n'),
+ (None, '2\n'),
+ (None, '15\n'),
+ (None, '20\n'),
+ ]
+ test_out = [
+ 7,
+ 6,
+ 1,
+ 1,
+ 9,
+ 22]
+
+ # List of outputs: (expression result, stdout, stderr, exception).
+ answers = python(code=code, inputs=test_in, timeout=1.0)
+ outputs = [ans[1] for ans in answers]
+
+ n_correct, tin = 0, None
+ for i, (output, result) in enumerate(zip(outputs, test_out)):
+ if result in get_numbers(output):
+ n_correct += 1
+ else:
+ tin = test_in[i][1]
+ tout = result
+
+ 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, '8\n')], timeout=1.0)
+ exc = get_exception_desc(answer[0][3])
+ if exc:
+ if 'ZeroDivisionError' in answer[0][3]:
+ return [{'id' : 'zero_division', 'args': {'message': answer[0][3]}}]
+ else:
+ return exc
+
+ # if has no input, tell him how to ask questions
+ if not has_token_sequence(tokens, ['input']):
+ return [{'id' : 'input_clause'}]
+
+ # if it has no range, explain how we can create a list (generator) from a number
+ if not has_token_sequence(tokens, ['range']):
+ return [{'id' : 'range_function'}]
+
+ # if it has no print function, tell him to use print
+ if not has_token_sequence(tokens, ['print']):
+ return [{'id' : 'printing'}]
+
+ # if it has no divisoin-by-modulo operator, explain that operator
+ if not has_token_sequence(tokens, ['%']):
+ return [{'id' : 'divisor'}]
+
+ # if the program prints 15 instead of 7,
+ # it means that is adds also itself
+ nums = get_numbers(answer[0][1])
+ if 15 in nums and 7 not in nums:
+ return [{'id' : 'last_number'}]
+
+ # if program prints only the last number or
+ # only the first number, explain how to sum
+ # elements in iterations
+ if 7 not in nums and \
+ (1 in nums or 4 in nums or 8 in nums):
+ return [{'id' : 'summing'}]
+
+
+ return None
diff --git a/python/problems/lists_and_for/perfect_numbers/en.py b/python/problems/lists_and_for/perfect_numbers/en.py
new file mode 100644
index 0000000..19d5262
--- /dev/null
+++ b/python/problems/lists_and_for/perfect_numbers/en.py
@@ -0,0 +1,16 @@
+# coding=utf-8
+
+id = 217
+name = 'Perfect number'
+slug = 'Perfect number'
+
+description = '''\
+<p>(translation missing)</p>'''
+
+hint = {
+ 'plan': '''\
+<p>(translation missing)</p>''',
+
+ 'no_input_call': '''\
+<p>(translation missing)</p>''',
+}
diff --git a/python/problems/lists_and_for/perfect_numbers/sl.py b/python/problems/lists_and_for/perfect_numbers/sl.py
new file mode 100644
index 0000000..bc02d00
--- /dev/null
+++ b/python/problems/lists_and_for/perfect_numbers/sl.py
@@ -0,0 +1,96 @@
+# coding=utf-8
+import server
+mod = server.problems.load_language('python', 'sl')
+
+
+id = 217
+name = 'Popolna števila'
+slug = 'Popolna števila'
+
+
+description = '''\
+<p> Napiši program, ki izpiše "je popolno", če je podano število popolno, oz.
+"ni popolno", č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>'''
+
+input_clause = ['''\
+Uporabi funkcijo <code>input</code>.
+''']
+
+range_function = ['''\
+Uporabi funkcijo <code>range(a,b)</code>
+''',
+ '''\
+<p>Funkcija <code>range(a, b)</code> pripravi seznam števil od a do števila b-1. Poskusi!</p>''',
+ '''\
+<p>Z zanko <code>for</code> se sprehodi čez elemente seznama oz. v našem primeru
+čez vsa števila od <code>a</code> do <code>b-1</code>.</p>
+''']
+
+divisor = ['''\
+<p>Operator % vrne ostanek pri deljenju.</p>''',
+ '''\
+<pre>
+>>> 5%3
+2
+>>> 17%8
+1
+</pre>''',
+ '''\
+<p>Če je ostanek pri deljenju števila <code>a</code> s številom <code>b</code> enak 0, potem <code>b</code>
+deli število <code>a</code>. </p>''',
+ '''\
+<pre>
+if a%b == 0:
+ # b je delitelj števila a
+</pre>
+''']
+
+summing = ['''\
+<p>Če hočemo delitelje prišteti vsoti, moramo imeti vsoto nekje shranjeno.
+Torej potrebujemo spremenljivko.</p>''',
+ '''\
+<p>Nekako tako: </p>
+<pre>
+vsota = 0
+for ....
+ if ...
+ vsota += d # d deli izbrano število
+</pre>''']
+
+plan = ['''\
+<p>Gre za praktično enako nalogo, kot je bila prejšnja. Le na koncu
+moramo preveriti, če je vsota deliteljev enaka vnešenemu številu. </p>
+''']
+
+
+hint = {
+ 'input_clause': input_clause,
+
+ 'range_function': range_function,
+
+ 'printing': ['''\
+<p>Izpiši rezultat.</p>'''],
+
+ 'divisor': divisor,
+
+ 'final_hint': ['''\
+<p>Naloga rešena! <br>
+Podobno kot prejšnjo, lahko rešitev napišemo krajše: </p>
+<pre>
+n = int(input("Vpiši število: "))
+print(n == sum(i for i in range(1, n) if n % i == 0))
+</pre>'''],
+
+ 'summing': summing,
+
+ 'zero_division': [mod.general_msg['error_head'],
+ mod.general_msg['general_exception'],
+ '''\
+<p>Deljenje z nič ni dovoljeno!</p>''',
+ '''\
+<p>Računanje ostanka z operatorjem % je tudi deljenje.''']
+
+}