Python3 Solution


  • 0
    A
    # roman_to_arabic.py: Takes a roman numeral, between 1 and 3999,
    # and returns an arabic numeral.
    
    def roman_to_arabic(roma):
        conversion = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
        roma = roma.upper()
        arabic = 0
        skip = False
        for i in range(0, len(roma)):
            if skip == True:
                skip = False
                continue
            if i != len(roma)-1 and conversion[roma[i]] < conversion[roma[i+1]]:
                num_to_add = conversion[roma[i+1]] - conversion[roma[i]]
                skip = True
            else:
                num_to_add = conversion[roma[i]]
            arabic += num_to_add
        return arabic
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.