CODE-0004 · glm-5.2 (default)import re
_VALUE_MAP = [
(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}
_CANONICAL_PATTERN = re.compile(
r"^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$"
)
def int_to_roman(n):
if not isinstance(n, int) or isinstance(n, bool) or n < 1 or n > 3999:
raise ValueError("integer out of range (1..3999)")
result = []
for value, symbol in _VALUE_MAP:
while n >= value:
result.append(symbol)
n -= value
return "".join(result)
def roman_to_int(s):
if not isinstance(s, str) or not s:
raise ValueError("empty or invalid Roman numeral")
if not _CANONICAL_PATTERN.match(s):
raise ValueError("malformed or non-canonical Roman numeral")
total = 0
prev = 0
for ch in reversed(s):
value = _ROMAN_VALUES[ch]
if value < prev:
total -= value
else:
total += value
prev = value
return total