# The toolkit repair needs reading the docstrings; the reference rewrites each function from its # docstring. Bodies come from the specification in each docstring (same as the public template). import ast, re src = open("src/toolkit.py").read() REF = { "median": " s = sorted(values)\n n = len(s)\n mid = n // 2\n if n % 2 == 1:\n return s[mid]\n return (s[mid - 1] + s[mid]) / 2\n", "clamp": " if x < lo:\n return lo\n if x > hi:\n return hi\n return x\n", "moving_average": " return [sum(values[i:i + k]) / k for i in range(len(values) - k + 1)]\n", "percent_change": " return round((new - old) / old * 100, 2)\n", "chunk": " return [items[i:i + size] for i in range(0, len(items), size)]\n", "is_leap": " return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)\n", "dedupe": " seen = set()\n out = []\n for item in items:\n if item not in seen:\n seen.add(item)\n out.append(item)\n return out\n", "overlap": " return max(0, min(a_end, b_end) - max(a_start, b_start))\n", "word_count": " counts = {}\n for word in re.findall(r\"[a-z]+\", text.lower()):\n counts[word] = counts.get(word, 0) + 1\n return counts\n", "to_seconds": " total = 0\n for part in clock.split(\":\"):\n total = total * 60 + int(part)\n return total\n", } tree = ast.parse(src) lines = src.splitlines(keepends=True) out, last = [], 0 for node in tree.body: if isinstance(node, ast.FunctionDef): doc_end = node.body[0].end_lineno # docstring line out += lines[last:doc_end] out.append(REF[node.name]) last = node.end_lineno out += lines[last:] open("src/toolkit.py", "w").write("".join(out))