-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathr_quiz.py
237 lines (181 loc) · 6.4 KB
/
r_quiz.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""Rechen-Quiz
Randomly generates a mix of math quizzes for the elementary school:
- +/- quizzes
- Multiply/division quizzes
Output will be written to a pdf file defined by --path
"""
import abc
import random
from dataclasses import dataclass
from enum import Enum
import numpy as np
PLUS_MINUS_MAX = 1_000_000
MUL_RESULT_MAX = 10_000
QUIZ_PER_GROUP = 15
def random_bool():
"""Generate a random boolean"""
return bool(random.getrandbits(1))
@dataclass(frozen=True)
class OrderQuiz(abc.ABC):
"""Base class for quiz where order matters"""
left: int
right: int
@abc.abstractmethod
def str(self, schriftlich=False) -> str:
"""
Returns the str representation of the quiz.
Parameters
----------
schriftlich : bool, default False
False to print a simple form e.g. '1+1='
True to print a 'schriftlich' form, e.g.
1
+12
---
"""
raise NotImplementedError
@property
def max_len(self) -> int:
"Max len of left & right"
return max(len(str(self.left)), len(str(self.right)))
class NoOrderQuiz(OrderQuiz):
# Intermediate ABC
# pylint: disable=W0223
"""Base class for quiz where order doesn't matter"""
def __init__(self, one, the_other):
if random_bool():
super().__init__(one, the_other)
else:
super().__init__(the_other, one)
def __eq__(self, other):
if isinstance(other, type(self)):
this_no_order = set((self.left, self.right))
other_no_order = set((other.left, other.right))
return this_no_order == other_no_order
return NotImplemented
def __hash__(self):
return hash(tuple(sorted((self.left, self.right))))
class PlusQuiz(NoOrderQuiz):
# pylint: disable=too-few-public-methods
"""A + quiz"""
def __init__(self):
super().__init__(
random.randrange(1, PLUS_MINUS_MAX), random.randrange(1, PLUS_MINUS_MAX)
)
def str(self, schriftlich=False) -> str:
if schriftlich:
return f""" {str(self.left).rjust(self.max_len)}
+{str(self.right).rjust(self.max_len)}
{'-'*(self.max_len+1)}"""
return f"{self.left}+{self.right}="
class MinusQuiz(OrderQuiz):
# pylint: disable=too-few-public-methods
"""A - quiz"""
def __init__(self):
minuend = random.randrange(2, PLUS_MINUS_MAX)
super().__init__(minuend, random.randrange(1, minuend))
def str(self, schriftlich=False) -> str:
if schriftlich:
return f""" {str(self.left).rjust(self.max_len)}
-{str(self.right).rjust(self.max_len)}
{'-'*(self.max_len+1)}"""
return f"{self.left}-{self.right}="
class MulQuiz(NoOrderQuiz):
# pylint: disable=too-few-public-methods
"""A multiply quiz"""
def __init__(self):
one_digit_factor = random.randrange(2, 100)
super().__init__(
one_digit_factor, random.randrange(2, MUL_RESULT_MAX // one_digit_factor)
)
def str(self, schriftlich=False) -> str:
quiz = f"{self.left}⋅{self.right}"
return quiz + ("\n" + "-" * len(quiz) if schriftlich else "=")
class DivQuiz(OrderQuiz):
# pylint: disable=too-few-public-methods
"""A division quiz"""
def __init__(self):
divisor = random.randrange(2, 9)
super().__init__(random.randrange(divisor, 1000), divisor)
def str(self, _=False) -> str:
return f"{self.left}:{self.right}="
class Layout(Enum):
"""Contains info about the layout depending on the quiz form (schriftlich or not)"""
SIMPLE = 3, 27
SCHRIFTLICH = 6, 7
def __init__(self, col: int, row: int):
self.col = col
self.row = row
def produce_quizzes(count, ratio_plus_minus):
"""Produce a set of quizzes w/ size of count"""
quizzes = set()
while len(quizzes) < count:
if random.random() > ratio_plus_minus:
if random_bool():
quizzes.add(MulQuiz())
else:
quizzes.add(DivQuiz())
else:
if random_bool():
quizzes.add(PlusQuiz())
else:
quizzes.add(MinusQuiz())
return quizzes
def produce_matrix(ratio_plus_minus, schriftlich) -> list[str]:
"Produce a 2d matrix of quizzes"
layout = Layout.SCHRIFTLICH if schriftlich else Layout.SIMPLE
count = layout.row * layout.col
quizzes = produce_quizzes(count, ratio_plus_minus)
quizzes_str = [q.str(schriftlich) for q in quizzes]
return np.char.array(quizzes_str).reshape(layout.row, layout.col).tolist()
if __name__ == "__main__":
import argparse
import os
from reportlab.platypus import SimpleDocTemplate, Table
from reportlab.lib import colors
def positive_float(arg):
"Parses arg as a postive float"
flt = float(arg)
if flt <= 0:
raise argparse.ArgumentTypeError(f"{arg} is not postive")
return flt
PARSER = argparse.ArgumentParser()
PARSER.add_argument(
"--ratio-plus-minus",
help="ratio of +/- quizzes (default=0.4, i.e. 40%% of quizzes will be +/-)",
default=0.4,
type=positive_float,
)
PARSER.add_argument(
"-S",
"--schriftlich",
help="produce quizzes in 'schriftlich' forms",
action="store_true",
)
PARSER.add_argument(
"-P",
"--path",
help=(
"where the output will be saved, existing file will be overwritten"
" (default=r_quiz.pdf in the working directory)"
),
default="r_quiz.pdf",
)
ARGS, _ = PARSER.parse_known_args()
DATA = produce_matrix(ARGS.ratio_plus_minus, ARGS.schriftlich)
STYLE = [
("SIZE", (0, 0), (-1, -1), 16),
("RIGHTPADDING", (0, 0), (-1, -1), 20 if ARGS.schriftlich else 55),
("BOTTOMPADDING", (0, 0), (-1, -1), 55 if ARGS.schriftlich else 10),
# monospace necessary for schriftlich alignment
("FONTNAME", (0, 0), (-1, -1), "Courier"),
("VALIGN", (0, 0), (-1, -1), "TOP"),
]
LAYOUT = Layout.SCHRIFTLICH if ARGS.schriftlich else Layout.SIMPLE
INTERVAL = QUIZ_PER_GROUP // LAYOUT.col
LINE = 0
while LINE < LAYOUT.row:
LINE += INTERVAL
STYLE.append(("LINEABOVE", (0, LINE), (-1, LINE), 1, colors.black))
SimpleDocTemplate(ARGS.path).build([Table(DATA, style=STYLE)])
print(f"Written to {os.path.abspath(ARGS.path)}")