Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python roman to integer

def roman_to_int(roman:str) -> int:
    romans = {
        "I": 1,
        "V": 5,
        "X": 10,
        "L": 50,
        "C": 100,
        "D": 500,
        "M": 1000
    }
    result = 0
    
    # Single digit number
    if len(roman) < 2:
        return romans[roman.upper()]
      
    # Iterate the string only once -> O(n)
    for i in range(len(roman)-1):
        current_num = romans[roman[i].upper()]
        next_num = romans[roman[i+1].upper()]
        if current_num < next_num:
            result -= current_num
        else:
            result += current_num
    else:  # Add the last number
        result += next_num

    return result
 
PREVIOUS NEXT
Tagged: #python #roman #integer
ADD COMMENT
Topic
Name
4+5 =