-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquestion.py
executable file
·32 lines (25 loc) · 1.07 KB
/
question.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# this is the only way to prevent circular imports; which is only importing contest if
# type checking is happening, which does not occur at runtime
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from contest import Contest
class Question:
def __init__(self, contest_instance: Contest, correct_answer: float, point_value: int):
self.contest_instance = contest_instance
self.correct_answer = correct_answer
self.point_value = point_value
@property
def number(self) -> int:
return self.contest_instance.questions.index(self) + 1
# corrects for floating point error.
def verify(self, answer: float): return abs(answer - self.correct_answer) < 1e-8
@property
def data(self) -> dict:
return {
"correctAnswer": self.correct_answer,
"pointValue": self.point_value
}
@staticmethod
def from_data(contest_instance, data: dict):
return Question(contest_instance, data["correctAnswer"], data["pointValue"])