-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroman_numerals_short.py
More file actions
29 lines (21 loc) · 983 Bytes
/
Copy pathroman_numerals_short.py
File metadata and controls
29 lines (21 loc) · 983 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
from collections import OrderedDict
class RomanNumerals:
def to_roman(num):
conversions = OrderedDict([('M', 1000), ('CM', 900), ('D', 500), ('CD', 400), ('C', 100), ('XC', 90), ('L', 50), ('XL', 40),
('X', 10), ('IX', 9), ('V', 5), ('IV', 4), ('I', 1)])
out = ''
for key, value in conversions.items():
while num >= value:
out += key
num -= value
return out
def from_roman(roman):
conversions = OrderedDict([('CM', 900), ('CD', 400), ('XC', 90), ('XL', 40), ('IX', 9), ('IV', 4), ('M', 1000), ('D', 500),
('C', 100), ('L', 50), ('X', 10), ('V', 5), ('I', 1)])
out = 0
for key, value in conversions.items():
out += value * roman.count(key)
roman = str.replace(roman, key, "")
return out
print(RomanNumerals.to_roman(1249))
print(RomanNumerals.from_roman("MCCXLIX"))