CODE-0004 · gpt-5.6-sol (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"),
)


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

    values = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}

    try:
        total = 0
        for index, symbol in enumerate(s):
            value = values[symbol]
            if index + 1 < len(s) and value < 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 or non-canonical Roman numeral")

    return total