Skip to content

Commit 7b4b72c

Browse files
Merge pull request #2 from codewithme-py/feat/integer-to-roman-0012
feat: Integer to Roman (#12)
2 parents b8d6639 + 504f67f commit 7b4b72c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

solutions/integer_to_roman_0012.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def intToRoman(self, num: int) -> str:
3+
roman_pairs = [
4+
(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'),
5+
(90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'),
6+
(9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')
7+
]
8+
result = []
9+
for value, symbol in roman_pairs:
10+
count, num = divmod(num, value)
11+
result.append(symbol * count)
12+
return ''.join(result)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from solutions.integer_to_roman_0012 import Solution
2+
3+
def test_int_to_roman():
4+
sol = Solution()
5+
assert sol.intToRoman(3749) == "MMMDCCXLIX"
6+
assert sol.intToRoman(58) == "LVIII"
7+
assert sol.intToRoman(1994) == "MCMXCIV"
8+
assert sol.intToRoman(1) == "I"
9+
assert sol.intToRoman(4) == "IV"
10+
assert sol.intToRoman(9) == "IX"
11+
assert sol.intToRoman(40) == "XL"
12+
assert sol.intToRoman(90) == "XC"
13+
assert sol.intToRoman(400) == "CD"
14+
assert sol.intToRoman(900) == "CM"
15+
assert sol.intToRoman(3999) == "MMMCMXCIX"

0 commit comments

Comments
 (0)