Skip to content

Instantly share code, notes, and snippets.

@dharmx
Last active February 23, 2024 19:21
Show Gist options
  • Save dharmx/cc25de779a4e3f49f851d78fdd964212 to your computer and use it in GitHub Desktop.
Save dharmx/cc25de779a4e3f49f851d78fdd964212 to your computer and use it in GitHub Desktop.
Generate problem set for boosting mental calculations.
{
"multiply": {
"two": 20,
"three": 10
},
"add": {
"two": 10,
"three": 20,
"four": 10,
"five": 5
},
"alpha": {
"subtract": 20,
"add": 20,
"multiply": 10
},
"tricks": {
"99": 5,
"999": 5,
"9999": 5,
"99999": 5,
"x11": 5,
"x25": 5,
"x125": 5
},
"roots": 10,
"square": 32,
"cube": 32
}
#!/usr/bin/env python
# http://www.cut-the-knot.org/arithmetic.shtml
import pathlib
import sys
from random import randint, shuffle
from json import dumps, loads
from math import prod, sqrt
from operator import sub
from fpdf import FPDF
# Settings {{{
config = loads(pathlib.Path("./config.json").read_text())
problem = {
"multiply": { "two": [], "three": [] },
"add": { "two": [], "three": [], "four": [], "five": [] },
"alpha": { "normal": [], "subtract": [], "add": [], "multiply": [] },
"tricks": { "99": [], "999": [], "9999": [], "99999": [], "x11": [], "x25": [], "x125": [] },
"roots": [],
"square": [ 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 ],
"cube": [ 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 ],
}
answer = {
"multiply": { "two": [], "three": [] },
"add": { "two": [], "three": [], "four": [], "five": [] },
"roots": [],
"square": [],
"cube": [],
"alpha": { "normal": [], "subtract": [], "add": [], "multiply": [] },
"tricks": { "99": [], "999": [], "9999": [], "99999": [], "x11": [], "x25": [], "x125": [] },
}
settings = {"config": config, "problem": problem, "answer": answer}
# }}}
# Creating Problem {{{
for index in range(config["multiply"]["two"]):
problem["multiply"]["two"].append([randint(10, 99), randint(10, 99)])
answer["multiply"]["two"].append(prod(problem["multiply"]["two"][index]))
for index in range(config["multiply"]["three"]):
problem["multiply"]["three"].append([randint(100, 999), randint(100, 999)])
answer["multiply"]["three"].append(prod(problem["multiply"]["three"][index]))
for index in range(config["add"]["two"]):
problem["add"]["two"].append([randint(10, 99), randint(10, 99)])
answer["add"]["two"].append(sum(problem["add"]["two"][index]))
for index in range(config["add"]["three"]):
problem["add"]["three"].append([randint(100, 999), randint(100, 999)])
answer["add"]["three"].append(sum(problem["add"]["three"][index]))
for index in range(config["add"]["four"]):
problem["add"]["four"].append([randint(1000, 9999), randint(1000, 9999)])
answer["add"]["four"].append(sum(problem["add"]["four"][index]))
for index in range(config["add"]["five"]):
problem["add"]["five"].append([randint(10000, 99999), randint(10000, 99999)])
answer["add"]["five"].append(sum(problem["add"]["five"][index]))
for index in range(config["tricks"]["99"]):
problem["tricks"]["99"].append([99, randint(10, 999)])
answer["tricks"]["99"].append(prod(problem["tricks"]["99"][index]))
for index in range(config["tricks"]["999"]):
problem["tricks"]["999"].append([999, randint(10, 999)])
answer["tricks"]["999"].append(prod(problem["tricks"]["999"][index]))
for index in range(config["tricks"]["9999"]):
problem["tricks"]["9999"].append([9999, randint(10, 999)])
answer["tricks"]["9999"].append(prod(problem["tricks"]["9999"][index]))
for index in range(config["tricks"]["99999"]):
problem["tricks"]["99999"].append([99999, randint(10, 999)])
answer["tricks"]["99999"].append(prod(problem["tricks"]["99999"][index]))
for index in range(config["tricks"]["x11"]):
problem["tricks"]["x11"].append([randint(10, 99999), 11])
answer["tricks"]["x11"].append(prod(problem["tricks"]["x11"][index]))
for index in range(config["tricks"]["x25"]):
problem["tricks"]["x25"].append([randint(10, 9999), 25])
answer["tricks"]["x25"].append(prod(problem["tricks"]["x25"][index]))
for index in range(config["tricks"]["x125"]):
problem["tricks"]["x125"].append([randint(10, 999), 125])
answer["tricks"]["x125"].append(prod(problem["tricks"]["x125"][index]))
alpha = [chr(symbol) for symbol in range(65, 91)]
for index in range(config["alpha"]["multiply"]):
problem["alpha"]["multiply"].append([alpha[randint(0, 25)], alpha[randint(0, 25)]])
answer["alpha"]["multiply"].append(prod([ord(problem["alpha"]["multiply"][index][0]) - 65 + 1, ord(problem["alpha"]["multiply"][index][1]) - 65 + 1]))
for index in range(config["alpha"]["add"]):
problem["alpha"]["add"].append([alpha[randint(0, 25)], alpha[randint(0, 25)]])
answer["alpha"]["add"].append(sum([ord(problem["alpha"]["add"][index][0]) - 65 + 1, ord(problem["alpha"]["add"][index][1]) - 65 + 1, ]))
alpha_multiply = problem["alpha"]["multiply"]
for index in range(config["alpha"]["subtract"]):
problem["alpha"]["subtract"].append([alpha[randint(0, 25)], alpha[randint(0, 25)]])
answer["alpha"]["subtract"].append(sub(ord(problem["alpha"]["subtract"][index][0]) - 65 + 1, ord(problem["alpha"]["subtract"][index][1]) - 65 + 1))
shuffle(problem["square"])
shuffle(problem["cube"])
problem["square"] = problem["square"][: config["square"]]
problem["cube"] = problem["cube"][: config["cube"]]
answer["square"] = [item**2 for item in problem["square"]]
answer["cube"] = [item**3 for item in problem["cube"]]
for index in range(config["roots"]):
problem["roots"].append(randint(10, 999))
answer["roots"].append(round(sqrt(problem["roots"][index]), 2))
# }}}
# PDF Class {{{
class PDF(FPDF):
size_header = 12
size_footer = 12
size_title = 15
size_body = 15
level = 1
def __init__(self, orientation, format) -> None:
super().__init__(orientation=orientation, format=format)
self.add_font("Normal", fname="/home/dharmx/.local/share/fonts/Dosis/Dosis-Regular.ttf", uni=True)
self.add_font("Bold", fname="/home/dharmx/.local/share/fonts/Dosis/Dosis-Bold.ttf", uni=True)
self.add_font("Mono", fname="/home/dharmx/.local/share/fonts/Iosevka/Iosevka Nerd Font Complete.ttf", uni=True)
self.set_margins(10, 10, 10)
self.set_auto_page_break(True)
self.add_page()
def chapter_title(self, label):
self.set_font("Bold", "", self.size_title)
self.cell(0, 0, f"{self.level} {label}", False, 1, "L", False)
self.level += 1
self.ln(5)
def chapter_body(self, contents):
self.set_font("Mono", "", self.size_body)
self.multi_cell(0, 5, contents, False, "L", False, False)
self.ln(10)
def print_chapter(self, title, contents):
self.chapter_title(title)
self.chapter_body(contents)
# }}}
pdf = PDF(orientation="P", format="A4")
pdf.print_chapter("Multiplication 2", " ".join([f"{item[0]} × {item[1]}" for item in problem["multiply"]["two"]]))
pdf.print_chapter("Multiplication 3", " ".join([f"{item[0]} × {item[1]}" for item in problem["multiply"]["three"]]))
pdf.print_chapter("Addition 2", " ".join([f"{item[0]} + {item[1]}" for item in problem["add"]["two"]]))
pdf.print_chapter("Addition 3", " ".join([f"{item[0]} + {item[1]}" for item in problem["add"]["three"]]))
pdf.print_chapter("Addition 4", " ".join([f"{item[0]} + {item[1]}" for item in problem["add"]["four"]]))
pdf.print_chapter("Addition 5", " ".join([f"{item[0]} + {item[1]}" for item in problem["add"]["five"]]))
pdf.print_chapter("Alphabetical Multiplication", " ".join([f"{item[0]} × {item[1]}" for item in problem["alpha"]["multiply"]]))
pdf.print_chapter("Alphabetical Addition", " ".join([f"{item[0]} + {item[1]}" for item in problem["alpha"]["add"]]))
pdf.print_chapter("Alphabetical Subtraction", " ".join([f"{item[0]} - {item[1]}" for item in problem["alpha"]["subtract"]]))
pdf.print_chapter("Multiplication 99", " ".join([f"{item[0]} × {item[1]}" for item in problem["tricks"]["99"]]))
pdf.print_chapter("Multiplication 999", " ".join([f"{item[0]} × {item[1]}" for item in problem["tricks"]["999"]]))
pdf.print_chapter("Multiplication 9999", " ".join([f"{item[0]} × {item[1]}" for item in problem["tricks"]["9999"]]))
pdf.print_chapter("Multiplication 99999", " ".join([f"{item[0]} × {item[1]}" for item in problem["tricks"]["99999"]]))
pdf.print_chapter("Multiplication 11", " ".join([f"{item[0]} × {item[1]}" for item in problem["tricks"]["x11"]]))
pdf.print_chapter("Multiplication 25", " ".join([f"{item[0]} × {item[1]}" for item in problem["tricks"]["x25"]]))
pdf.print_chapter("Multiplication 125", " ".join([f"{item[0]} × {item[1]}" for item in problem["tricks"]["x125"]]))
pdf.print_chapter("Roots", " ".join([f"√{item}" for item in problem["roots"]]))
pdf.print_chapter("Squares", " ".join([f"{item}²" for item in problem["square"]]))
pdf.print_chapter("Cubes", " ".join([f"{item}³" for item in problem["cube"]]))
pdf.ln(15)
pdf.add_page()
pdf.set_auto_page_break(True)
pdf.level = 1
pdf.set_font("Bold", "", pdf.size_title + 5)
pdf.cell(0, 0, "Answers", False, 2, "L", False)
pdf.ln(15)
pdf.level = 1
pdf.print_chapter("Multiplication 2", " ".join(map(str, answer["multiply"]["two"])))
pdf.print_chapter("Multiplication 3", " ".join(map(str, answer["multiply"]["three"])))
pdf.print_chapter("Addition 2", " ".join(map(str, answer["add"]["two"])))
pdf.print_chapter("Addition 3", " ".join(map(str, answer["add"]["three"])))
pdf.print_chapter("Addition 4", " ".join(map(str, answer["add"]["four"])))
pdf.print_chapter("Addition 5", " ".join(map(str, answer["add"]["five"])))
pdf.print_chapter("Alphabetical Multiplication", " ".join(map(str, answer["alpha"]["multiply"])))
pdf.print_chapter("Alphabetical Addition", " ".join(map(str, answer["alpha"]["add"])))
pdf.print_chapter("Alphabetical Subtraction", " ".join(map(str, answer["alpha"]["subtract"])))
pdf.print_chapter("Multiplication 99", " ".join(map(str, answer["tricks"]["99"])))
pdf.print_chapter("Multiplication 999", " ".join(map(str, answer["tricks"]["999"])))
pdf.print_chapter("Multiplication 9999", " ".join(map(str, answer["tricks"]["9999"])))
pdf.ln(15)
pdf.print_chapter("Multiplication 99999", " ".join(map(str, answer["tricks"]["99999"])))
pdf.print_chapter("Multiplication 11", " ".join(map(str, answer["tricks"]["x11"])))
pdf.print_chapter("Multiplication 25", " ".join(map(str, answer["tricks"]["x25"])))
pdf.print_chapter("Multiplication 125", " ".join(map(str, answer["tricks"]["x125"])))
pdf.print_chapter("Roots", " ".join(map(str, answer["roots"])))
pdf.print_chapter("Squares", " ".join(map(str, answer["square"])))
pdf.print_chapter("Cubes", " ".join(map(str, answer["cube"])))
if len(sys.argv) < 2:
print(f"NEEDS ARGS\n{sys.argv[0]} --[all|print|dump|help]")
sys.exit(1)
elif sys.argv[1] == "--print":
pdf.output("vedic.pdf")
elif sys.argv[1] == "--dump":
print(dumps(settings))
elif sys.argv[1] == "--all":
pdf.output("vedic.pdf")
print(dumps(settings))
elif sys.argv[1] == "--help":
print(f"{sys.argv[0]} --[all|print|dump|help]")
@dharmx
Copy link
Author

dharmx commented Feb 23, 2024

vedic-0
vedic-1
vedic-2
vedic-3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment