summaryrefslogtreecommitdiff
path: root/libremath/specs.py
diff options
context:
space:
mode:
Diffstat (limited to 'libremath/specs.py')
-rw-r--r--libremath/specs.py204
1 files changed, 204 insertions, 0 deletions
diff --git a/libremath/specs.py b/libremath/specs.py
new file mode 100644
index 0000000..e68dce9
--- /dev/null
+++ b/libremath/specs.py
@@ -0,0 +1,204 @@
+"""LibreMath problem specs: curriculum topic, grade, the facts a problem must use, and the
+exact answer, all computed here. The language model only writes the problem and solution
+around these facts; anything it gets wrong is discarded downstream.
+
+Covers grades 1-6: whole-number operations at several scales, place value, rounding,
+comparison, money, elapsed time, measurement and conversion, perimeter/area/volume,
+fractions, decimals, data (mean/range/tables), factors and multiples, order of operations,
+patterns, ratios, percents and multi-step problems with irrelevant information.
+"""
+import math, random
+from fractions import Fraction
+
+R = random.Random()
+
+
+def _r(a, b): return R.randint(a, b)
+
+
+def _t(h, m): # minutes since midnight -> "h:mm AM/PM"
+ h, m = divmod(h * 60 + m, 60)
+ h %= 24
+ return f"{(h % 12) or 12}:{m:02d} {'AM' if h < 12 else 'PM'}"
+
+
+def add_sub():
+ scale = R.choice([20, 100, 1000, 10000])
+ a, b = _r(scale // 10, scale), _r(1, scale)
+ if R.random() < 0.5:
+ return 2 if scale <= 100 else 3, "addition", f"{a} and {b} are combined", a + b
+ a, b = max(a, b), min(a, b)
+ return 2 if scale <= 100 else 3, "subtraction", f"start with {a}, remove or compare {b}", a - b
+
+
+def mult():
+ a, b = R.choice([(_r(2, 12), _r(2, 12)), (_r(11, 99), _r(2, 9)), (_r(12, 99), _r(11, 40))])
+ return 3 if max(a, b) <= 12 else 4, "multiplication", f"{a} groups of {b}", a * b
+
+
+def div():
+ b = _r(2, 12); q = _r(2, 30 if R.random() < 0.7 else 120); rem = _r(0, b - 1) if R.random() < 0.3 else 0
+ a = b * q + rem
+ if rem:
+ kind = R.choice(["quotient", "remainder", "round_up"])
+ ans = {"quotient": q, "remainder": rem, "round_up": q + 1}[kind]
+ how = {"quotient": "how many full groups", "remainder": "how many are left over",
+ "round_up": "how many groups are needed so none are left out"}[kind]
+ return 4, "division with remainder", f"{a} items split into groups of {b}; ask {how}", ans
+ return 3, "division", f"{a} items shared equally into {b} groups", q
+
+
+def place_value():
+ n = _r(1000, 999999); d = R.choice(["thousands", "hundreds", "tens"])
+ v = {"thousands": 1000, "hundreds": 100, "tens": 10}[d]
+ if R.random() < 0.5:
+ return 4, "rounding", f"round {n} to the nearest {d[:-1]}", int(math.floor(n / v + 0.5) * v)
+ return 4, "place value", f"the value of the {d} digit in {n}", (n // v % 10) * v
+
+
+def money():
+ items = _r(2, 4); prices = [Fraction(_r(25, 1999), 100) for _ in range(items)]
+ paid = Fraction(math.ceil(float(sum(prices))) + R.choice([0, 1, 5, 10, 20]), 1)
+ if R.random() < 0.5:
+ return 3, "money", "items costing " + ", ".join(f"${float(p):.2f}" for p in prices) + " are bought; find the total", f"{float(sum(prices)):.2f}"
+ return 4, "making change", "items costing " + ", ".join(f"${float(p):.2f}" for p in prices) + f" are paid for with ${float(paid):.2f}; find the change", f"{float(paid - sum(prices)):.2f}"
+
+
+def elapsed_time():
+ h, m = _r(6, 18), R.choice(range(0, 60, 5)); d = R.choice([15, 20, 25, 35, 40, 45, 50, 70, 85, 95, 110, 135])
+ if R.random() < 0.5:
+ return 3, "elapsed time", f"starts at {_t(h, m)} and lasts {d} minutes; find the end time", _t(h, m + d)
+ return 3, "elapsed time", f"starts at {_t(h, m)} and ends at {_t(h, m + d)}; find how many minutes", d
+
+
+def measurement():
+ kind = R.choice(["m_cm", "kg_g", "l_ml", "ft_in", "hr_min", "yd_ft"])
+ big, small, k = {"m_cm": ("meters", "centimeters", 100), "kg_g": ("kilograms", "grams", 1000),
+ "l_ml": ("liters", "milliliters", 1000), "ft_in": ("feet", "inches", 12),
+ "hr_min": ("hours", "minutes", 60), "yd_ft": ("yards", "feet", 3)}[kind]
+ a, b = _r(1, 12), _r(1, k - 1)
+ return 4, "unit conversion", f"{a} {big} and {b} {small}, expressed in {small}", a * k + b
+
+
+def geometry():
+ kind = R.choice(["perim_rect", "area_rect", "area_l", "volume", "perim_poly", "missing_side"])
+ l, w, h = _r(2, 30), _r(2, 20), _r(2, 12)
+ if kind == "perim_rect": return 3, "perimeter", f"a rectangle {l} by {w}", 2 * (l + w)
+ if kind == "area_rect": return 3, "area", f"a rectangle {l} by {w}", l * w
+ if kind == "volume": return 5, "volume", f"a box {l} by {w} by {h}", l * w * h
+ if kind == "perim_poly":
+ s = [_r(3, 25) for _ in range(_r(3, 6))]
+ return 3, "perimeter", "a shape with sides " + ", ".join(map(str, s)), sum(s)
+ if kind == "missing_side":
+ return 4, "area", f"a rectangle with area {l * w} and one side {l}; find the other side", w
+ a, b = _r(2, 10), _r(2, 10)
+ return 4, "area", f"an L-shape made of a {l} by {w} rectangle and a {a} by {b} rectangle", l * w + a * b
+
+
+def fractions():
+ kind = R.choice(["of_whole", "add_like", "sub_like", "compare", "equiv"])
+ d = R.choice([2, 3, 4, 5, 6, 8, 10, 12])
+ if kind == "of_whole":
+ n = _r(1, d - 1); k = _r(2, 12) * d
+ return 4, "fraction of a number", f"{n}/{d} of {k}", n * k // d
+ a, b = _r(1, d - 1), _r(1, d - 1)
+ if kind == "add_like":
+ s = Fraction(a + b, d); return 4, "adding fractions", f"{a}/{d} plus {b}/{d}, simplified", str(s)
+ if kind == "sub_like":
+ a, b = max(a, b), min(a, b); s = Fraction(a - b, d)
+ return 4, "subtracting fractions", f"{a}/{d} minus {b}/{d}, simplified", str(s)
+ if kind == "equiv":
+ k = _r(2, 6); return 4, "equivalent fractions", f"{a}/{d} written with denominator {d * k}; find the numerator", a * k
+ d2 = R.choice([2, 3, 4, 5, 6, 8]); c = _r(1, d2 - 1)
+ if Fraction(a, d) == Fraction(c, d2): c = (c % (d2 - 1)) + 1
+ big = f"{a}/{d}" if Fraction(a, d) > Fraction(c, d2) else f"{c}/{d2}"
+ return 4, "comparing fractions", f"which is larger, {a}/{d} or {c}/{d2}", big
+
+
+def decimals():
+ a, b = Fraction(_r(10, 9999), 100), Fraction(_r(10, 9999), 100)
+ if R.random() < 0.5: return 5, "decimal addition", f"{float(a):.2f} and {float(b):.2f} combined", f"{float(a + b):.2f}"
+ a, b = max(a, b), min(a, b); return 5, "decimal subtraction", f"{float(a):.2f} minus {float(b):.2f}", f"{float(a - b):.2f}"
+
+
+def data():
+ k = _r(4, 7); xs = [_r(1, 60) for _ in range(k)]
+ kind = R.choice(["mean", "range", "total", "diff"])
+ if kind == "mean":
+ xs[-1] += (-sum(xs)) % k
+ return 5, "mean", "values " + ", ".join(map(str, xs)), sum(xs) // k
+ if kind == "range": return 5, "range", "values " + ", ".join(map(str, xs)), max(xs) - min(xs)
+ if kind == "total": return 3, "reading a table", "a table with values " + ", ".join(map(str, xs)) + "; find the total", sum(xs)
+ return 3, "reading a table", "a table with values " + ", ".join(map(str, xs)) + "; find the difference between the largest and smallest category", max(xs) - min(xs)
+
+
+def factors():
+ kind = R.choice(["lcm", "gcf", "count_factors", "multiple"])
+ a, b = _r(2, 15), _r(2, 15)
+ if kind == "lcm": return 5, "least common multiple", f"two things repeat every {a} and every {b}; when do they first match", a * b // math.gcd(a, b)
+ if kind == "gcf":
+ a, b = a * _r(1, 6), b * _r(1, 6); return 5, "greatest common factor", f"{a} and {b} split into the largest equal groups", math.gcd(a, b)
+ if kind == "count_factors":
+ n = _r(6, 100); return 4, "factors", f"how many factors {n} has", sum(1 for i in range(1, n + 1) if n % i == 0)
+ n, k = _r(3, 12), _r(4, 15); return 3, "multiples", f"the {k}th multiple of {n}", n * k
+
+
+def order_ops():
+ a, b, c, d = _r(2, 20), _r(2, 12), _r(2, 12), _r(1, 30)
+ expr = R.choice([f"{a} + {b} × {c}", f"({a} + {b}) × {c}", f"{a} × {b} - {d}", f"{a * b} ÷ {b} + {c}", f"{a} + {b} × {c} - {d}"])
+ val = eval(expr.replace("×", "*").replace("÷", "//"))
+ return 5, "order of operations", f"the expression {expr}", val
+
+
+def patterns():
+ s, d, n = _r(1, 80), R.choice([2, 3, 4, 5, 6, 7, 9, 11, 12, 15, 25]), _r(5, 10)
+ if R.random() < 0.3: d = -d; s += abs(d) * (n + 2)
+ seq = [s + d * i for i in range(4)]
+ return 3, "number patterns", "the sequence " + ", ".join(map(str, seq)) + f"; find term number {n}", s + d * (n - 1)
+
+
+def ratio_percent():
+ kind = R.choice(["ratio", "percent", "unit_rate", "discount"])
+ if kind == "ratio":
+ a, b, k = _r(1, 9), _r(1, 9), _r(2, 12); return 6, "ratios", f"a {a}:{b} ratio, with {a * k} of the first kind; find the second", b * k
+ if kind == "percent":
+ p = R.choice([10, 20, 25, 30, 40, 50, 60, 75, 80]); n = R.choice([20, 40, 60, 80, 120, 200, 240, 400])
+ return 6, "percent of a number", f"{p}% of {n}", p * n // 100
+ if kind == "unit_rate":
+ r, t = _r(2, 15), _r(2, 12); return 6, "unit rate", f"{r * t} done in {t} equal parts; find the amount per part", r
+ p = R.choice([10, 20, 25, 50]); price = R.choice([20, 40, 60, 80, 100, 120])
+ return 6, "discounts", f"a ${price} item with {p}% off; find the sale price", price - price * p // 100
+
+
+def multi_step():
+ a, b, c, d = _r(3, 12), _r(4, 25), _r(5, 60), _r(2, 9)
+ form = R.choice([(f"{a} packs of {b}, then {c} more are added", a * b + c),
+ (f"{a} packs of {b}, then {c} are given away", a * b - c if a * b > c else a * b + c),
+ (f"{a * d} items split into {d} equal groups, then each group gets {b} more", a + b),
+ (f"saving ${b} a week for {a} weeks, then spending ${c}", a * b - c if a * b > c else a * b + c)])
+ return R.choice([3, 4, 5]), "multi-step", form[0] + "; include one irrelevant number", form[1]
+
+
+TOPICS = [(add_sub, 14), (mult, 9), (div, 8), (place_value, 4), (money, 7), (elapsed_time, 5), (measurement, 6),
+ (geometry, 8), (fractions, 8), (decimals, 4), (data, 5), (factors, 5), (order_ops, 3), (patterns, 4),
+ (ratio_percent, 5), (multi_step, 9)]
+
+
+def sample(rng=None):
+ global R
+ if rng is not None: R = rng
+ fn = R.choices([t for t, _ in TOPICS], weights=[w for _, w in TOPICS])[0]
+ grade, topic, facts, ans = fn()
+ return {"grade": grade, "topic": topic, "facts": facts, "answer": str(ans)}
+
+
+def sample_pair(rng=None):
+ """Two specs from the same topic family, so one scenario can hold both questions."""
+ global R
+ if rng is not None: R = rng
+ fn = R.choices([t for t, _ in TOPICS], weights=[w for _, w in TOPICS])[0]
+ out = []
+ for _ in range(2):
+ grade, topic, facts, ans = fn()
+ out.append({"grade": grade, "topic": topic, "facts": facts, "answer": str(ans)})
+ return tuple(out)