|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +convert_single_question.py |
| 4 | +────────────────────────── |
| 5 | +Paste ONE question dict into QUESTION_DICT and run: |
| 6 | +
|
| 7 | + python utils/convert_single_question.py |
| 8 | +
|
| 9 | +The script splits it into: |
| 10 | +
|
| 11 | +questions/<id>_<slug>/ |
| 12 | + ├─ meta.json |
| 13 | + ├─ description.md |
| 14 | + ├─ learn.md |
| 15 | + ├─ starter_code.py |
| 16 | + ├─ solution.py |
| 17 | + ├─ example.json |
| 18 | + ├─ tests.json |
| 19 | + ├─ tinygrad/ (optional) |
| 20 | + └─ pytorch/ (optional) |
| 21 | +""" |
| 22 | + |
| 23 | +import base64 |
| 24 | +import json |
| 25 | +import pathlib |
| 26 | +import re |
| 27 | +from typing import Any, Dict |
| 28 | + |
| 29 | +# ── 1️⃣ EDIT YOUR QUESTION HERE ──────────────────────────────────────────── |
| 30 | +QUESTION_DICT: Dict[str, Any] = { |
| 31 | + "id": "140", |
| 32 | + "title": "Bernoulli Naive Bayes Classifier", |
| 33 | + "difficulty": "medium", |
| 34 | + "category": "Machine Learning", |
| 35 | + "description": "Write a Python class …", |
| 36 | + "learn_section": "# Learn section …", |
| 37 | + "starter_code": "import numpy as np\n\nclass NaiveBayes():\n pass", |
| 38 | + "solution": "import numpy as np\n\nclass NaiveBayes():\n pass", |
| 39 | + "example": { |
| 40 | + "input": "…", |
| 41 | + "output": "[1]", |
| 42 | + "reasoning": "…" |
| 43 | + }, |
| 44 | + "test_cases": [ |
| 45 | + {"test": "print(1+1)", "expected_output": "2"} |
| 46 | + ], |
| 47 | + "video": "", |
| 48 | + "likes": "0", |
| 49 | + "dislikes": "0", |
| 50 | + "contributor": [ |
| 51 | + {"name": "Moe Chabot", "profile_link": "https://github.com/moe18"} |
| 52 | + ] |
| 53 | + # Optional extras: |
| 54 | + # "marimo_link": "https://…", |
| 55 | + # "tinygrad_difficulty": "medium", |
| 56 | + # "tinygrad_starter_code": "BASE64…", |
| 57 | + # "tinygrad_solution": "BASE64…", |
| 58 | + # "tinygrad_test_cases": [], |
| 59 | + # "pytorch_difficulty": "medium", |
| 60 | + # "pytorch_starter_code": "BASE64…", |
| 61 | + # "pytorch_solution": "BASE64…", |
| 62 | + # "pytorch_test_cases": [] |
| 63 | +} |
| 64 | +# ──────────────────────────────────────────────────────────────────────────── |
| 65 | + |
| 66 | + |
| 67 | +# ---------- helpers --------------------------------------------------------- |
| 68 | +def slugify(text: str) -> str: |
| 69 | + text = re.sub(r"[^0-9A-Za-z]+", "-", text.lower()) |
| 70 | + return re.sub(r"-{2,}", "-", text).strip("-")[:50] |
| 71 | + |
| 72 | + |
| 73 | +def maybe_b64(s: str) -> str: |
| 74 | + try: |
| 75 | + if len(s) % 4 == 0 and re.fullmatch(r"[0-9A-Za-z+/=\n\r]+", s): |
| 76 | + return base64.b64decode(s).decode("utf-8") |
| 77 | + except Exception: |
| 78 | + pass |
| 79 | + return s |
| 80 | + |
| 81 | + |
| 82 | +def write_text(path: pathlib.Path, content: str) -> None: |
| 83 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 84 | + path.write_text(content.rstrip("\n") + "\n", encoding="utf-8") |
| 85 | + |
| 86 | + |
| 87 | +def write_json(path: pathlib.Path, obj: Any) -> None: |
| 88 | + write_text(path, json.dumps(obj, indent=2, ensure_ascii=False)) |
| 89 | + |
| 90 | + |
| 91 | +# ---------- converter ------------------------------------------------------- |
| 92 | +def convert_one(q: Dict[str, Any]) -> None: |
| 93 | + folder = pathlib.Path("questions") / f"{q['id']}_{slugify(q['title'])}" |
| 94 | + folder.mkdir(parents=True, exist_ok=True) |
| 95 | + |
| 96 | + # meta.json |
| 97 | + meta = { |
| 98 | + "id": q["id"], |
| 99 | + "title": q["title"], |
| 100 | + "difficulty": q["difficulty"], |
| 101 | + "category": q["category"], |
| 102 | + "video": q.get("video", ""), |
| 103 | + "likes": q.get("likes", "0"), |
| 104 | + "dislikes": q.get("dislikes", "0"), |
| 105 | + "contributor": q.get("contributor", []), |
| 106 | + } |
| 107 | + for opt in ("tinygrad_difficulty", "pytorch_difficulty", "marimo_link"): |
| 108 | + if opt in q: |
| 109 | + meta[opt] = q[opt] |
| 110 | + write_json(folder / "meta.json", meta) |
| 111 | + |
| 112 | + # core files |
| 113 | + write_text(folder / "description.md", q["description"]) |
| 114 | + write_text(folder / "learn.md", q["learn_section"]) |
| 115 | + write_text(folder / "starter_code.py", q["starter_code"]) |
| 116 | + write_text(folder / "solution.py", q["solution"]) |
| 117 | + write_json(folder / "example.json", q["example"]) |
| 118 | + write_json(folder / "tests.json", q["test_cases"]) |
| 119 | + |
| 120 | + # optional language-specific extras |
| 121 | + for lang in ("tinygrad", "pytorch"): |
| 122 | + sc, so, tc = (f"{lang}_starter_code", f"{lang}_solution", f"{lang}_test_cases") |
| 123 | + if any(k in q for k in (sc, so, tc)): |
| 124 | + sub = folder / lang |
| 125 | + if sc in q: |
| 126 | + write_text(sub / "starter_code.py", maybe_b64(q[sc])) |
| 127 | + if so in q: |
| 128 | + write_text(sub / "solution.py", maybe_b64(q[so])) |
| 129 | + if tc in q: |
| 130 | + write_json(sub / "tests.json", q[tc]) |
| 131 | + |
| 132 | + # success message (relative if possible) |
| 133 | + try: |
| 134 | + rel = folder.relative_to(pathlib.Path.cwd()) |
| 135 | + except ValueError: |
| 136 | + rel = folder |
| 137 | + print(f"✓ Created {rel}") |
| 138 | + |
| 139 | + |
| 140 | +# ---------- main ------------------------------------------------------------ |
| 141 | +def main() -> None: |
| 142 | + convert_one(QUESTION_DICT) |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments