-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_roman_to_integer.py
More file actions
51 lines (48 loc) · 1.33 KB
/
13_roman_to_integer.py
File metadata and controls
51 lines (48 loc) · 1.33 KB
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
'''
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
'''
class Solution:
def romanToInt(self, s: 'str') -> 'int':
out = 0
conditional = ['I', 'X', 'C']
vals = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
dubs = {'IV': 4, 'IX': 9, 'XL': 40, 'XC': 90, 'CD': 400, 'CM': 900}
index = 0
L = len(s)
while index < L:
char = s[index]
if char in conditional:
if index == L-1:
out += vals[char]
index += 1
else:
chars = s[index:index+2]
try:
out += dubs[chars]
index += 2
except:
out += vals[char]
index += 1
else:
out += vals[char]
index += 1
return(out)
if __name__ == '__main__':
test1 = 'III'
test2 = 'IV'
test3 = 'IX'
test4 = 'MCMXCIV'
test5 = 'LVIII'
sol = Solution()
assert(sol.romanToInt(test1) == 3)
assert(sol.romanToInt(test2) == 4)
assert(sol.romanToInt(test3) == 9)
assert(sol.romanToInt(test4) == 1994)
assert(sol.romanToInt(test5) == 58)