-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroman_to_integer.py
More file actions
29 lines (25 loc) · 967 Bytes
/
roman_to_integer.py
File metadata and controls
29 lines (25 loc) · 967 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
class Solution:
def romanToInt(self, s: str) -> int:
dictionary = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
my_sum = 0
for i in range( len(s) ):
if i ==0:
my_sum = dictionary[s[i]]
elif dictionary[s[i-1]] < dictionary[s[i]]:
# be careful:
# smaller roman literal ‘I’ appears before it,
# we need to subtract ‘I’ from ‘V’
# that we already added another ‘I’ before it,
# so we need to subtract a total of 'two' one’s from it
my_sum = my_sum + dictionary[s[i]] - (2*dictionary[s[i-1]])
else:
my_sum = my_sum + dictionary[s[i]]
return my_sum