summaryrefslogtreecommitdiff
path: root/python/problems/introduction/fast_fingers_2/common.py
blob: 01ad94126ac501037073f99a875d8d3318e70ace (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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 = 191
number = 6
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(True)
else:
    print(False)
print('Za razmišljanje ste porabili', time.time() ­ t, 's.')
'''

random_code = '''\
import random
random.randint = lambda x, y: {}
'''

hint_type = {
    'random': Hint('random'),
    'if_clause': Hint('if_clause'),
    'final_hint': Hint('final_hint'),
    'final_hint_noif': Hint('final_hint_noif')
}

def test(python, code):
    # List of inputs: (expression to eval, stdin).
    test = [(5,'25\n'),
            (6, '29\n'),
            (1, '1\n'),
            (7, '65\n'),
            (9, '81\n')]

    test_out = [
        True,
        False,
        True,
        False,
        True
    ]

   # List of outputs: (expression result, stdout, stderr, exception).

    n_correct = 0
    tin = None
    for ti, t in enumerate(test):
        # change code, so that it replaces random values
        # hook randint
        tcode = random_code.format(t[0]) + code

        answers = python(code=tcode, inputs=[(None, t[1])], timeout=1.0)
        output = answers[0][1]

        if str(test_out[ti]) in output and str(not test_out[ti]) not in output:
            n_correct += 1
        else:
            tin = '{t[1]}'.format(t=t)
            tout = test_out[ti]
            mult = '{t[0]}*{t[0]}'.format(t=t)

    passed = n_correct == len(test)
    hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test)}}]
    if tin:
        hints.append({'id': 'problematic_test_case', 'args': {'mult' : mult, 'testin': str(tin), 'testout': str(tout)}})
    else:
        tokens = get_tokens(code)
        if has_token_sequence(tokens, ['if']):
            hints.append({'id': 'final_hint'})
        else:
            hints.append({'id': 'final_hint_noif'})
            
    return passed, hints

def hint(python, code):
    tokens = get_tokens(code)

    # run one test first to see if there are any exceptions
    test_in = [(None, '16\n')]
    answer = python(code=code, inputs=test_in, timeout=1.0)
    exc = answer[0][3]
    exc_hint = get_exception_desc(answer[0][3])
    # if have an exception!
    if exc_hint:
        return exc_hint

    # First: if student does not import random, tell him about that module
    if not has_token_sequence(tokens, ['random']) or \
       not has_token_sequence(tokens, ['randint']):
        return [{'id': 'random'}]


    # Student will have to test whether result is correct or not
    if not has_token_sequence(tokens, ['if']) and \
       not has_token_sequence(tokens, ['==']):
        return [{'id' : 'if_clause'}]

    return None