summaryrefslogtreecommitdiff
path: root/python/problems/introduction/fast_fingers/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/problems/introduction/fast_fingers/common.py')
-rw-r--r--python/problems/introduction/fast_fingers/common.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/python/problems/introduction/fast_fingers/common.py b/python/problems/introduction/fast_fingers/common.py
new file mode 100644
index 0000000..3c1b138
--- /dev/null
+++ b/python/problems/introduction/fast_fingers/common.py
@@ -0,0 +1,61 @@
+# coding=utf-8
+
+from python.util import has_token_sequence
+from server.hints import Hint, HintSequence
+
+id = 190
+group = 'introduction'
+number = 2
+visible = True
+
+solution = '''\
+from time import time
+zacetek = time()
+rezultat = float(input("Koliko je 7*6? "))
+konec = time()
+cas = konec­zacetek
+print ("Porabil si ", cas, "s.")
+'''
+
+hint_type = {
+ 'plan': Hint('plan'),
+ 'name_error': HintSequence('name_error', 4),
+ 'type_error': HintSequence('name_error', 4),
+ 'error': HintSequence('error', 2),
+ 'time': Hint('time'),
+ 'time_diff': Hint('time_diff'),
+}
+
+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=2.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 time, tell him about that module
+ if not has_token_sequence(code, ['time']):
+ return [{'id': 'time'}]
+
+ # 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 compute time difference and they will need
+ # the substraction (-) operator to do this. Check for the minus operator.
+ if not has_token_sequence(code, ['-']):
+ return [{'id' : 'time_diff'}]
+
+ return None