-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromanToArab.py
43 lines (38 loc) · 1.04 KB
/
romanToArab.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
def RomanToArab(romanNumber):
l = []
d = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
for i in range(len(romanNumber)):
l.append(d.get(romanNumber[i]))
def compute(start, end):
if start >= end:
return 0
maximum = 0
index = -1
repeat = True
for i in range(start, end):
if i > start and repeat:
if l[i] != l[start]:
repeat = False
if l[i] > maximum:
maximum = l[i]
index = i
if repeat:
return (end - start) * l[start]
else:
return maximum - compute(start, index) + compute(index + 1, end)
return compute(0, len(l))
assert RomanToArab('I') == 1
assert RomanToArab('III') == 3
assert RomanToArab('VII') == 7
assert RomanToArab('XCVIII') == 98
assert RomanToArab('MDCCCLXXXVIII') == 1888
assert RomanToArab('MDCCCXCIX') == 1899
assert RomanToArab('MMMCMXCIX') == 3999