-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharith.py
85 lines (77 loc) · 2.07 KB
/
arith.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
# -*- coding: utf-8 -*-
import sys
import random
from termcolor import colored
import time
LVL = 10 #20
N = 30 #10
P_sum = 0.5
MAX_ERRORS = 1
MIN_ELEM = 2
MIN_SUM = 4 #11
#LVL = 20
#N = 30
#P_sum = 0.5
#MAX_ERRORS = 1
#MIN_ELEM = 2
#MIN_SUM = 11
def gen_sub_primer(min_sum, max_sum):
a = random.randint(min_sum, max_sum)
b = random.randint(1, a - 1)
return a, b, a - b, '-'
#def gen_add_primer(min_sum, max_sum):
# a = random.randint(2, max_sum - 1)
# b = random.randint(1, min(a, max_sum - a))
# return a, b, a + b, '+'
def gen_add_primer(min_sum, max_sum):
s, a, b, _ = gen_sub_primer(min_sum, max_sum)
return a, b, s, '+'
def gen_primer(min_sum, max_sum):
rnd = random.random()
if rnd < P_sum:
return gen_add_primer(min_sum, max_sum)
else:
return gen_sub_primer(min_sum, max_sum)
def main():
t0 = time.time()
print "Уровень:", LVL
random.seed()
were = []
err = 0
i = 1
a = None
b = None
done = True
while True:
if done:
a, b, ans, sign = gen_primer(MIN_SUM, LVL)
if (a, b) in were:
continue
if a < MIN_ELEM or b < MIN_ELEM:
continue
done = False
sys.stdout.write('{}) {} {} {} = '.format(i, a, sign, b))
sAns = sys.stdin.readline()
if sAns.strip() == '.':
return
try:
if int(sAns) == ans:
were.append((a, b))
print colored('Правильно!', 'green')
else:
print colored('Неправильно.', 'red'), 'Правильный ответ:', colored('{} {} {} = {}'.format(a, sign, b, ans), 'magenta')
err = err + 1
i = i + 1
done = True
except:
continue
if i > N:
break
print 'Ошибок:', err
if err <= MAX_ERRORS:
print colored('Сдал!', 'green')
else:
print colored('Не сдал.', 'red')
print "Время:", time.time() - t0, "секунд"
if __name__ == '__main__':
main()