Skip to content

Commit f871b9a

Browse files
Merge pull request #1 from codewithme-py/feat/roman-to-integer-0013
feat: Roman to Integer (#13)
2 parents 48b330d + 62759c5 commit f871b9a

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
My clean, typed, and tested solutions to LeetCode problems (Python 3.10+).
44

55
<!-- START_STATS -->
6-
- ✅ Solved: 2
7-
- 🟢 Easy: 2
6+
- ✅ Solved: 3
7+
- 🟢 Easy: 3
88
- 🟡 Medium: 0
99
- 🔴 Hard: 0
1010
<!-- END_STATS -->
@@ -15,4 +15,5 @@ My clean, typed, and tested solutions to LeetCode problems (Python 3.10+).
1515
|---|-------|------------|----------|
1616
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | Easy | [`two_sum_0001.py`](solutions/two_sum_0001.py) |
1717
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | Easy | [`palindrome_number_0009.py`](solutions/palindrome_number_0009.py) |
18+
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | Easy | [`roman_to_integer_0013.py`](solutions/roman_to_integer_0013.py) |
1819
<!-- END_TABLE -->

solutions/roman_to_integer_0013.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class Solution:
2+
def romanToInt(self, s: str) -> int:
3+
roman_values = {
4+
'I': 1,
5+
'V': 5,
6+
'X': 10,
7+
'L': 50,
8+
'C': 100,
9+
'D': 500,
10+
'M': 1000
11+
}
12+
total = 0
13+
for i in range(len(s)):
14+
if i + 1 < len(s) and roman_values[s[i]] < roman_values[s[i + 1]]:
15+
total -= roman_values[s[i]]
16+
else:
17+
total += roman_values[s[i]]
18+
return total
19+
20+
"""
21+
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
22+
23+
Symbol Value
24+
I 1
25+
V 5
26+
X 10
27+
L 50
28+
C 100
29+
D 500
30+
M 1000
31+
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
32+
33+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
34+
35+
I can be placed before V (5) and X (10) to make 4 and 9.
36+
X can be placed before L (50) and C (100) to make 40 and 90.
37+
C can be placed before D (500) and M (1000) to make 400 and 900.
38+
Given a roman numeral, convert it to an integer.
39+
40+
41+
42+
Example 1:
43+
44+
Input: s = "III"
45+
Output: 3
46+
Explanation: III = 3.
47+
Example 2:
48+
49+
Input: s = "LVIII"
50+
Output: 58
51+
Explanation: L = 50, V= 5, III = 3.
52+
Example 3:
53+
54+
Input: s = "MCMXCIV"
55+
Output: 1994
56+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
57+
58+
59+
Constraints:
60+
61+
1 <= s.length <= 15
62+
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
63+
It is guaranteed that s is a valid roman numeral in the range [1, 3999].
64+
"""
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from solutions.roman_to_integer_0013 import Solution
2+
3+
def test_roman_to_int():
4+
sol = Solution()
5+
assert sol.romanToInt('III') == 3
6+
assert sol.romanToInt('LVIII') == 58
7+
assert sol.romanToInt('MCMXCIV') == 1994
8+
assert sol.romanToInt('IV') == 4
9+
assert sol.romanToInt('IX') == 9
10+
assert sol.romanToInt('XL') == 40
11+
assert sol.romanToInt('XC') == 90
12+
assert sol.romanToInt('CD') == 400
13+
assert sol.romanToInt('CM') == 900
14+
assert sol.romanToInt('M') == 1000

0 commit comments

Comments
 (0)