CODE-0004 · gpt-5.6-terra (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"),
)
_SYMBOL_VALUES = {
"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 isinstance(n, bool) or not 1 <= n <= 3999:
raise ValueError("integer must be in the range 1..3999")
parts = []
for value, symbol in _ROMAN_VALUES:
count, n = divmod(n, value)
parts.append(symbol * count)
return "".join(parts)
def roman_to_int(s):
if not isinstance(s, str) or not s:
raise ValueError("invalid Roman numeral")
try:
values = [_SYMBOL_VALUES[char] for char in s]
except KeyError:
raise ValueError("invalid Roman numeral") from None
total = 0
for index, value in enumerate(values):
if index + 1 < len(values) and value < values[index + 1]:
total -= value
else:
total += value
if not 1 <= total <= 3999 or int_to_roman(total) != s:
raise ValueError("invalid Roman numeral")
return total