-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.RomanToInteger.py
More file actions
40 lines (34 loc) · 1.16 KB
/
Copy path13.RomanToInteger.py
File metadata and controls
40 lines (34 loc) · 1.16 KB
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
"""
File: 13.RomanToInteger.py
Author: Madhu Kumar K S
Description: Convert Roman numerals to integers
Problem: LeetCode #13 - Roman to Integer
"""
def roman_to_integer(s: str) -> int:
"""
Convert a Roman numeral string to an integer.
Args:
s (str): Roman numeral string
Returns:
int: Integer value of the Roman numeral
"""
# Dictionary mapping Roman numerals to their integer values
roman = {
"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000
}
total = 0
# Iterate through each character in the Roman numeral
for i in range(len(s)):
curr = roman[s[i]] # Current Roman numeral value
next = roman[s[i+1]] if i+1 < len(s) else 0 # Next Roman numeral value
# If current value is less than next, subtract it (e.g., IV = 4)
if curr < next:
total -= curr
else:
# Otherwise, add the current value
total += curr
return total
# Test the function with Roman numeral "MCMIV" (1904)
roman_num = "MCMIV"
result = roman_to_integer(roman_num)
print(f"The given Roman numeral {roman_num} converts to: {result}")