Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created January 7, 2021 12:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/4ea5fc3efbe8b522dc340de8f72dbbd9 to your computer and use it in GitHub Desktop.
Save amankharwal/4ea5fc3efbe8b522dc340de8f72dbbd9 to your computer and use it in GitHub Desktop.
tallies = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
# specify more numerals if you wish
}
def RomanNumeralToDecimal(romanNumeral):
sum = 0
for i in range(len(romanNumeral) - 1):
left = romanNumeral[i]
right = romanNumeral[i + 1]
if tallies[left] < tallies[right]:
sum -= tallies[left]
else:
sum += tallies[left]
sum += tallies[romanNumeral[-1]]
return sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment