Skip to content

Commit 663cb81

Browse files
Alexei BezborodovAlexei Bezborodov
Alexei Bezborodov
authored and
Alexei Bezborodov
committed
Math test
1 parent 0220b07 commit 663cb81

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

gen_math_test.py

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright © 2021 Alexei Bezborodov. Contacts: <[email protected]>
4+
# License: Public domain: http://unlicense.org/
5+
6+
from enum import Enum
7+
import random
8+
9+
class NoValue(Enum):
10+
def __repr__(self):
11+
return '<%s.%s>' % (self.__class__.__name__, self.name)
12+
13+
class AutoNumber(NoValue):
14+
def __new__(cls):
15+
value = len(cls.__members__) + 1
16+
obj = object.__new__(cls)
17+
obj._value_ = value
18+
return obj
19+
20+
class Tags(AutoNumber):
21+
INIT = () # Определение
22+
SUMM = () # Сумма
23+
MINUS = () # Минус
24+
MULTIPLE = () # Умножение
25+
DIVIDE = () # Деление
26+
DIV_INT = () # Целочисленное деление
27+
DIV_REM = () # Остаток от деления
28+
AND = () # И логический оператор
29+
OR = () # ИЛИ логический оператор
30+
NOT = () # НЕ логический оператор
31+
EQUAL = () # равно в условии
32+
GR_EQUAL = () # >равно в условии
33+
LE_EQUAL = () # <равно в условии
34+
GREAT = () # > в условии
35+
LESS = () # < в условии
36+
IF = () # Условия
37+
ELSEIF = () # Повторное условие
38+
ELSE = () # Иначе
39+
PRINT_VARS = ()
40+
MAX_TAGS = ()
41+
42+
class TagsSet:
43+
init = {
44+
Tags.INIT,
45+
}
46+
math = {
47+
Tags.SUMM,
48+
Tags.MINUS,
49+
Tags.MULTIPLE,
50+
Tags.DIVIDE,
51+
Tags.DIV_INT,
52+
Tags.DIV_REM,
53+
}
54+
logical = {
55+
Tags.AND,
56+
Tags.OR,
57+
Tags.NOT
58+
}
59+
condition = {
60+
Tags.EQUAL,
61+
Tags.GR_EQUAL,
62+
Tags.LE_EQUAL,
63+
Tags.GREAT,
64+
Tags.LESS,
65+
}
66+
print_vars = {
67+
Tags.PRINT_VARS,
68+
}
69+
all_set = init | math | logical | condition | print_vars
70+
71+
72+
class Property:
73+
rand_seed = 0
74+
tags = TagsSet.all_set
75+
var_prefix = "n"
76+
range_variables = (1, 3) # Количество используемых переменных (от, до)
77+
range_operators = (1, 2) # Количество используемых операторов (от, до)
78+
range_constant = (-10, 10) # Константы (от, до)
79+
range_math_action = (1, 2) # Количество используемых математических действий на каждом шаге
80+
range_math_nesting = (0, 3) # Вложенность математических операторов
81+
82+
range_math_try_nest = 0.5 # Войти во вложенность математических операторов
83+
range_math_change_to_constant = 0.3 # Заменить на константу
84+
85+
86+
def GetRandValue(range_val):
87+
return random.randint(range_val[0], range_val[1])
88+
89+
def CheckRandRange(range_val):
90+
return random.random() < range_val
91+
92+
93+
#random.seed(Property.rand_seed)
94+
95+
class CurTestValues:
96+
tags = Property.tags
97+
98+
range_constant = Property.range_constant
99+
max_vars = GetRandValue(Property.range_variables)
100+
range_math_action = Property.range_math_action
101+
range_math_nesting = Property.range_math_nesting
102+
103+
range_math_try_nest = Property.range_math_try_nest
104+
range_math_change_to_constant = Property.range_math_change_to_constant
105+
106+
var_list = [Property.var_prefix + str(i + 1) for i in range(max_vars)]
107+
result_code = ""
108+
109+
cur_test_val = CurTestValues()
110+
111+
def CheckSetInSet(set1, set2):
112+
if len(set1 & set2) > 0:
113+
return True
114+
else:
115+
return False
116+
117+
def CheckValueInSet(val, set_name):
118+
set_val = {val}
119+
return CheckSetInSet(set_val, set_name)
120+
121+
def MakeInit(cur_test_val):
122+
if CheckValueInSet(Tags.INIT, cur_test_val.tags):
123+
for val in cur_test_val.var_list:
124+
cur_test_val.result_code += val + " = " + str(GetRandValue(cur_test_val.range_constant)) + "\n"
125+
126+
def GetMathOper(tag):
127+
if tag == Tags.SUMM:
128+
return "+"
129+
elif tag == Tags.MINUS:
130+
return "-"
131+
elif tag == Tags.MULTIPLE:
132+
return "*"
133+
elif tag == Tags.DIVIDE:
134+
return "/"
135+
elif tag == Tags.DIV_INT:
136+
return "//"
137+
elif tag == Tags.DIV_REM:
138+
return "%"
139+
140+
def GetCurMath(cur_test_val, nest_len):
141+
cur_var = ["", ""]
142+
for i in range(2):
143+
if CheckRandRange(cur_test_val.range_math_change_to_constant):
144+
cur_var[i] = str(GetRandValue(cur_test_val.range_constant))
145+
elif CheckRandRange(cur_test_val.range_math_try_nest):
146+
cur_var[i] = "(" + GetCurMath(cur_test_val, nest_len - 1) + ")"
147+
else:
148+
cur_var[i] = random.choice(cur_test_val.var_list)
149+
150+
op = GetMathOper(random.sample(cur_test_val.tags & TagsSet.math, 1)[0])
151+
152+
return cur_var[0] + " " + op + " " + cur_var[1]
153+
154+
155+
def MakeMath(cur_test_val):
156+
cur_var = random.choice(cur_test_val.var_list)
157+
cur_nest = GetRandValue(cur_test_val.range_math_nesting)
158+
159+
cur_test_val.result_code += cur_var + " = " + GetCurMath(cur_test_val, cur_nest) + "\n"
160+
161+
def MakePrintVars(cur_test_val):
162+
if CheckValueInSet(Tags.PRINT_VARS, cur_test_val.tags):
163+
cur_test_val.result_code += "print(" + ",".join(cur_test_val.var_list) + ")"
164+
165+
# Запускаем программу
166+
167+
if CheckSetInSet(TagsSet.init, cur_test_val.tags):
168+
MakeInit(cur_test_val)
169+
170+
if CheckSetInSet(TagsSet.math, cur_test_val.tags):
171+
for i in range(GetRandValue(cur_test_val.range_math_action)):
172+
MakeMath(cur_test_val)
173+
174+
if CheckSetInSet(TagsSet.print_vars, cur_test_val.tags):
175+
MakePrintVars(cur_test_val)
176+
177+
print(cur_test_val.result_code)
178+
179+
180+
print("Ответ:")
181+
exec(cur_test_val.result_code)
182+
183+

0 commit comments

Comments
 (0)