e}", "r") as f:
lines = f.readlines()[:-1:2] # Remove the '\n'
for line in lines:
if (prob == line[4:-1] or # Remove the prob index and space and '\n'
self.reverse(prob) == line[4:-1]):
return True
return False
def save_probs(self, account: str, probs_num: int) -> None:
"""Save the probs to the file.
Arguments:
account: The account using the program.
probs_num: The number of probs to generate.
Returns:
None.
"""
probs = []
for i in range(probs_num):
while True:
prob = f"{self.generate()}="
if ((prob not in probs) and
(not self.check_repeat(account, prob))):
break
probs.append(prob)
if not os.path.exists(f"exams/{account}"):
os.mkdir(f"exams/{account}")
# filename: 年-月-日-时-分-秒.txt
filename = f"{time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())}.txt"
with open(f"exams/{account}/{filename}", "w") as f:
index = 1
for prob in probs:
space = " " if index < 10 else " " # Add space to align the index
f.write(f"{index}.{space}{prob}\n\n")
index += 1
class ExamGenerator1(ExamGenerator):
"""Exam generator for primary school."""
def __init__(self) -> None:
super().__init__()
def generate(self, nums_range=(1, 5)) -> str:
op_num = random.randint(nums_range[0], nums_range[1])
op = random.choice(self.operators)
if op_num == 1:
if nums_range[1] == 5:
return self.generate((1, 5)) # If the result is only 1 number, generate again
else:
return str(random.randint(1, 100))
elif op_num == 2:
left_op = self.generate((1, 1))
right_op = self.generate((1, 1))
if random.random() < 0.45 and nums_range[1] != 5:
return f"({left_op}{op}{right_op})" # 45% to add brackets
else:
return f"{left_op}{op}{right_op}"
else:
left_op = self.generate((1, op_num // 2))
right_op = self.generate((1, op_num - op_num // 2))
if random.random() < 0.45 and nums_range[1] != 5:
return f"({left_op}{op}{right_op})"
else:
return f"{left_op}{op}{right_op}"
class ExamGenerator2(ExamGenerator):
"""Exam generator for junior high school."""
def __init__(self) -> None:
super().__init__()
self.operators.extend(["^2", "sqrt"])
def generate(self, nums_range=(1, 5)) -> str:
op_num = random.randint(nums_range[0], nums_range[1])
op = random.choice(self.operators)
op1 = random.choice(self.operators[:4])
if op_num == 1:
if op in self.operators[4:] and random.random() < 0.66:
result = self.unary_op(op, str(random.randint(1, 100)))
else:
result = str(random.randint(1, 100))
elif op_num == 2:
left_op = self.generate((1, 1))
right_op = self.generate((1, 1))
if op in ["^2", "sqrt"]:
return self.unary_op(op, f"{left_op}{op1}{right_op}")
else:
result = f"{left_op}{op}{right_op}"
else:
left_op = self.generate((1, op_num // 2))
right_op = self.generate((1, op_num - op_num // 2))
if op in ["^2", "sqrt"]:
result = self.unary_op(op, f"{left_op}{op1}{right_op}")
else:
result = f"{left_op}{op}{right_op}"
if (nums_range[1] == 5 and result.find("sqrt") == -1 and
result.find("^2") == -1): # If the result don't contai |