设为首页 加入收藏

TOP

HNU个人项目互评(三)
2023-09-23 15:43:37 】 浏览:182
Tags:HNU 项目互
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
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python入门基础(14)--类的属性、.. 下一篇【matplotlib基础】--动画

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目