-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecimate.py
More file actions
55 lines (44 loc) · 906 Bytes
/
decimate.py
File metadata and controls
55 lines (44 loc) · 906 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
This problem was asked by Facebook.
Given a number in Roman numeral format, convert it to decimal.
The values of Roman numerals are as follows:
{
'M': 1000,
'D': 500,
'C': 100,
'L': 50,
'X': 10,
'V': 5,
'I': 1
}
In addition, note that the Roman numeral system uses subtractive notation for numbers such as IV and XL.
For the input XIV, for instance, you should return 14.
"""
PAIRS = {
'CM': 900,
'CD': 400,
'XC': 90,
'XL': 40,
'IX': 9,
'IV': 4
}
SINGLES = {
'M': 1000,
'D': 500,
'C': 100,
'L': 50,
'X': 10,
'V': 5,
'I': 1
}
def decimate(s, total=0):
if not s:
return total
if s[:2] in PAIRS:
total += PAIRS[s[:2]]
return decimate(s[2:], total)
else:
total += SINGLES[s[:1]]
return decimate(s[1:], total)
if __name__ == '__main__':
print(decimate("XIV"))