Skip to content

Instantly share code, notes, and snippets.

@LotusChing
Created July 30, 2016 02:43
Show Gist options
  • Save LotusChing/7521ab08e87c83e974ca3a612a00c450 to your computer and use it in GitHub Desktop.
Save LotusChing/7521ab08e87c83e974ca3a612a00c450 to your computer and use it in GitHub Desktop.
roman number to integer
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