-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman-to-integer.py
55 lines (49 loc) · 1.34 KB
/
roman-to-integer.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution:
def romanToInt(self, s: str) -> int:
vals = {
'I' : 1,
'V' : 5,
'X' : 10,
'L' : 50,
'C' : 100,
'D' : 500,
'M' : 1000
}
sum = 0
prob = self.stringsplit(s)
for i in prob:
if len(i) == 1:
sum += vals[i]
else:
sum = sum + vals[i[1]] - vals[i[0]]
return sum
def stringsplit(self, s):
end = []
flag = 0
temp = ""
for i in range(len(s)):
if flag == 1:
flag = 0
continue
temp = s[i]
try:
if temp == 'I':
if s[i+1] in "VX":
temp += s[i+1]
flag = 1
if temp == 'X':
if s[i+1] in "LC":
temp += s[i+1]
flag = 1
if temp == 'C':
if s[i+1] in "DM":
temp += s[i+1]
flag = 1
except:
pass
end.append(temp)
return end
n = Solution()
print(n.romanToInt("MCMXCIV"))
print(n.romanToInt("LVIII"))
print(n.romanToInt("III"))