-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path12-integer-to-roman.py
More file actions
27 lines (21 loc) · 927 Bytes
/
12-integer-to-roman.py
File metadata and controls
27 lines (21 loc) · 927 Bytes
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
class Solution:
def intToRoman(self, num: int) -> str:
values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
result = ""
for i in range(len(values)):
while num >= values[i]:
result += symbols[i]
num -= values[i]
return result
# Test cases
if __name__ == "__main__":
sol = Solution()
print("Testing Integer to Roman:")
print(f"3 -> {sol.intToRoman(3)} (Expected: III)")
print(f"58 -> {sol.intToRoman(58)} (Expected: LVIII)")
print(f"1994 -> {sol.intToRoman(1994)} (Expected: MCMXCIV)")
print(f"9 -> {sol.intToRoman(9)} (Expected: IX)")
print(f"4 -> {sol.intToRoman(4)} (Expected: IV)")
print(f"1 -> {sol.intToRoman(1)} (Expected: I)")
print(f"3999 -> {sol.intToRoman(3999)} (Expected: MMMCMXCIX)")