{ "task": "Write a Python function `roman_to_int(s: str) -> int` that converts a Roman numeral (I, V, X, L, C, D, M, including subtractive forms like IV, IX, XL, XC, CD, CM) to an integer, and raises ValueError for an empty string or any character that is not a Roman numeral. Reply with one Python code block only.", "reasoning_effort": "low", "elapsed_s": 7.9, "usage": { "prompt_tokens": 121, "completion_tokens": 823, "total_tokens": 944, "prompt_tokens_details": { "cached_tokens": 0 } }, "passed": 10, "total": 10, "results": [ { "input": "III", "expected": 3, "got": 3, "ok": true }, { "input": "IV", "expected": 4, "got": 4, "ok": true }, { "input": "IX", "expected": 9, "got": 9, "ok": true }, { "input": "LVIII", "expected": 58, "got": 58, "ok": true }, { "input": "MCMXCIV", "expected": 1994, "got": 1994, "ok": true }, { "input": "MMXXVI", "expected": 2026, "got": 2026, "ok": true }, { "input": "CDXLIV", "expected": 444, "got": 444, "ok": true }, { "input": "", "expected": "ValueError", "got": "ValueError", "ok": true }, { "input": "ABC", "expected": "ValueError", "got": "ValueError", "ok": true }, { "input": "MMX1", "expected": "ValueError", "got": "ValueError", "ok": true } ], "code": "def roman_to_int(s: str) -> int:\n if not s:\n raise ValueError(\"Empty string\")\n\n values = {\n \"I\": 1,\n \"V\": 5,\n \"X\": 10,\n \"L\": 50,\n \"C\": 100,\n \"D\": 500,\n \"M\": 1000,\n }\n\n if any(ch not in values for ch in s):\n raise ValueError(\"Invalid Roman numeral character\")\n\n total = 0\n i = 0\n\n while i < len(s):\n if i + 1 < len(s) and s[i:i + 2] in {\n \"IV\": 4,\n \"IX\": 9,\n \"XL\": 40,\n \"XC\": 90,\n \"CD\": 400,\n \"CM\": 900,\n }:\n total += values[s[i + 1]] - values[s[i]]\n i += 2\n else:\n total += values[s[i]]\n i += 1\n\n return total\n" }