-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_intToRoman.py
89 lines (63 loc) · 2.06 KB
/
12_intToRoman.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 12. Integer to Roman
from collections import deque
class Solution:
def intToRoman(self, num: int) -> str:
i = 0
roman = ''
digits = deque([])
num_=num
while(num_!=0):
dig = num_%10
digits.appendleft(dig)
num_ = num_//10
n = len(digits)-1
while(digits):
dig = (digits.popleft())*(10**n)
if dig>0 and dig<4:
while(dig>0):
roman+='I'
dig-=1
elif dig==4:
roman+='IV'
elif dig>=5 and dig<9:
roman+='V'
dig-=5
while(dig>0):
roman+='I'
dig-=1
elif dig==9:
roman+='IX'
elif dig>=10 and dig<40:
while(dig>0):
roman+='X'
dig-=10
elif dig==40:
roman+='XL'
elif dig>=50 and dig<90:
roman+='L'
dig-=50
while(dig>0):
roman+='X'
dig-=10
elif dig==90:
roman+='XC'
elif dig>=100 and dig<400:
while(dig>0):
roman+='C'
dig-=100
elif dig==400:
roman+='CD'
elif dig>=500 and dig<900:
roman+='D'
dig-=500
while(dig>0):
roman+='C'
dig-=100
elif dig==900:
roman+='CM'
elif dig>=1000:
while(dig)>0:
roman+='M'
dig-=1000
n-=1
return roman