CODE-0004 · gpt-5.6-sol (max)_ROMAN_PAIRS = (
(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_VALUES = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000,
}
def int_to_roman(n):
if isinstance(n, bool) or not isinstance(n, int) or not 1 <= n <= 3999:
raise ValueError("n must be an integer from 1 to 3999")
result = []
for value, numeral in _ROMAN_PAIRS:
count, n = divmod(n, value)
if count:
result.append(numeral * count)
return "".join(result)
def roman_to_int(s):
if not isinstance(s, str) or not s:
raise ValueError("invalid Roman numeral")
total = 0
try:
for index, symbol in enumerate(s):
value = _ROMAN_VALUES[symbol]
if index + 1 < len(s) and value < _ROMAN_VALUES[s[index + 1]]:
total -= value
else:
total += value
except KeyError:
raise ValueError("invalid Roman numeral") from None
if not 1 <= total <= 3999 or int_to_roman(total) != s:
raise ValueError("invalid Roman numeral")
return total