Generate variant practice problems and verify answers
Get AI variants of a math problem you got wrong, then recompute every answer key from a check line with your own script, without running AI-written code.
When you practice with AI-made variants of a problem you got wrong, a wrong answer key or an ugly fraction breaks the session. The model writes the story and the difficulty steps well, but the numeric answer still needs to be recomputed separately.
This post does not run Python written by the model. Running model-written code as-is lets that code do whatever it does (read or delete files, for example) on your computer without you reading it first. Instead the model gives one line of numbers per problem, and a script you control recomputes the answer from those numbers. Recomputing figures the model stated follows the same method as AI stated numbers recomputation.
[1] Define the original problem. This example is a classic word problem: two people walking toward each other.
[Original Problem]Person A and Person B start walking toward each other at the same time from two locations that are 18 km apart. Person A walks at 4 km/h and Person B walks at 5 km/h. How many hours after departure do they meet?- Stated Answer: 2 hours[2] Build the prompt. Ask for 3 difficulty levels, require positive integer answers, and require one Check: line per problem in a fixed format. Specifying the exact output format follows OpenAI's prompt engineering guide. The prompt says plainly not to write code.
Analyze the following incorrect math practice problem, and generate 3 variant practice problems of the same type across different difficulty levels (Level 1: Basic parameter change, Level 2: Different departure times, Level 3: Speed change mid-journey). [Original Problem]{Problem statement and answer} [Generation Rules]1. For each problem, clearly structure: Difficulty Level, Problem Statement, Step-by-Step Solution, and Final Answer.2. Ensure all problem conditions are designed so that the final answer is a positive integer (e.g., exact whole hours or km).3. Directly below each solution, write one line that starts with "Check:" in exactly the format below. Use plain integers without units. - Level 1: Check: type=1, distance=<km>, speed_a=<km/h>, speed_b=<km/h>, answer=<hours until they meet> - Level 2: Check: type=2, distance=<km>, speed_a=<km/h>, speed_b=<km/h>, a_head_start=<hours>, answer=<hours after B departs until they meet> - Level 3: Check: type=3, distance=<km>, speed_a=<km/h>, speed_b=<km/h>, change_time=<hours>, speed_a_after=<km/h>, answer=<total hours from departure until they meet>4. Do not write any program code.5. Do not run any external tools or commands; output the entire response directly as Markdown text.[3] Run the prompt.
$ claude -p --model claude-sonnet-5-5 --max-turns 1 --tools "" --setting-sources "" --strict-mcp-config < run/en.1.prompt.txt > run/en.1.out.txtThe runs were made from an empty folder outside the repository (run from a project folder, instruction files in parent folders are read too). The opening analysis of the original problem is shortened here; the three variants follow.
…## Level 1: Basic Parameter Change **Problem Statement**Person A and Person B start walking toward each other at the same time from two locations 36 km apart. Person A walks at 7 km/h and Person B walks at 5 km/h. How many hours after departure do they meet? **Step-by-Step Solution**1. Combined speed = 7 + 5 = 12 km/h.2. Time = distance ÷ combined speed = 36 ÷ 12 = 3 hours.3. Verification: A walks 7 × 3 = 21 km and B walks 5 × 3 = 15 km, and 21 + 15 = 36 km. Check: type=1, distance=36, speed_a=7, speed_b=5, answer=3 **Final Answer:** 3 hours --- ## Level 2: Different Departure Times **Problem Statement**Two locations are 38 km apart. Person A leaves first, walking at 4 km/h toward Person B. Exactly 2 hours later, Person B leaves the other location and walks toward A at 6 km/h. How many hours after B departs do they meet? **Step-by-Step Solution**1. In the 2-hour head start, A walks 4 × 2 = 8 km.2. The remaining gap when B departs is 38 − 8 = 30 km.3. Combined speed = 4 + 6 = 10 km/h.4. Time after B departs = 30 ÷ 10 = 3 hours.5. Verification: A walks 4 × (2 + 3) = 20 km and B walks 6 × 3 = 18 km, and 20 + 18 = 38 km. Check: type=2, distance=38, speed_a=4, speed_b=6, a_head_start=2, answer=3 **Final Answer:** 3 hours after B departs --- ## Level 3: Speed Change Mid-Journey **Problem Statement**Person A and Person B start walking toward each other at the same time from two locations 50 km apart. A starts at 4 km/h and B walks at a constant 6 km/h. After 2 hours, A speeds up to 9 km/h and keeps that pace. How many hours after departure do they meet? **Step-by-Step Solution**1. First 2 hours: combined speed = 4 + 6 = 10 km/h, so they cover 10 × 2 = 20 km. Since 20 < 50, they have not met yet.2. Remaining gap = 50 − 20 = 30 km.3. After the change: combined speed = 9 + 6 = 15 km/h.4. Additional time = 30 ÷ 15 = 2 hours.5. Total time = 2 + 2 = 4 hours.6. Verification: A walks 4 × 2 + 9 × 2 = 26 km and B walks 6 × 4 = 24 km, and 26 + 24 = 50 km. Check: type=3, distance=50, speed_a=4, speed_b=6, change_time=2, speed_a_after=9, answer=4 **Final Answer:** 4 hours after departure[4] Recompute the answer keys with a script. check_problems.py reads the three Check: lines and recomputes each answer with a formula for its type: distance ÷ (speed A + speed B) for type 1; the same after subtracting A's head-start distance for type 2; for type 3, subtract the part before the speed change, divide the rest by the new combined speed, and add the first stretch's time. Then it checks that:
- the computed value equals the stated answer and is a positive integer
- the distances walked by the two people add up to the total distance
- every number on the check line appears in that problem's statement, and the part of the
Final Answerline before any parenthesis contains the stated answer (numbers in a parenthetical check do not count) - there are exactly 3 check lines and no key repeats
The script reads strings only; it never runs anything from the model output.
#!/usr/bin/env python3"""usage: check_problems.py <out.txt>Recomputes each practice problem from its "Check:" line. Never runs code from the model output."""import re, sysfrom fractions import Fraction KEYS = {"거리": "d", "A속력": "a", "B속력": "b", "A선출발": "h", "변경시각": "c", "A변경속력": "a2", "정답": "ans", "distance": "d", "speed_a": "a", "speed_b": "b", "a_head_start": "h", "change_time": "c", "speed_a_after": "a2", "answer": "ans"}NEED = {1: "d a b ans", 2: "d a b h ans", 3: "d a b c a2 ans"} text = open(sys.argv[1], encoding="utf-8").read()checks = list(re.finditer(r"^(?:검산용|Check):\s*(.+)$", text, re.M))errors = []if len(checks) != 3: errors.append(f"expected 3 check lines, found {len(checks)}") for n, m in enumerate(checks, 1): kv = {} for part in m.group(1).split(","): k, _, v = part.strip().partition("=") if k.strip() in kv or k.strip() == "": errors.append(f"problem {n}: duplicate or empty key {k!r}") kv[k.strip()] = v.strip() if not re.fullmatch(r"[123]", kv.get("유형", kv.get("type", ""))) or int(kv.get("유형", kv.get("type"))) != n: errors.append(f"problem {n}: type missing or not {n}"); continue p = {KEYS[k]: v for k, v in kv.items() if k in KEYS} if sorted(p) != sorted(NEED[n].split()) or not all(re.fullmatch(r"[1-9]\d*", v) for v in p.values()): errors.append(f"problem {n}: keys or values wrong: {p}"); continue d, a, b, ans = (int(p[k]) for k in ("d", "a", "b", "ans")) if n == 1: calc = Fraction(d, a + b); walked = (a + b) * calc elif n == 2: h = int(p["h"]); rem = d - a * h if rem <= 0: errors.append(f"problem 2: A already past the distance"); continue calc = Fraction(rem, a + b); walked = a * (h + calc) + b * calc else: c, a2 = int(p["c"]), int(p["a2"]); rem = d - (a + b) * c if rem <= 0: errors.append("problem 3: they meet before the speed change"); continue calc = c + Fraction(rem, a2 + b); walked = a * c + a2 * (calc - c) + b * calc # the block of this problem: between the surrounding '---' lines start = text.rfind("\n---", 0, m.start()); end = text.find("\n---", m.end()) block = text[start if start >= 0 else 0: end if end >= 0 else len(text)] stmt = re.split(r"풀이|Solution", block, maxsplit=1)[0] nums_stmt = set(re.findall(r"(?<![\d.])\d+(?![\d.])", stmt)) missing = [k for k in p if k != "ans" and p[k] not in nums_stmt] fin = [l for l in block.splitlines() if re.search(r"최종 정답|Final Answer", l)] head = re.split(r"[((]", re.split(r"최종 정답|Final Answer", fin[-1])[-1])[0] if fin else "" fin_ok = bool(fin) and str(ans) in re.findall(r"(?<![\d.])\d+(?![\d.])", head) bad = [] if calc != ans: bad.append(f"calc {calc} != stated {ans}") if calc.denominator != 1: bad.append("not an integer") if walked != d: bad.append(f"distance walked {walked} != {d}") if missing: bad.append(f"numbers not in problem statement: {missing}") if not fin_ok: bad.append("final-answer line lacks the stated answer") print(("FAIL" if bad else "OK "), f"problem {n}: calc={calc} stated={ans}", "; ".join(bad)) errors += [f"problem {n}: {x}" for x in bad] if errors: print(f"{len(errors)} problem(s)") for e in errors: print(" -", e) sys.exit(1)print("3 problems OK")Run it on both outputs. Both passed.
$ python run/check_problems.py run/en.1.out.txtOK problem 1: calc=3 stated=3 OK problem 2: calc=3 stated=3 OK problem 3: calc=4 stated=4 3 problems OK$ python run/check_problems.py run/en.2.out.txtOK problem 1: calc=3 stated=3 OK problem 2: calc=4 stated=4 OK problem 3: calc=4 stated=4 3 problems OKSeven hand-edited inputs (answer changed to 4, a distance that gives a fraction, problem 3 deleted, only the final-answer line changed, only the value before the parenthetical in the final-answer line changed to 6, only a speed in the statement changed, a repeated key) all exit with code 1. The first is shown below (not model output).
$ python run/check_problems.py run/bad-answer.en.txtFAIL problem 1: calc=3 stated=4 calc 3 != stated 4; final-answer line lacks the stated answerOK problem 2: calc=3 stated=3 OK problem 3: calc=4 stated=4 2 problem(s) - problem 1: calc 3 != stated 4 - problem 1: final-answer line lacks the stated answerWhat it cannot catch: the check line is numbers the model copied itself, so the script cannot tell whether the statement says the same thing (who leaves first, whose speed changes). A number passes if it appears anywhere in the statement.
[5] Three things to check by hand.
1. Statement matches the check line: read who leaves first and whose speed changes, and compare with the check line. 2. Structure of the levels: Level 1 changes numbers, Level 2 adds staggered departures, Level 3 changes speed mid-route. 3. Plausible numbers: no walking speed of 50 km/h and no negative distances.
What the two English runs showed: all three problems had integer answers in both runs and the distance check held. Run 1 used 36, 38 and 50 km; run 2 used 36, 46 and 50 km. The prompt did not include a student's wrong answer, so the common mistakes the model lists are its guesses; paste the student's own working into the prompt if you want the actual cause.