Skip to content

Instantly share code, notes, and snippets.

@ChrisMissal
Created April 30, 2010 03:51
Show Gist options
  • Save ChrisMissal/384711 to your computer and use it in GitHub Desktop.
Save ChrisMissal/384711 to your computer and use it in GitHub Desktop.
def romanToInt(str):
table = [ ['M', 1000], ['D', 500], ['C', 100], ['L', 50], ['X', 10], ['V', 5], ['I', 1] ]
sum = 0
previous = 999999
chars = str.split()
for c in chars:
for pair in table:
if (pair[0] == c):
val = pair[1]
sum = sum + val
if (previous < val):
sum = (sum - (2 * previous))
previous = val
print str, " = ", sum
romanToInt("VII")
romanToInt("MMIX")
romanToInt("CCXXXIX")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment