-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomanNumerals.py
66 lines (63 loc) · 1.6 KB
/
RomanNumerals.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
literals = {1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL', 50: 'L', 90: 'XC', 100: 'C', 500: 'D', 900: 'CM', 1000: 'M'}
literalsInv = dict([(v,k) for k,v in literals.items()])
def arabic_to_roman(arabic):
"""Converts an arabic number into a roman number.
>>> arabic_to_roman(1)
'I'
>>> arabic_to_roman(4)
'IV'
>>> arabic_to_roman(5)
'V'
>>> arabic_to_roman(515)
'DXV'
>>> arabic_to_roman(789)
'DCCLXXXIX'
>>> arabic_to_roman(1888)
'MDCCCLXXXVIII'
>>> arabic_to_roman(1984)
'MCMLXXXIV'
>>> arabic_to_roman(1999)
'MCMXCIX'
>>> arabic_to_roman(2001)
'MMI'
>>> arabic_to_roman(2006)
'MMVI'
"""
roman = ''
for l in sorted(literals.keys(), reverse=True):
roman += (arabic//l) * literals[l]
arabic %= l
return roman
def roman_to_arabic(roman):
"""Converts a roman number into an arabic number.
>>> roman_to_arabic('I')
1
>>> roman_to_arabic('IV')
4
>>> roman_to_arabic('V')
5
>>> roman_to_arabic('DXV')
515
>>> roman_to_arabic('DCCLXXXIX')
789
>>> roman_to_arabic('MDCCCLXXXVIII')
1888
>>> roman_to_arabic('MCMLXXXIV')
1984
>>> roman_to_arabic('MCMXCIX')
1999
>>> roman_to_arabic('MMI')
2001
>>> roman_to_arabic('MMVI')
2006
"""
if len(roman) == 0:
return 0
if len(roman) == 1:
return literalsInv[roman]
else:
a,b = roman[:2]
if a+b in literalsInv:
return literalsInv[a+b] + roman_to_arabic(roman[2:])
else:
return literalsInv[a] + roman_to_arabic(roman[1:])