-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_training.py
executable file
·122 lines (101 loc) · 3.28 KB
/
math_training.py
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
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
import bisect
import collections
import datetime
import json
import os
import random
import time
import termcolor
QUESTION_COUNT = 10
RESULTS_DIR = 'results'
Question = collections.namedtuple(
'Question', ['first', 'second', 'operation', 'expected'])
Result = collections.namedtuple(
'Result', ['question', 'error_count', 'time_sec'])
def make_question():
a = random.randint(100, 999)
b = random.randint(100, 999)
operation = '+' if random.randint(0, 1) == 0 else '-'
if operation == '+':
expected = a + b
else:
expected = a - b
return Question(a, b, operation, expected)
def ask_question(question):
start_time = time.time()
error_count = 0
while True:
print('{} {} {} = ... '.format(
question.first, question.operation, question.second))
try:
result = input()
result = int(result)
except ValueError:
print(f'Not a number {result}. Try again')
continue
if result == question.expected:
break
print(termcolor.colored('WRONG!', 'red'))
error_count += 1
time_delta = time.time() - start_time
return Result(question, error_count, time_delta)
def result_file_name():
script_dir = os.path.dirname(os.path.realpath(__file__))
d = datetime.datetime.fromtimestamp(time.time())
file_name = d.strftime('%Y-%m-%d_%H-%M-%S.json')
return os.path.join(script_dir, RESULTS_DIR, file_name)
def results_to_dicts(items):
result = []
for item in items:
d = item._asdict()
d['question'] = item.question._asdict()
result.append(d)
return result
def save_results(results):
file_name = result_file_name()
dir_name = os.path.dirname(file_name)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
with open(file_name, 'w') as f:
json.dump(results_to_dicts(results), f)
def load_all_times():
result = []
for file_name in os.listdir(RESULTS_DIR):
date_str = file_name.split('.')[0]
if not date_str:
continue
with open(os.path.join(RESULTS_DIR, file_name), 'r') as f:
items = json.load(f)
total_time = sum(d['time_sec'] for d in items)
result.append(total_time)
return result
def main():
results = []
all_times = load_all_times()
start = time.time()
error_count = 0
for question_idx in range(1, QUESTION_COUNT + 1):
print(termcolor.colored(
'# {}:'.format(question_idx),
'blue'))
question = make_question()
result = ask_question(question)
error_count += result.error_count
results.append(result)
time_delta = time.time() - start
save_results(results)
all_times.sort()
this_place = bisect.bisect_left(all_times, time_delta)
print(termcolor.colored(
f'{QUESTION_COUNT} operations took {time_delta:.3f} sec. {error_count} errors.',
'green'))
print(termcolor.colored(
f'This is {this_place + 1} place (by time) among {len(all_times) + 1}',
'green'))
percentile = ((this_place + 1) * 100.) / (len(all_times) + 1)
print(termcolor.colored(
f'This result is in the top {percentile:.3f}%',
'green'))
if __name__ == '__main__':
main()