Skip to content

Commit 1735aed

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent bcfd98c commit 1735aed

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

conversions/roman_numerals.py

+45-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
ROMAN = [
2-
(1000000, "M_"), (900000, "C_M_"), (500000, "D_"), (400000, "C_D_"),
3-
(100000, "C_"), (90000, "X_C_"), (50000, "L_"), (40000, "X_L_"),
4-
(10000, "X_"), (9000, "I_X_"), (5000, "V_"), (4000, "I_V_"),
5-
(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"),
6-
(100, "C"), (90, "XC"), (50, "L"), (40, "XL"),
7-
(10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I")
2+
(1000000, "M_"),
3+
(900000, "C_M_"),
4+
(500000, "D_"),
5+
(400000, "C_D_"),
6+
(100000, "C_"),
7+
(90000, "X_C_"),
8+
(50000, "L_"),
9+
(40000, "X_L_"),
10+
(10000, "X_"),
11+
(9000, "I_X_"),
12+
(5000, "V_"),
13+
(4000, "I_V_"),
14+
(1000, "M"),
15+
(900, "CM"),
16+
(500, "D"),
17+
(400, "CD"),
18+
(100, "C"),
19+
(90, "XC"),
20+
(50, "L"),
21+
(40, "XL"),
22+
(10, "X"),
23+
(9, "IX"),
24+
(5, "V"),
25+
(4, "IV"),
26+
(1, "I"),
827
]
28+
29+
930
def roman_to_int(roman: str) -> int:
1031
"""
1132
Convert a Roman numeral to an integer, supporting Vinculum notation
@@ -22,15 +43,26 @@ def roman_to_int(roman: str) -> int:
2243
True
2344
"""
2445
vals = {
25-
"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000,
26-
"I_": 1000, "V_": 5000, "X_": 10000, "L_": 50000, "C_": 100000,
27-
"D_": 500000, "M_": 1000000
46+
"I": 1,
47+
"V": 5,
48+
"X": 10,
49+
"L": 50,
50+
"C": 100,
51+
"D": 500,
52+
"M": 1000,
53+
"I_": 1000,
54+
"V_": 5000,
55+
"X_": 10000,
56+
"L_": 50000,
57+
"C_": 100000,
58+
"D_": 500000,
59+
"M_": 1000000,
2860
}
2961

3062
i, total = 0, 0
3163
while i < len(roman):
32-
if i + 1 < len(roman) and roman[i:i+2] in vals:
33-
total += vals[roman[i:i+2]]
64+
if i + 1 < len(roman) and roman[i : i + 2] in vals:
65+
total += vals[roman[i : i + 2]]
3466
i += 2
3567
else:
3668
total += vals[roman[i]]
@@ -62,6 +94,8 @@ def int_to_roman(number: int) -> str:
6294
break
6395
return "".join(result)
6496

97+
6598
if __name__ == "__main__":
6699
import doctest
100+
67101
doctest.testmod()

0 commit comments

Comments
 (0)