Last active
February 1, 2022 07:24
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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