-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegerToRoman.py
55 lines (46 loc) · 1.21 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution:
def intToRoman(self, num: int) -> str:
return ''.join(self.convert(num))
def convert(self, num: int) -> str:
roman = {
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'
}
thresholds = list(roman.keys())
index = -1
while num > 0:
threshold = thresholds[index]
if num >= threshold:
yield roman[threshold]
num -= threshold
else:
index -= 1
def main():
import sys
import io
def readlines():
for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'):
yield line.strip('\n')
lines = readlines()
while True:
try:
line = next(lines)
num = int(line);
ret = Solution().intToRoman(num)
out = (ret);
print(out)
except StopIteration:
break
if __name__ == '__main__':
main()