File size: 6,367 Bytes
f47988a 3664b25 f47988a 3664b25 f47988a | 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 | // Shared core for visual classes. Runs in the browser (drawing, sliders) and in Node
// (tools/gen_visual_sets.mjs, question generation + checks). No DOM access here.
export function rng(seed) { // mulberry32: reproducible instances from a seed
let a = seed >>> 0;
const next = () => { a |= 0; a = (a + 0x6d2b79f5) | 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
return {
next,
int: (lo, hi) => lo + Math.floor(next() * (hi - lo + 1)),
pick: (arr) => arr[Math.floor(next() * arr.length)],
shuffle: (arr) => { const b = arr.slice(); for (let i = b.length - 1; i > 0; i--) { const j = Math.floor(next() * (i + 1)); [b[i], b[j]] = [b[j], b[i]]; } return b; },
};
}
export const gcd = (a, b) => { a = Math.abs(a); b = Math.abs(b); while (b) [a, b] = [b, a % b]; return a; };
export const lcm = (a, b) => (a / gcd(a, b)) * b;
// Exact fractions: ratios and shares must never pick up floating-point dust.
export class Frac {
constructor(n, d = 1) { if (d === 0) throw new Error("zero denominator"); if (d < 0) { n = -n; d = -d; } const g = gcd(n, d) || 1; this.n = n / g; this.d = d / g; }
static of(x) { return x instanceof Frac ? x : new Frac(x, 1); }
add(o) { o = Frac.of(o); return new Frac(this.n * o.d + o.n * this.d, this.d * o.d); }
sub(o) { o = Frac.of(o); return new Frac(this.n * o.d - o.n * this.d, this.d * o.d); }
mul(o) { o = Frac.of(o); return new Frac(this.n * o.n, this.d * o.d); }
div(o) { o = Frac.of(o); return new Frac(this.n * o.d, this.d * o.n); }
neg() { return new Frac(-this.n, this.d); }
abs() { return new Frac(Math.abs(this.n), this.d); }
sign() { return Math.sign(this.n); }
eq(o) { o = Frac.of(o); return this.n === o.n && this.d === o.d; }
cmp(o) { o = Frac.of(o); return Math.sign(this.n * o.d - o.n * this.d); }
isInt() { return this.d === 1; }
val() { return this.n / this.d; }
toString() { return this.d === 1 ? String(this.n) : `${this.n}/${this.d}`; }
}
// A ratio like 3:2:1 -> shares 3/6, 2/6, 1/6.
export function shares(ratio) { const t = ratio.reduce((a, b) => a + b, 0); return ratio.map((r) => new Frac(r, t)); }
// Fractions -> smallest whole-number ratio (e.g. 1/10, 3/10 -> 1:3). Zeros allowed.
export function toRatio(fracs) {
const L = fracs.reduce((m, f) => lcm(m, f.d), 1); const ints = fracs.map((f) => (f.n * L) / f.d);
const g = ints.reduce((m, x) => gcd(m, x), 0) || 1; return ints.map((x) => x / g);
}
export const ratioStr = (r) => r.join(" : ");
// Indian digit grouping: 1,20,000
export function inr(x) {
const neg = x < 0; let s = String(Math.round(Math.abs(x)));
if (s.length > 3) { const last = s.slice(-3); let rest = s.slice(0, -3); rest = rest.replace(/\B(?=(\d{2})+(?!\d))/g, ","); s = rest + "," + last; }
return (neg ? "−" : "") + s;
}
export const rupee = (x) => `₹${inr(x)}`;
// Check log: every rule an instance or question must satisfy, with a reason when it fails.
export class Checks {
constructor(what) { this.what = what; this.items = []; }
ok(name, cond, detail = "") { this.items.push({ name, pass: !!cond, detail: cond ? "" : detail }); return !!cond; }
get passed() { return this.items.every((i) => i.pass); }
get failures() { return this.items.filter((i) => !i.pass); }
}
// Generic checks applied to every generated question, whatever its class.
export function checkQuestion(q, c = new Checks(q.id)) {
c.ok("has prompt", typeof q.prompt === "string" && q.prompt.length > 20, "prompt missing or too short");
c.ok("has explanation", typeof q.explanation === "string" && q.explanation.length > 30, "explanation missing or too short");
c.ok("tests a named rule", !!q.rule, "no rule tag");
c.ok("has atoms", Array.isArray(q.atom_ids) && q.atom_ids.length > 0, "no atom ids");
if (q.type === "mcq" || q.type === "multi") {
const norm = q.options.map((o) => o.replace(/\s+/g, " ").trim().toLowerCase());
c.ok("4 options", q.options.length === 4, `has ${q.options.length} options`);
c.ok("options distinct", new Set(norm).size === norm.length, "two options read the same");
c.ok("no empty option", norm.every((o) => o.length > 0), "empty option");
if (q.type === "mcq") {
c.ok("answer index valid", Number.isInteger(q.answer) && q.answer >= 0 && q.answer < q.options.length, "bad answer index");
c.ok("every distractor names its misconception", (q.distractors || []).length === q.options.length - 1 && q.distractors.every((d) => d.why), "a distractor has no stated reason");
} else {
c.ok("multi has 1-3 answers", Array.isArray(q.answer) && q.answer.length >= 1 && q.answer.length < q.options.length, "multi answer count wrong");
}
// majority-vote shortcut: if each wrong option changes one feature of the key, the key shares the most
// words/numbers with the others and can be found without knowing anything. The key must not be the unique top scorer.
// Statement-style options ("what is wrong with…") share function words by nature; those are ignored for them.
if (q.type === "mcq") {
const STOP = new Set(["the", "is", "an", "of", "to", "in", "at", "on", "and", "or", "not", "it", "be", "by", "for", "from", "with", "as", "this", "that", "are", "was", "its", "instead", "should", "must"]);
const tok = (o) => new Set((o.toLowerCase().match(/[a-z]+|[0-9][0-9,/]*/g) || []).filter((w) => w.length > 1 && !(q.statementOptions && STOP.has(w))));
const T = q.options.map(tok); const score = T.map((S, i) => [...S].reduce((t, w) => t + T.filter((U, j) => j !== i && U.has(w)).length, 0));
const others = score.filter((_, i) => i !== q.answer);
c.ok("no majority-vote shortcut", score[q.answer] <= Math.max(...others), `key overlap ${score[q.answer]} vs ${Math.max(...others)}`);
}
const lens = q.options.map((o) => o.length);
c.ok("option lengths balanced", Math.max(...lens) <= 3 * Math.min(...lens) + 20, "the key stands out by length");
}
if (q.type === "numeric") {
c.ok("numeric answer finite", Number.isFinite(q.answer), "answer not a number");
c.ok("numeric answer whole rupees/units", Number.isInteger(q.answer) || q.allowFraction, "answer has decimals");
}
return c;
}
|