summaryrefslogtreecommitdiff
path: root/python/problems/introduction/fast_fingers_2/common.py
blob: d562f7bd697e7f00997b7cca12bfead570e0e3fb (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
64
65
66
67
68
69
# 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):
    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=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