-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman_Numerals_Helper.py
More file actions
46 lines (40 loc) · 1.19 KB
/
Roman_Numerals_Helper.py
File metadata and controls
46 lines (40 loc) · 1.19 KB
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
class RomanNumerals(object):
roman_number_dictionary = {
"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
}
@staticmethod
def to_roman(number):
_number = number
roman_number_dictionary = RomanNumerals.roman_number_dictionary
roman = ''
for key in roman_number_dictionary:
roman_number = roman_number_dictionary[key]
while _number >= roman_number:
roman += key
_number -= roman_number
return roman
@staticmethod
def from_roman(line):
_line = line
roman_number_dictionary = RomanNumerals.roman_number_dictionary
number = 0
previous_character = 0
for i in range(len(line) - 1, -1, -1):
if roman_number_dictionary[line[i]] >= previous_character:
number += roman_number_dictionary[line[i]]
else:
number -= roman_number_dictionary[line[i]]
previous_character = roman_number_dictionary[line[i]]
return number