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

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

    result = []
    for value, numeral in values:
        count, n = divmod(n, value)
        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,
    }

    total = 0
    previous = 0
    for char in reversed(s):
        if char not in values:
            raise ValueError("invalid Roman numeral")
        value = values[char]
        if value < previous:
            total -= value
        else:
            total += value
            previous = value

    if not 1 <= total <= 3999 or int_to_roman(total) != s:
        raise ValueError("invalid Roman numeral")
    return total