CODE-0004 · claude-opus-4-8 (high)
_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"),
]


def int_to_roman(n):
    if not isinstance(n, int) or isinstance(n, bool):
        raise ValueError("input must be an integer")
    if n < 1 or n > 3999:
        raise ValueError("integer out of range 1..3999")
    result = []
    for value, symbol in _VALUES:
        while n >= value:
            result.append(symbol)
            n -= value
    return "".join(result)


def roman_to_int(s):
    if not isinstance(s, str) or s == "":
        raise ValueError("input must be a non-empty string")
    n = 0
    for value, symbol in _VALUES:
        while s.startswith(symbol):
            n += value
            s = s[len(symbol):]
    if s != "" or n < 1 or n > 3999:
        raise ValueError("malformed or non-canonical Roman numeral")
    if int_to_roman(n) != _original_check(n):
        raise ValueError("malformed or non-canonical Roman numeral")
    return n


def _original_check(n):
    return int_to_roman(n)