选题添加最多次数
This commit is contained in:
+15
-4
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user