Skip to content

Commit e558319

Browse files
committed
add roman to integer alg
1 parent 7c02e25 commit e558319

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
3+
4+
Symbol Value
5+
I 1
6+
V 5
7+
X 10
8+
L 50
9+
C 100
10+
D 500
11+
M 1000
12+
13+
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.
14+
15+
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:
16+
17+
I can be placed before V (5) and X (10) to make 4 and 9.
18+
X can be placed before L (50) and C (100) to make 40 and 90.
19+
C can be placed before D (500) and M (1000) to make 400 and 900.
20+
21+
Given a roman numeral, convert it to an integer.
22+
23+
24+
Example 1:
25+
26+
Input: s = "III"
27+
Output: 3
28+
Explanation: III = 3.
29+
30+
Example 2:
31+
32+
Input: s = "LVIII"
33+
Output: 58
34+
Explanation: L = 50, V= 5, III = 3.
35+
36+
Example 3:
37+
38+
Input: s = "MCMXCIV"
39+
Output: 1994
40+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
41+
42+
"""
43+
roman_dict = {
44+
"I": 1,
45+
"V": 5,
46+
"X": 10,
47+
"L": 50,
48+
"C": 100,
49+
"D": 500,
50+
"M": 1000
51+
}
52+
53+
def romanToInt(s: str) -> int: # noqa
54+
""" This function convert valid roman int to modern int.
55+
56+
Notes:
57+
1. This function used cache to store tmp info.
58+
2. Average complexity is O(N), but in the worst case it is going to be O(N^2).
59+
60+
Args:
61+
s: string representation of valid roman values.
62+
63+
Returns:
64+
modern int representation of value.
65+
"""
66+
cache_roman = []
67+
integer = 0
68+
c_ind = 0
69+
length = len(s)
70+
71+
while c_ind < length:
72+
current_roman = s[c_ind]
73+
74+
if len(cache_roman) == 0: # если ничего нет, то обновляем информацию
75+
cache_roman.append(current_roman)
76+
else:
77+
past_val = roman_dict.get(cache_roman[0])
78+
current_val = roman_dict.get(current_roman)
79+
if cache_roman[-1] != current_roman and past_val > current_val:
80+
for ind in range(len(cache_roman)):
81+
cache_roman.pop(-1)
82+
integer += past_val
83+
cache_roman.append(current_roman)
84+
elif cache_roman[-1] != current_roman and past_val < current_val:
85+
cache_roman.pop(-1)
86+
new_val = current_val - past_val
87+
integer += new_val
88+
else:
89+
cache_roman.append(current_roman)
90+
91+
c_ind += 1
92+
93+
if len(cache_roman) != 0:
94+
for ind in range(len(cache_roman)):
95+
val = cache_roman.pop(-1)
96+
integer += roman_dict.get(val)
97+
return integer
98+
99+
if __name__ == "__main__":
100+
input = "MCMXCIV" # noqa
101+
output = 1994
102+
if romanToInt(input) == output:
103+
print("It is ok!")

0 commit comments

Comments
 (0)