From 5eafb11f0be44aaa442280d69bf6c895f69a7388 Mon Sep 17 00:00:00 2001 From: Jeffrey Hsu Date: Tue, 24 Jun 2025 16:55:39 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=89=E9=A2=98=E6=B7=BB=E5=8A=A0=E6=9C=80?= =?UTF-8?q?=E5=A4=9A=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/schema.py | 19 +++++++++++++++---- module/worker.py | 7 +++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/module/schema.py b/module/schema.py index 3166acf..f7dc93f 100644 --- a/module/schema.py +++ b/module/schema.py @@ -8,6 +8,7 @@ class Question: def __init__(self, no: str, topic: str): self._no: str = no self._topic: str = topic + self._count: int = 0 def __str__(self): return f"Question" @@ -43,6 +44,12 @@ class Question: def topic(self) -> str: return self._topic + def increase_count(self) -> None: + self._count += 1 + + def can_pick(self, max_count: int) -> bool: + return self._count <= max_count + class Student: def __init__(self, no: str, so: str, name: str, major: str, class_name: str): @@ -105,11 +112,15 @@ class Student: def class_name(self) -> str: return self._class_name - def pick_question(self, questions: list[Question], num: int = 3) -> None: - if len(questions) < num: - raise ValueError("Not enough questions to pick from.") - self._picked_questions = random.sample(questions, num) + def pick_question(self, questions: list[Question], num: int = 3, max_count: int = 3) -> None: + available_questions = [q for q in questions if q.can_pick(max_count)] + if len(available_questions) < num: + raise ValueError("Not enough questions with count <= max_count") + self._picked_questions = random.sample(available_questions, num) + + for q in self._picked_questions: + q.increase_count() class Course: def __init__(self, name: str): diff --git a/module/worker.py b/module/worker.py index 5994a21..6df740f 100644 --- a/module/worker.py +++ b/module/worker.py @@ -1,3 +1,4 @@ +import math import os import traceback @@ -39,11 +40,13 @@ class DTGWorker(QObject): students = Student.load_from_xls(self.input_filepath) questions = Question.load_from_xls(self.input_question_filepath) - d = DocPaper(self.output_filename, template_path=resource_path("template/template.docx")) + max_question_count = math.ceil(len(students) * 3 / len(questions)) + + d = DocPaper(self.output_filename, template_path=resource_path("template/template-2.docx")) for index, student in enumerate(students): if (p := int((index + 1) / len(students) * 100)) != 100: self.progress[int].emit(p) - student.pick_question(questions) + student.pick_question(questions, max_count=max_question_count) d.add_paper(course, student) d.save(self.output_filepath)