-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathrun.py
286 lines (252 loc) · 11.8 KB
/
run.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
A module for creating .pdf math worksheets
"""
__author__ = 'januschung'
import argparse
import random
from fpdf import FPDF
from fpdf.enums import XPos, YPos
from functools import reduce
from typing import List, Tuple
QuestionInfo = Tuple[int, str, int, int]
class MathWorksheetGenerator:
"""class for generating math worksheet of specified size and main_type"""
def __init__(self, type_: str, max_number: int, question_count: int):
self.main_type = type_
self.max_number = max_number
self.question_count = question_count
self.pdf = FPDF()
self.small_font_size = 10
self.middle_font_size = 15
self.large_font_size = 30
self.size = 21
self.tiny_pad_size = 2
self.pad_size = 10
self.large_pad_size = 30
self.num_x_cell = 4
self.num_y_cell = 2
self.font_1 = 'Times'
self.font_2 = 'Helvetica'
# From https://stackoverflow.com/questions/6800193/what-is-the-most-efficient-way-of-finding-all-the-factors-of-a
# -number-in-python
def factors(self, n: int):
return set(reduce(list.__add__,
([i, n//i] for i in range(1, int(n**0.5) 1) if n % i == 0)))
def division_helper(self, num) -> [int, int, int]:
# prevent num = 0 or divisor = 1 or divisor = dividend
factor = 1
while not num or factor == 1 or factor == num:
num = random.randint(0, self.max_number)
# pick a factor of num; answer will always be an integer
if num:
factor = random.sample(list(self.factors(num)), 1)[0]
answer = int(num / factor)
return [num, factor, answer]
def generate_question(self) -> QuestionInfo:
"""Generates each question and calculate the answer depending on the type_ in a list
To keep it simple, number is generated randomly within the range of 0 to 100
:return: list of value1, main_type, value2, and answer for the generated question
"""
num_1 = random.randint(0, self.max_number)
num_2 = random.randint(0, self.max_number)
if self.main_type == 'mix':
current_type = random.choice([' ', '-', 'x', '/'])
else:
current_type = self.main_type
if current_type == ' ':
answer = num_1 num_2
elif current_type == '-':
# avoid having a negative answer which is an advanced concept
num_1, num_2 = sorted((num_1, num_2), reverse=True)
answer = num_1 - num_2
elif current_type == 'x':
answer = num_1 * num_2
elif current_type == '/':
num_1, num_2, answer = self.division_helper(num_1)
else:
raise RuntimeError(f'Question main_type {current_type} not supported')
return num_1, current_type, num_2, answer
def get_list_of_questions(self, question_count: int) -> List[QuestionInfo]:
"""Generate all the questions for the worksheet in a list. Initially trying for unique questions, but
allowing duplicates if needed (e.g. asking for 80 addition problems with max size 3 requires duplication)
:return: list of questions
"""
questions = []
duplicates = 0
while len(questions) < question_count:
new_question = self.generate_question()
if new_question not in questions or duplicates >= 10:
questions.append(new_question)
else:
duplicates = 1
return questions
def make_question_page(self, data: List[QuestionInfo]):
"""Prepare a single page of questions"""
page_area = self.num_x_cell * self.num_y_cell
problems_per_page = self.split_arr(self.question_count, page_area)
total_pages = len(problems_per_page)
for page in range(total_pages):
self.pdf.add_page(orientation='L')
if problems_per_page[page] < self.num_x_cell:
self.print_question_row(data, page * page_area, problems_per_page[page])
else:
problems_per_row = self.split_arr(problems_per_page[page], self.num_x_cell)
total_rows = len(problems_per_row)
self.print_question_row(data, page * page_area, problems_per_row[0])
for row in range(1, total_rows):
page_row = row * self.num_x_cell
self.print_horizontal_separator()
self.print_question_row(data, page * page_area page_row, problems_per_row[row])
def split_arr(self, x: int, y: int):
"""Split x into x = y y ... (x % y)"""
quotient, remainder = divmod(x, y)
if remainder != 0:
return [y] * quotient [remainder]
return [y] * quotient
def print_top_row(self, question_num: str):
"""Helper function to print first character row of a question row"""
self.pdf.set_font(self.font_1, size=self.middle_font_size)
self.pdf.cell(self.pad_size, self.pad_size, txt=question_num, border='LT', align='C')
self.pdf.cell(self.size, self.pad_size, border='T')
self.pdf.cell(self.size, self.pad_size, border='T')
self.pdf.cell(self.pad_size, self.pad_size, border='TR')
def print_second_row(self, num: int):
"""Helper function to print second character row of a question row"""
self.pdf.set_font(self.font_2, size=self.large_font_size)
self.pdf.cell(self.pad_size, self.size, border='L')
self.pdf.cell(self.size, self.size)
self.pdf.cell(self.size, self.size, txt=str(num), align='R')
self.pdf.cell(self.pad_size, self.size, border='R')
def print_second_row_division(self, num_1: int, num_2: int):
"""Helper function to print second character row of a question row for division"""
self.pdf.set_font(self.font_2, size=self.large_font_size)
self.pdf.cell(self.pad_size, self.size, border='L')
self.pdf.cell(self.size, self.size, txt=str(num_2), align='R')
x_cor = self.pdf.get_x() - 3
y_cor = self.pdf.get_y()
self.pdf.image(name='division.png', x=x_cor, y=y_cor)
self.pdf.cell(self.size, self.size, txt=str(num_1), align='R')
self.pdf.cell(self.pad_size, self.size, border='R')
def print_third_row(self, num: int, current_type: str):
"""Helper function to print third character row of a question row"""
self.pdf.cell(self.pad_size, self.size, border='L')
self.pdf.cell(self.size, self.size, txt=current_type, align='L')
self.pdf.cell(self.size, self.size, txt=str(num), align='R')
self.pdf.cell(self.pad_size, self.size, border='R')
def print_third_row_division(self):
"""Helper function to print third character row of a question row for division"""
self.pdf.cell(self.pad_size, self.size, border='L')
self.pdf.cell(self.size, self.size, align='L')
self.pdf.cell(self.size, self.size, align='R')
self.pdf.cell(self.pad_size, self.size, border='R')
def print_bottom_row(self):
"""Helper function to print bottom row of question"""
self.pdf.cell(self.pad_size, self.size, border='LB')
self.pdf.cell(self.size, self.size, border='TB')
self.pdf.cell(self.size, self.size, border='TB')
self.pdf.cell(self.pad_size, self.size, border='BR')
def print_bottom_row_division(self):
"""Helper function to print bottom row of question"""
self.pdf.cell(self.pad_size, self.size, border='LB')
self.pdf.cell(self.size, self.size, border='B')
self.pdf.cell(self.size, self.size, border='B')
self.pdf.cell(self.pad_size, self.size, border='BR')
def print_edge_vertical_separator(self):
"""Print space between question for the top or bottom row"""
self.pdf.cell(self.pad_size, self.pad_size)
def print_middle_vertical_separator(self):
"""Print space between question for the second or third row"""
self.pdf.cell(self.pad_size, self.size)
def print_horizontal_separator(self):
"""Print line breaker between two rows of questions"""
self.pdf.cell(self.size, self.size)
self.pdf.ln()
def print_question_row(self, data, offset, num_problems):
"""Print a single row of questions (total question in a row is set by num_x_cell)"""
for x in range(0, num_problems):
self.print_top_row(str(x 1 offset))
self.print_edge_vertical_separator()
self.pdf.ln()
for x in range(0, num_problems):
if data[x offset][1] == '/':
self.print_second_row_division(data[x offset][0], data[x offset][2])
else:
self.print_second_row(data[x offset][0])
self.print_middle_vertical_separator()
self.pdf.ln()
for x in range(0, num_problems):
if data[x offset][1] == '/':
self.print_third_row_division()
else:
self.print_third_row(data[x offset][2], data[x offset][1])
self.print_middle_vertical_separator()
self.pdf.ln()
for x in range(0, num_problems):
if data[x offset][1] == '/':
self.print_bottom_row_division()
else:
self.print_bottom_row()
self.print_edge_vertical_separator()
self.pdf.ln()
def make_answer_page(self, data):
"""Print answer sheet"""
self.pdf.add_page(orientation='L')
self.pdf.set_font(self.font_1, size=self.large_font_size)
self.pdf.cell(self.large_pad_size, self.large_pad_size, txt='Answers', new_x=XPos.LEFT, new_y=YPos.NEXT, align='C')
for i in range(len(data)):
self.pdf.set_font(self.font_1, size=self.small_font_size)
self.pdf.cell(self.pad_size, self.pad_size, txt=f'{i 1}:', border='TLB', align='R')
self.pdf.set_font(self.font_2, size=self.small_font_size)
self.pdf.cell(self.pad_size, self.pad_size, txt=str(data[i][3]), border='TB', align='R')
self.pdf.cell(self.tiny_pad_size, self.pad_size, border='TRB', align='R')
self.pdf.cell(self.tiny_pad_size, self.pad_size, align='C')
if i >= 9 and (i 1) % 10 == 0:
self.pdf.ln()
def main(type_, size, question_count, filename):
"""main function"""
new_pdf = MathWorksheetGenerator(type_, size, question_count)
seed_question = new_pdf.get_list_of_questions(question_count)
new_pdf.make_question_page(seed_question)
new_pdf.make_answer_page(seed_question)
new_pdf.pdf.output(filename)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Generate Maths Addition/Subtraction/Multiplication Exercise Worksheet'
)
parser.add_argument(
'--type',
default=' ',
choices=[' ', '-', 'x', '/', 'mix'],
help='type of calculation: '
' : Addition; '
'-: Subtraction; '
'x: Multiplication; '
'/: Division; '
'mix: Mixed; '
'(default: )',
)
parser.add_argument(
'--digits',
default='2',
choices=['1', '2', '3'],
help='range of numbers: 1: 0-9, 2: 0-99, 3: 0-999' '(default: 2 -> 0-99)',
)
parser.add_argument(
'-q',
'--question_count',
type=int,
default='80', # Must be a multiple of 40
help='total number of questions' '(default: 80)',
)
parser.add_argument('--output', metavar='filename.pdf', default='worksheet.pdf',
help='Output file to the given filename '
'(default: worksheet.pdf)')
args = parser.parse_args()
# how many places, 1:0-9, 2:0-99, 3:0-999
if args.digits == "1":
size_ = 9
elif args.digits == "3":
size_ = 999
else:
size_ = 99
main(args.type, size_, args.question_count, args.output)