Skip to content

Instantly share code, notes, and snippets.

@tanmay27vats
Last active February 1, 2022 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanmay27vats/d97e5e4a3bd26b783cd90ddc544e1028 to your computer and use it in GitHub Desktop.
Save tanmay27vats/d97e5e4a3bd26b783cd90ddc544e1028 to your computer and use it in GitHub Desktop.
[Python] Roman to Integer - Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
class Solution:
def romanToInt(self, s: str) -> int:
roman_dict = { 'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000, 'IV':4, 'IX':9, 'XL':40, 'XC':90, 'CD':400, 'CM':900 }
i = 0
sum = 0
while i < len(s):
if s[i:i+2] in roman_dict and i+1<len(s):
sum+=roman_dict[s[i:i+2]]
i+=2
else:
sum+=roman_dict[s[i]]
i+=1
return sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment