选题添加最多次数

This commit is contained in:
2025-06-24 16:55:39 +08:00
parent c6af350e4b
commit 5eafb11f0b
2 changed files with 20 additions and 6 deletions

View File

@@ -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<No: {self._no}, Topic: {self._topic}>"
@@ -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):

View File

@@ -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)