-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromanToInteger.py
47 lines (46 loc) · 1.24 KB
/
romanToInteger.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
def romanToInteger(s):
obj={
"I":1,
"IV":4,
"V":5,
"IX":9,
"X":10,
"XL":40,
"L":50,
"XC":90,
"C":100,
"CD":400,
"D":500,
"CM":900,
"M":1000,
}
num = 0
for i in range(len(s)):
if (s[i] == 'V' or s[i] == 'X') and i != 0:
if(s[i-1] == 'I'):
print("FIRST",i)
x = str(s[i-1]) + str(s[i])
num -= obj["I"]
num += obj[x]
else:
num += obj[s[i]]
print("FIRST",num)
elif (s[i] == 'L' or s[i] == 'C') and i != 0:
if(s[i-1] == 'X'):
x = str(s[i-1]) + str(s[i])
num -= obj["X"]
num += obj[x]
else:
num += obj[s[i]]
elif (s[i] == 'D' or s[i] == 'M') and i != 0:
if(s[i-1] == 'C'):
x = str(s[i-1]) + str(s[i])
num -= obj["C"]
num += obj[x]
else:
num += obj[s[i]]
else:
print("EKHANE",num)
num += obj[s[i]]
return num
print(romanToInteger("VII"))