-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathIntegerToRoman.py
38 lines (31 loc) · 954 Bytes
/
IntegerToRoman.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
# Given an integer, convert it to a roman numeral.
#
# Input is guaranteed to be within the range from 1 to 3999.
#
# Python, Python3 all accepted.
class IntegerToRoman:
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
result = ""
roman = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
value = [1000, 500, 100, 50, 10, 5, 1]
for n in range(0, 7, 2):
x = num // value[n]
if x < 4:
for _ in range(1, x + 1):
result += roman[n]
elif x == 4:
result += roman[n]
result += roman[n - 1]
elif x < 9:
result += roman[n - 1]
for _ in range(6, x + 1):
result += roman[n]
elif x == 9:
result += roman[n]
result += roman[n - 2]
num %= value[n]
return result