88]
99def roman_to_int (roman : str ) -> int :
1010 """
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}
1821 >>> all(roman_to_int(key) == value for key, value in tests.items())
1922 True
2023 """
2124 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
2428 }
29+
2530 i , total = 0 , 0
2631 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 :
2833 total += vals [roman [i :i + 2 ]]
2934 i += 2
3035 else :
3136 total += vals [roman [i ]]
3237 i += 1
3338 return total
34- def int_to_roman (number : int ) -> str :
39+
40+
41+ def int_to_roman (number : int ) -> str :
3542 """
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}
4051 >>> all(int_to_roman(value) == key for key, value in tests.items())
4152 True
4253 """
@@ -48,9 +59,9 @@ def int_to_roman(number: int) -> str:
4859 factor , number = divmod (number , arabic )
4960 result .append (roman * factor )
5061 if number == 0 :
51- break
62+ break
5263 return "" .join (result )
53-
54- if __name__ == "__main__" :
64+
65+ if __name__ == "__main__" :
5566 import doctest
5667 doctest .testmod ()
0 commit comments