-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path13.py
83 lines (69 loc) · 2.05 KB
/
13.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# [ LeetCode ] 13. Roman to Integer
def solution(s: str) -> int:
answer: int = 0
rome_to_integer: dict[str, int] = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
is_minus: bool = False
previous_symbol: str = ""
for symbol in s[::-1]:
if symbol == "I":
if previous_symbol == "V" or previous_symbol == "X":
is_minus = True
elif symbol == "X":
if previous_symbol == "L" or previous_symbol == "C":
is_minus = True
elif symbol == "C":
if previous_symbol == "D" or previous_symbol == "M":
is_minus = True
if is_minus:
answer -= rome_to_integer[symbol]
else:
answer += rome_to_integer[symbol]
is_minus = False
previous_symbol = symbol
return answer
def another_solution(s: str) -> int:
rome_to_integer: dict[str, int] = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
reversed_s: str = s[::-1]
answer: int = rome_to_integer[reversed_s[0]]
for idx in range(1, len(reversed_s)):
current_value: int = rome_to_integer[reversed_s[idx]]
previous_value: int = rome_to_integer[reversed_s[idx-1]]
if current_value < previous_value:
answer -= current_value
else:
answer += current_value
return answer
if __name__ == "__main__":
cases: list[dict[str, dict[str, str]] | int] = [
{
"input": {"s": "III"},
"output": 3
},
{
"input": {"s": "LVIII"},
"output": 58
},
{
"input": {"s": "MCMXCIV"},
"output": 1994
}
]
for case in cases:
assert case["output"] == solution(s=case["input"]["s"])
assert case["output"] == another_solution(s=case["input"]["s"])