forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy path13_Roman_to_Integer.py
More file actions
33 lines (29 loc) · 840 Bytes
/
Copy path13_Roman_to_Integer.py
File metadata and controls
33 lines (29 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
romanDict = {'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
intNum = 0
if len(s) == 1:
return romanDict[s]
for i in range(len(s) - 1):
v1 = romanDict[s[i]]
v2 = romanDict[s[i + 1]]
if romanDict[s[i]] >= romanDict[s[i + 1]]:
intNum = romanDict[s[i]] + intNum
else:
intNum = intNum - romanDict[s[i]]
return intNum + romanDict[s[-1]]
sol = Solution()
input = "MCMXCIV"
value = sol.romanToInt(input)
print("Value: ", value)