-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-Integer to Roman.py
45 lines (45 loc) · 1.13 KB
/
12-Integer to Roman.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
class Solution:
def intToRoman(self, num: int) -> str:
dict1 = {1000: 'M', 900:'CM', 500:'D',400:'CD',100:'C',90:'XC',\
50:'L',40:'XL',10:'X',9:'IX',5:'V',4:'IV',1:'I'}
result = ''
while num >= 1000:
num -=1000
result +='M'
while num >= 900:
num -=900
result +='CM'
while num >= 500:
num -=500
result +='D'
while num >= 400:
num -=400
result +='CD'
while num >= 100:
num -=100
result +='C'
while num >= 90:
num -=90
result +='XC'
while num >= 50:
num -=50
result +='L'
while num >= 40:
num -= 40
result +='XL'
while num >= 10:
num -=10
result +='X'
while num >= 9:
num -=9
result +='IX'
while num >= 5:
num -=5
result +='V'
while num >=4:
num -=4
result +='IV'
while num >=1:
num -=1
result +='I'
return result