8
8
]
9
9
def roman_to_int (roman : str ) -> int :
10
10
"""
11
- Convert a Roman numeral to an integer, supporting Vinculum notation (underscore _ represents 1000 times).
12
- LeetCode No. 13 Roman to Integer
13
- Given a roman numeral, convert it to an integer.
14
- Input is guaranteed to be within the range from 1 to 3999.
15
- https://en.wikipedia.org/wiki/Roman_numerals
16
- >>> all(roman_to_int(key) == value for key, value in tests.items())
17
- >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999, "I_V_": 4000, "X_": 10000, "M_": 1000000}
11
+ Convert a Roman numeral to an integer, supporting Vinculum notation
12
+ (underscore _ represents 1000 times).
13
+
14
+ LeetCode No. 13 Roman to Integer:
15
+ Given a Roman numeral, convert it to an integer.
16
+ Input is guaranteed to be within the range from 1 to 3999.
17
+
18
+ Reference: https://en.wikipedia.org/wiki/Roman_numerals
19
+ >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500,
20
+ ... "MMMCMXCIX": 3999, "I_V_": 4000, "X_": 10000, "M_": 1000000}
18
21
>>> all(roman_to_int(key) == value for key, value in tests.items())
19
22
True
20
23
"""
21
24
vals = {
22
- "I" : 1 , "V" : 5 , "X" : 10 , "L" : 50 , "C" : 100 , "D" : 500 , "M" : 1000 ,
23
- "I_" : 1000 , "V_" : 5000 , "X_" : 10000 , "L_" : 50000 , "C_" : 100000 , "D_" : 500000 , "M_" : 1000000
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
24
28
}
29
+
25
30
i , total = 0 , 0
26
31
while i < len (roman ):
27
- if i + 1 < len (roman ) and ( roman [i :i + 2 ] in vals ): # 处理 `_` 记法
32
+ if i + 1 < len (roman ) and roman [i :i + 2 ] in vals :
28
33
total += vals [roman [i :i + 2 ]]
29
34
i += 2
30
35
else :
31
36
total += vals [roman [i ]]
32
37
i += 1
33
38
return total
34
- def int_to_roman (number : int ) -> str :
39
+
40
+
41
+ def int_to_roman (number : int ) -> str :
35
42
"""
36
- Convert an integer to a Roman numeral, supporting Vinculum notation (underscore _ represents 1000 times).
37
- Given a integer, convert it to an roman numeral.
38
- https://en.wikipedia.org/wiki/Roman_numerals
39
- >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999, "I_V_": 4000, "X_": 10000, "M_": 1000000}
43
+ Convert an integer to a Roman numeral, supporting Vinculum notation
44
+ (underscore _ represents 1000 times).
45
+
46
+ Given an integer, convert it to a Roman numeral.
47
+
48
+ Reference:https://en.wikipedia.org/wiki/Roman_numerals
49
+ >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500,
50
+ ... "MMMCMXCIX": 3999, "I_V_": 4000, "X_": 10000, "M_": 1000000}
40
51
>>> all(int_to_roman(value) == key for key, value in tests.items())
41
52
True
42
53
"""
@@ -48,9 +59,9 @@ def int_to_roman(number: int) -> str:
48
59
factor , number = divmod (number , arabic )
49
60
result .append (roman * factor )
50
61
if number == 0 :
51
- break
62
+ break
52
63
return "" .join (result )
53
-
54
- if __name__ == "__main__" :
64
+
65
+ if __name__ == "__main__" :
55
66
import doctest
56
67
doctest .testmod ()
0 commit comments