"""Turn a verified Problem into the user prompt and the assistant reply (English or Finnish).""" from __future__ import annotations import json from .base import Problem, fmt_call from .runner import run_script ASK = { "en": {"solve": ["Write the solution in Python.", "Solve this in Python.", "Implement it in Python."], "explain": ["Give an efficient Python solution and briefly explain the idea.", "Solve it in Python and explain your approach and its complexity."], "tests": ["Write the solution in Python and include a few tests.", "Implement it in Python, then add assert-based tests that cover the edge cases."]}, "fi": {"solve": ["Kirjoita ratkaisu Pythonilla.", "Ratkaise tämä Pythonilla.", "Toteuta se Pythonilla."], "explain": ["Anna tehokas Python-ratkaisu ja selitä idea lyhyesti.", "Ratkaise Pythonilla ja selitä lähestymistapa ja aikavaativuus."], "tests": ["Kirjoita ratkaisu Pythonilla ja lisää muutama testi.", "Toteuta Pythonilla ja lisää assert-testit, jotka kattavat reunatapaukset."]}, } WORD = {"en": {"example": "Example", "examples": "Examples", "complexity": "Complexity", "tests": "Tests", "why": "Why it works:", "pass": "All of these assertions pass against the solution above.", "signature": "Signature:"}, "fi": {"example": "Esimerkki", "examples": "Esimerkit", "complexity": "Vaativuus", "tests": "Testit", "why": "Miksi tämä toimii:", "pass": "Kaikki nämä assertit menevät läpi yllä olevalla ratkaisulla.", "signature": "Allekirjoitus:"}} FI_COMPLEXITY = [("amortised O(1) per call", "tasattu O(1) kutsua kohden"), ("O(1) average per operation", "keskimäärin O(1) operaatiota kohden"), ("O(1) set, O(log n) get", "set O(1), get O(log n)"), ("time for the sort", "aikaa järjestämiseen"), ("extra space besides the output", "lisämuistia tulosteen lisäksi"), ("extra space", "lisämuistia"), ("time and space", "aikaa ja muistia"), (" time", " aikaa"), (" space", " muistia"), (" and ", " ja "), ("per call", "kutsua kohden")] def complexity(p: Problem) -> str: s = p.complexity if p.lang == "fi": for en, fi in FI_COMPLEXITY: s = s.replace(en, fi) return s def _class_lines(p: Problem, call: dict, outputs: list, n_ops: int, assert_mode: bool) -> list[str]: var = "obj" lines = [f"{var} = {p.entry}(" + ", ".join(repr(a) for a in call["init"]) + ")"] for op, out in list(zip(call["ops"], outputs))[:n_ops]: expr = f"{var}.{op[0]}(" + ", ".join(repr(a) for a in op[1:]) + ")" if out is None: lines.append(expr) elif assert_mode: lines.append(f"assert {expr}" if out is True else f"assert not {expr}" if out is False else f"assert {expr} == {out!r}") else: lines.append(f"{expr} # {out!r}") return lines def examples_block(p: Problem, n: int = 2) -> str: w = WORD[p.lang] if p.kind == "function": lines = [] for call, out in p.examples(n): lines += [f">>> {fmt_call(p.entry, call)}", repr(out)] head = w["examples"] if n > 1 else w["example"] else: call = p.small[0] lines = _class_lines(p, call, p.results[json.dumps(call)], 7, False) head = w["example"] return f"{head}:\n```python\n" + "\n".join(lines) + "\n```" def tests_block(p: Problem, rng) -> str: if p.kind == "function": calls = p.small[:2] + rng.sample(p.edge, min(3, len(p.edge))) lines = [] for c in calls: exp, call = p.results[json.dumps(c)], fmt_call(p.entry, c) lines.append(f"assert {call}" if exp is True else f"assert not {call}" if exp is False else f"assert {call} == {exp!r}") return "\n".join(lines) blocks = [] for call in [p.small[1]] + p.edge[:1]: blocks.append("\n".join(_class_lines(p, call, p.results[json.dumps(call)], 10, True))) return "\n\n".join(blocks) def user_prompt(p: Problem, mode: str, rng) -> str: w = WORD[p.lang] parts = [p.statement, f"{w['signature']}\n```python\n{p.signature}\n```", examples_block(p, 2 if p.kind == "function" else 1), rng.choice(ASK[p.lang][mode])] return "\n\n".join(parts) def assistant_reply(p: Problem, mode: str, think: bool, rng) -> tuple[str, bool]: w = WORD[p.lang] out = [] if think: steps = "\n".join(f"- {s}" for s in p.plan) out.append(f"\n{steps}\n- {w['complexity']}: {complexity(p)}.\n") out.append(p.approach) out.append(f"```python\n{p.solution.rstrip()}\n```") if mode == "explain": out.append(w["why"] + "\n" + "\n".join(f"- {s}" for s in p.plan[1:3])) out.append(f"**{w['complexity']}:** {complexity(p)}.") ok = True if mode == "tests": tests = tests_block(p, rng) code, output = run_script(p.solution + "\n\n" + tests + "\nprint('ok')\n", timeout=20) ok = code == 0 and output.strip().endswith("ok") out.append(f"{w['tests']}:\n```python\n{tests}\n```\n{w['pass']}") return "\n\n".join(out), ok