summaryrefslogtreecommitdiff
path: root/python/problems/introduction/fast_fingers_2/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/problems/introduction/fast_fingers_2/common.py')
-rw-r--r--python/problems/introduction/fast_fingers_2/common.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/python/problems/introduction/fast_fingers_2/common.py b/python/problems/introduction/fast_fingers_2/common.py
new file mode 100644
index 0000000..9c8772f
--- /dev/null
+++ b/python/problems/introduction/fast_fingers_2/common.py
@@ -0,0 +1,67 @@
+# coding=utf-8
+
+from python.util import has_token_sequence, string_almost_equal
+from server.hints import Hint, HintSequence
+
+id = 191
+group = 'introduction'
+number = 2
+visible = True
+
+solution = '''\
+import random
+import time
+x = random.randint(1, 10)
+y = random.randint(1, 10)
+t = time.time()
+print('Koliko je ', x, ' krat ', y, '? ')
+z = float(input())
+if x * y == z:
+ print('Odgovor je pravilen.')
+else:
+ print('Odgovor ni pravilen.')
+print('Za razmišljanje ste porabili', time.time() ­ t, 's.')
+'''
+
+hint_type = {
+ 'plan': HintSequence('plan', 2),
+ 'random': Hint('random'),
+ 'name_error': HintSequence('name_error', 4),
+ 'type_error': HintSequence('type_error', 4),
+ 'error': HintSequence('error', 2),
+ 'if_clause': HintSequence('if_clause', 2),
+}
+
+def test(python, code):
+ return 0,0
+
+def hint(python, code):
+ # run one test first to see if there are any exceptions
+ test_in = [(None, '5\n')]
+ answer = python(code=code, inputs=test_in, timeout=1.0)
+ exc = answer[0][3]
+ # if have an exception!
+ if exc:
+ if 'NameError' in exc:
+ return [{'id':'name_error', 'args': {'message': exc}}]
+ elif 'TypeError' in exc:
+ return [{'id':'type_error', 'args': {'message': exc}}]
+ else:
+ return [{'id':'error', 'args': {'message': exc}}]
+
+ # First: if student does not import random, tell him about that module
+ if not has_token_sequence(code, ['random']) or \
+ not has_token_sequence(code, ['randint']):
+ return [{'id': 'random'}]
+
+ # show plan if student is lost
+ # a) empty progam or
+ # b) there is not input (we can do it here, since we have no input hint)
+ if len(code.strip()) < 5 or (not has_token_sequence(code, ['input'])):
+ return [{'id': 'plan'}]
+
+ # Student will have to test whether result is correct or not
+ if not has_token_sequence(code, ['if']):
+ return [{'id' : 'if_clause'}]
+
+ return None