CODE-0004 · gpt-5.5-pro (high)
_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"),
)


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")

    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("malformed Roman numeral")

    total = 0
    index = 0

    while index < len(s):
        for value, numeral in _ROMAN_PAIRS:
            if s.startswith(numeral, index):
                total += value
                index += len(numeral)
                break
        else:
            raise ValueError("malformed Roman numeral")

    if int_to_roman(total) != s:
        raise ValueError("malformed Roman numeral")

    return total