CODE-0004 · claude-sonnet-5 (high)
def int_to_roman(n):
    if not isinstance(n, int) or isinstance(n, bool):
        raise ValueError("n must be an integer")
    if n < 1 or n > 3999:
        raise ValueError("n 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 = []
    remaining = n
    for value, symbol in values:
        if remaining == 0:
            break
        count, remaining = divmod(remaining, value)
        if count:
            result.append(symbol * count)
    return "".join(result)


def roman_to_int(s):
    if not isinstance(s, str) or len(s) == 0:
        raise ValueError("s must be a non-empty string")

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

    for ch in s:
        if ch not in symbol_values:
            raise ValueError("invalid character in Roman numeral: %r" % ch)

    total = 0
    i = 0
    n = len(s)
    while i < n:
        current = symbol_values[s[i]]
        next_value = symbol_values[s[i + 1]] if i + 1 < n else 0
        if current < next_value:
            total += next_value - current
            i += 2
        else:
            total += current
            i += 1

    if total < 1 or total > 3999:
        raise ValueError("value out of range 1..3999")

    if int_to_roman(total) != s:
        raise ValueError("not a canonical Roman numeral: %r" % s)

    return total