-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path013_Roman2int.py
39 lines (32 loc) · 1023 Bytes
/
013_Roman2int.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
# def romanToInt(s):
# res = 0
# dict_rom = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
# n = len(s)
# i = 0
# while (i < n):
# t = 0
# if i < (n-1) :
# if dict_rom[s[i]] < dict_rom[s[i+1]]:
# t = dict_rom[s[i+1]] - dict_rom[s[i]]
# res = res + t
# i = i + 2
# else:
# res = res + dict_rom[s[i]]
# i = i + 1
# else:
# res = res + dict_rom[s[i]]
# break
# return res
def romanToInt(s):
dict_rom = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
res = dict_rom[s[-1]]
N = len(s)
for i in range (N - 2, -1, -1):
if dict_rom[s[i]] < dict_rom[s[i + 1]]:
res = res - dict_rom[s[i]]
else:
res = res + dict_rom[s[i]]
return res
while (True):
input1 = input()
print(romanToInt(input1))