Minor reorganisation

This commit is contained in:
Heiko J Schick
2023-03-23 20:45:11 +01:00
parent 91169befad
commit 1b6611d8c4
+13 -15
View File
@@ -6,27 +6,25 @@ from reportlab.lib.styles import getSampleStyleSheet
from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont from reportlab.pdfbase.ttfonts import TTFont
def format_and_return(a, b, operation, result):
a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ')
return f"{a_str} {operation} {b_str} =", result
def generate_math_problem(): def generate_math_problem():
operation = random.choice(['+', '-', '*', '/']) operation = random.choice(['+', '-', '*', '/'])
a, b = random.randint(0, 20), random.randint(0, 20)
if operation == '+': if operation == '+':
a, b = random.randint(0, 20), random.randint(0, 20) return format_and_return(a, b, operation, a + b)
a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ')
return f"{a_str} {operation} {b_str} =", a + b
elif operation == '-': elif operation == '-':
a, b = random.randint(0, 20), random.randint(0, 20) a, b = max(a, b), min(a, b)
if a < b: return format_and_return(a, b, operation, a - b)
a, b = b, a
a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ')
return f"{a_str} {operation} {b_str} =", a - b
elif operation == '*': elif operation == '*':
a, b = random.randint(0, 20), random.randint(0, 20) return format_and_return(a, b, operation, a * b)
a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ')
return f"{a_str} {operation} {b_str} =", a * b
elif operation == '/': elif operation == '/':
b = random.randint(1, 10) # Divisor b = random.randint(1, 5)
a = b * random.randint(1, 20 // b) # Dividend (as a multiple of the divisor) a = b * random.randint(1, 40 // b)
a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ') return format_and_return(a, b, operation, a // b)
return f"{a_str} {operation} {b_str} =", a // b
def create_brain_jogging_pdf(filename): def create_brain_jogging_pdf(filename):
problems = [[generate_math_problem() for _ in range(4)] for _ in range(20)] problems = [[generate_math_problem() for _ in range(4)] for _ in range(20)]