CODE-0004 · gpt-5.5 (high)_ROMAN_VALUES = (
(1000, "M"),
(900, "CM"),
(500, "D"),
(400, "CD"),
(100, "C"),
(90, "XC"),
(50, "L"),
(40, "XL"),
(10, "X"),
(9, "IX"),
(5, "V"),
(4, "IV"),
(1, "I"),
)
_ROMAN_DIGITS = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000,
}
def int_to_roman(n):
if not isinstance(n, int) or not 1 <= n <= 3999:
raise ValueError("integer must be in the range 1..3999")
result = []
for value, symbol in _ROMAN_VALUES:
count, n = divmod(n, value)
result.append(symbol * count)
return "".join(result)
def roman_to_int(s):
if not isinstance(s, str) or not s:
raise ValueError("invalid Roman numeral")
total = 0
previous = 0
for char in reversed(s):
try:
value = _ROMAN_DIGITS[char]
except KeyError:
raise ValueError("invalid Roman numeral") from None
if value < previous:
total -= value
else:
total += value
previous = value
try:
canonical = int_to_roman(total)
except ValueError:
raise ValueError("invalid Roman numeral") from None
if s != canonical:
raise ValueError("invalid Roman numeral")
return total