"""Reorder answer options so correct answers are spread evenly across positions. For every mcq/multi question in syllabus/igcse/quizzes/*.json: - options that refer to other options or to the whole list ("all of the above", "none of these", "both A and B", "A and C only") are pinned in place; - for mcq, the correct option is moved to the least-used position so far in that chapter (ties broken by a seeded random choice), and the other movable options are shuffled; - for multi, the movable options are shuffled. Writes the new order back and records it in syllabus/igcse/quizzes/reviews/OPTION_SHUFFLE_MAP.json as {chapterId: {questionId: [old index for each new position]}}, which is needed to update letter references in explanations and mark schemes and to remap stored student answers. Run once from the project root: python apps/selfcheck-space/tools/shuffle_options.py """ import collections import json import pathlib import random import re ROOT = pathlib.Path(__file__).resolve().parents[3] SRC = ROOT / "syllabus" / "igcse" / "quizzes" MAP = SRC / "reviews" / "OPTION_SHUFFLE_MAP.json" PIN = re.compile(r"\b(all|none|both|neither) (of )?(the )?(above|these|options|statements)\b|\b[A-E] (and|or) [A-E]\b|\bonly [A-E]\b", re.I) def main(): if MAP.exists(): raise SystemExit("Already shuffled (OPTION_SHUFFLE_MAP.json exists); not running twice.") out = {} for f in sorted(SRC.glob("*.json")): d = json.loads(f.read_text(encoding="utf-8")) rng = random.Random(f"stepwise-{d['chapterId']}") used = collections.Counter() cmap = {} for z in d["quizzes"]: for q in z["questions"]: if q.get("type") not in ("mcq", "multi"): continue opts = q["options"] n = len(opts) pinned = {i for i, o in enumerate(opts) if PIN.search(o)} movable = [i for i in range(n) if i not in pinned] if len(movable) < 2: continue perm = list(range(n)) # perm[new_pos] = old_index if q["type"] == "mcq" and int(q["answer"]) in movable: ans = int(q["answer"]) least = min(used[p] for p in movable) target = rng.choice([p for p in movable if used[p] == least]) others = [i for i in movable if i != ans] rng.shuffle(others) slots = [p for p in movable if p != target] perm[target] = ans for p, i in zip(slots, others): perm[p] = i used[target] += 1 else: order = movable[:] rng.shuffle(order) for p, i in zip(movable, order): perm[p] = i if perm == list(range(n)): continue new_of_old = {old: new for new, old in enumerate(perm)} q["options"] = [opts[old] for old in perm] if q["type"] == "mcq": q["answer"] = new_of_old[int(q["answer"])] else: q["answer"] = sorted(new_of_old[int(a)] for a in q["answer"]) cmap[q["id"]] = perm f.write_text(json.dumps(d, ensure_ascii=False, indent=1) + "\n", encoding="utf-8") out[d["chapterId"]] = cmap mc = collections.Counter(int(q["answer"]) for z in d["quizzes"] for q in z["questions"] if q.get("type") == "mcq") print(f"{d['chapterId']}: reordered {len(cmap)} questions; mcq answer positions now {dict(sorted(mc.items()))}") MAP.write_text(json.dumps(out, indent=1), encoding="utf-8") if __name__ == "__main__": main()