-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathinteger-to-roman.py
More file actions
40 lines (33 loc) · 888 Bytes
/
integer-to-roman.py
File metadata and controls
40 lines (33 loc) · 888 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
class Number:
def __init__(self, num: int) -> None:
self._num = num
self._roman_numbers = {
1: "I",
4: "IV",
5: "V",
9: "IX",
10: "X",
40: "XL",
50: "L",
90: "XC",
100: "C",
400: "CD",
500: "D",
900: "CM",
1000: "M",
}
@property
def roman(self) -> str:
num = self._num
result = []
while num > 0:
for value, roman_repr in reversed(sorted(self._roman_numbers.items())):
if num >= value:
result.append(roman_repr)
num -= value
break
return "".join(result)
class Solution:
def intToRoman(self, num: int) -> str:
number = Number(num)
return number.roman