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
108
109
110
111
112
113
|
# coding=utf-8
import re
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 = 217
number = 22
visible = True
solution = '''\
n = int(input('Vnesi število: '))
vsota = 0
for i in range(1, n):
if n % i == 0:
vsota += i
if vsota == n:
print('Je popolno')
else:
print('Ni popolno')
'''
hint_type = {
'input_clause': Hint('input_clause'),
'range_function': Hint('range_function'),
'printing': Hint('printing'),
'divisor': Hint('divisor'),
'final_hint': Hint('final_hint'),
'zero_division': Hint('zero_division'),
'last_number': Hint('last_number'),
'summing': Hint('summing'),
}
def test(python, code):
test_in = [
(None, '8\n'),
(None, '6\n'),
(None, '5\n'),
(None, '2\n'),
(None, '15\n'),
(None, '20\n'),
]
test_out = [
7,
6,
1,
1,
9,
22]
# List of outputs: (expression result, stdout, stderr, exception).
answers = python(code=code, inputs=test_in, timeout=1.0)
outputs = [ans[1] for ans in answers]
n_correct, tin = 0, None
for i, (output, result) in enumerate(zip(outputs, test_out)):
if result in get_numbers(output):
n_correct += 1
else:
tin = test_in[i][1]
tout = result
passed = n_correct == len(test_in)
hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
if tin:
hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})
if passed:
hints.append({'id': 'final_hint'})
return passed, hints
def hint(python, code):
tokens = get_tokens(code)
# run one test first to see if there are any exceptions
answer = python(code=code, inputs=[(None, '8\n')], timeout=1.0)
exc = get_exception_desc(answer[0][3])
if exc:
if 'ZeroDivisionError' in answer[0][3]:
return [{'id' : 'zero_division', 'args': {'message': answer[0][3]}}]
else:
return exc
# if has no input, tell him how to ask questions
if not has_token_sequence(tokens, ['input']):
return [{'id' : 'input_clause'}]
# if it has no range, explain how we can create a list (generator) from a number
if not has_token_sequence(tokens, ['range']):
return [{'id' : 'range_function'}]
# if it has no print function, tell him to use print
if not has_token_sequence(tokens, ['print']):
return [{'id' : 'printing'}]
# if it has no divisoin-by-modulo operator, explain that operator
if not has_token_sequence(tokens, ['%']):
return [{'id' : 'divisor'}]
# if the program prints 15 instead of 7,
# it means that is adds also itself
nums = get_numbers(answer[0][1])
if 15 in nums and 7 not in nums:
return [{'id' : 'last_number'}]
# if program prints only the last number or
# only the first number, explain how to sum
# elements in iterations
if 7 not in nums and \
(1 in nums or 4 in nums or 8 in nums):
return [{'id' : 'summing'}]
return None
|