forked from kingblue5453/group11_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_midterm_2.py
38 lines (31 loc) · 966 Bytes
/
test_midterm_2.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
class Solution:
def romanToInt(self, s: str) -> int:
"""
Convert the given Roman numeral string to an integer.
Parameters:
s (str): The Roman numeral string to be converted.
Returns:
int: The integer value of the Roman numeral.
Examples:
>>> solution = Solution()
>>> solution.romanToInt("III")
3
>>> solution.romanToInt("LVIII")
58
>>> solution.romanToInt("MCMXCIV")
1994
"""
m = {
'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000
}
result = 0
s_len = len(s)
for i in range(s_len):
if i < len(s) - 1 and m[s[i]] < m[s[i + 1]]:
result -= m[s[i]]
else:
result += m[s[i]]
return result
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)