-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman_to_integer.py
45 lines (42 loc) · 1.13 KB
/
roman_to_integer.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
class Solution:
def romanToInt(self, s: str) -> int:
integer_first = {
"IV": 4,
"VI": 6,
"VII": 7,
"VIII": 8,
"IX": 9,
"XII": 12,
"XIII": 13,
"XIV": 14,
"XV": 15,
"XVI": 16,
"XVII": 17,
"XVIII": 18,
"XIX": 19,
"XX": 20,
"XXX": 30,
"XL": 40,
"LX": 60,
"LXX": 70,
"LXXX": 80,
"XC": 90,
"CM": 900,
"CD": 400,
"I": 1,
"II": 2,
"III": 3,
"V": 5,
"XI": 11,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000,
}
# Сразу ищем подходящие ключи, чтобы оптимизировать процесс
keys_in_s_first = [k for k in integer_first if k in s]
for k in keys_in_s_first:
s = s.replace(k, f"{str(integer_first[k])},")
integer = list(map(int, s.split(',')[:-1]))
return sum(integer)