Skip to content

Instantly share code, notes, and snippets.

@mike-pete
Created June 9, 2022 03:15
Show Gist options
  • Save mike-pete/329e3c9ac46613078ceae1495b210c20 to your computer and use it in GitHub Desktop.
Save mike-pete/329e3c9ac46613078ceae1495b210c20 to your computer and use it in GitHub Desktop.
class Solution:
def romanToInt(self, s: str) -> int:
numerals = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
total = 0
numeralList = list(s)
valueOfPreviousNumeral = 0
for numeral in numeralList:
valueOfCurrentNumeral = numerals[numeral]
total += valueOfCurrentNumeral
if (valueOfPreviousNumeral < valueOfCurrentNumeral):
total -= valueOfPreviousNumeral*2
valueOfPreviousNumeral = valueOfCurrentNumeral
return total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment