-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomanNumeralsHelper.py
61 lines (56 loc) · 2.4 KB
/
RomanNumeralsHelper.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
'''
Task
Create a RomanNumerals class that can convert a roman numeral to and from an integer value. It should follow the API demonstrated in the examples below. Multiple roman numeral values will be tested for each helper method.
Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.
Examples
RomanNumerals.to_roman(1000) # should return 'M'
RomanNumerals.from_roman('M') # should return 1000
Help
| Symbol | Value |
|----------------|
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
'''
class RomanNumerals():
@classmethod
def to_roman(self,num):
romans = {1:'I', 5:'V', 10:'X', 50:'L', 100:'C', 500:'D', 1000:'M'}
result = []
if num in romans: return romans[num]
else:
x = int('1'+'0' * (len(str(num))-1))
for i in range(len(str(num))):
if 1 <= num//x <=3: [result.append(romans[1*x]) for i in range(num//x)]
elif num//x == 4:
result.append(romans[1*x])
result.append(romans[5*x])
elif num//x == 5: result.append(romans[5*x])
elif 6<= num//x <=8:
result.append(romans[5*x])
[result.append(romans[x]) for i in range(num//x - 5)]
elif num//x == 9:
result.append(romans[1*x])
result.append(romans[1*x*10])
if x != 1:
num = num - num//x*x
x = int(str(x)[:-1])
return ''.join(result)
@classmethod
def from_roman(self,roman):
nums = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
result = 0
for index, i in enumerate(roman):
if index == len(roman)-1:
result += nums[i]
elif nums[i] < nums[roman[index + 1]]:
result -= nums[i]
else:
result += nums[i]
return result
print(RomanNumerals.to_roman(2020))
print(RomanNumerals.from_roman('MMXX'))