{ "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": 10.6, "usage": { "prompt_tokens": 121, "completion_tokens": 1160, "total_tokens": 1281, "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 is not a valid Roman numeral\")\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 total = 0\n i = 0\n\n while i < len(s):\n if s[i] not in values:\n raise ValueError(f\"Invalid Roman numeral character: {s[i]}\")\n\n if i + 1 < len(s) and s[i + 1] in values and values[s[i]] < values[s[i + 1]]:\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" }