summaryrefslogtreecommitdiff
path: root/python/problems/introduction/fast_fingers/common.py
blob: 8ab51ab9a772ed1c85e14a07452e989389497dd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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):
    passed = True
    hints = [{'id': 'test_results', 'args': {'passed': 0, 'total': 0}}]
    return passed, hints

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