-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path12_IntegertoRoman.py
executable file
·47 lines (41 loc) · 1.26 KB
/
12_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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
integer_symbols = [["I", "IV", "V", "IX"],
["X", "XL", "L", "XC"],
["C", "CD", "D", "CM"],
["M"]
]
roman_str = ""
counter = 0
while num != 0:
single = num % 10
if single in [1, 2, 3]:
roman_str = single * integer_symbols[counter][0] + roman_str
elif single == 4:
roman_str = integer_symbols[counter][1] + roman_str
elif single == 5:
roman_str = integer_symbols[counter][2] + roman_str
elif single in [6, 7, 8]:
roman_str = integer_symbols[counter][2] +\
(single - 5) * integer_symbols[counter][0] +\
roman_str
elif single == 9:
roman_str = integer_symbols[counter][3] + roman_str
else:
num = num / 10
counter += 1
continue
num = num / 10
counter += 1
return roman_str
"""
1
100
3999
"""