leetcode no 13 roman to integer given a roman numeral convert it to an integer input is guaranteed to be within the range from 1 to 3999 https en wikipedia orgwikiromannumerals tests iii 3 cliv 154 mix 1009 mmd 2500 mmmcmxcix 3999 allromantointkey value for key value in tests items true given a integer convert it to an roman numeral https en wikipedia orgwikiromannumerals tests iii 3 cliv 154 mix 1009 mmd 2500 mmmcmxcix 3999 allinttoromanvalue key for key value in tests items true leetcode no 13 roman to integer given a roman numeral convert it to an integer input is guaranteed to be within the range from 1 to 3999 https en wikipedia org wiki roman_numerals tests iii 3 cliv 154 mix 1009 mmd 2500 mmmcmxcix 3999 all roman_to_int key value for key value in tests items true given a integer convert it to an roman numeral https en wikipedia org wiki roman_numerals tests iii 3 cliv 154 mix 1009 mmd 2500 mmmcmxcix 3999 all int_to_roman value key for key value in tests items true
ROMAN = [
    (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 roman_to_int(roman: str) -> int:
    
    vals = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
    total = 0
    place = 0
    while place < len(roman):
        if (place + 1 < len(roman)) and (vals[roman[place]] < vals[roman[place + 1]]):
            total += vals[roman[place + 1]] - vals[roman[place]]
            place += 2
        else:
            total += vals[roman[place]]
            place += 1
    return total


def int_to_roman(number: int) -> str:
    
    result = []
    for arabic, roman in ROMAN:
        (factor, number) = divmod(number, arabic)
        result.append(roman * factor)
        if number == 0:
            break
    return "".join(result)


if __name__ == "__main__":
    import doctest

    doctest.testmod()