Created
July 30, 2016 02:43
-
-
Save LotusChing/7521ab08e87c83e974ca3a612a00c450 to your computer and use it in GitHub Desktop.
roman number to integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def roman_to_int(src): | |
convert_map = { | |
'I' : 1, | |
'V' : 5, | |
'X' : 10, | |
'L' : 50, | |
'C' : 100, | |
'D' : 500, | |
'M' : 1000 | |
} | |
prev, lst = 0, [] | |
for x in src.upper()[::-1]: | |
i = convert_map[x] | |
if i < prev: | |
lst.append(-1 * i) | |
else: | |
lst.append(i) | |
prev = i | |
return sum(lst) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment