2022-10-16 10:33:29 +01:00
|
|
|
|
ROMAN = [
|
2025-02-10 09:49:05 +00:00
|
|
|
|
(1000000, "M_"),
|
|
|
|
|
(900000, "C_M_"),
|
|
|
|
|
(500000, "D_"),
|
|
|
|
|
(400000, "C_D_"),
|
|
|
|
|
(100000, "C_"),
|
|
|
|
|
(90000, "X_C_"),
|
|
|
|
|
(50000, "L_"),
|
|
|
|
|
(40000, "X_L_"),
|
|
|
|
|
(10000, "X_"),
|
|
|
|
|
(9000, "I_X_"),
|
|
|
|
|
(5000, "V_"),
|
|
|
|
|
(4000, "I_V_"),
|
|
|
|
|
(1000, "M"),
|
|
|
|
|
(900, "CM"),
|
|
|
|
|
(500, "D"),
|
|
|
|
|
(400, "CD"),
|
|
|
|
|
(100, "C"),
|
|
|
|
|
(90, "XC"),
|
|
|
|
|
(50, "L"),
|
|
|
|
|
(40, "XL"),
|
|
|
|
|
(10, "X"),
|
|
|
|
|
(9, "IX"),
|
|
|
|
|
(5, "V"),
|
|
|
|
|
(4, "IV"),
|
|
|
|
|
(1, "I"),
|
2022-10-16 10:33:29 +01:00
|
|
|
|
]
|
2025-02-10 09:49:05 +00:00
|
|
|
|
|
|
|
|
|
|
2025-02-10 17:48:43 +08:00
|
|
|
|
def roman_to_int(roman):
|
2019-12-20 18:23:15 -05:00
|
|
|
|
"""
|
2025-02-10 08:19:12 +00:00
|
|
|
|
Convert a Roman numeral to an integer, supporting Vinculum notation
|
2025-02-10 16:18:58 +08:00
|
|
|
|
(underscore _ represents 1000 times).
|
|
|
|
|
|
|
|
|
|
LeetCode No. 13 Roman to Integer:
|
|
|
|
|
Given a Roman numeral, convert it to an integer.
|
|
|
|
|
Input is guaranteed to be within the range from 1 to 3999.
|
|
|
|
|
|
|
|
|
|
Reference: https://en.wikipedia.org/wiki/Roman_numerals
|
|
|
|
|
>>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500,
|
|
|
|
|
... "MMMCMXCIX": 3999, "I_V_": 4000, "X_": 10000, "M_": 1000000}
|
2019-12-20 18:23:15 -05:00
|
|
|
|
>>> all(roman_to_int(key) == value for key, value in tests.items())
|
|
|
|
|
True
|
|
|
|
|
"""
|
2025-02-10 17:48:43 +08:00
|
|
|
|
vals = {roman: arabic for arabic, roman in ROMAN}
|
|
|
|
|
# Convert the list of tuples to a dictionary
|
2025-02-10 08:19:12 +00:00
|
|
|
|
|
2025-02-10 11:44:48 +08:00
|
|
|
|
i, total = 0, 0
|
|
|
|
|
while i < len(roman):
|
2025-02-10 17:48:43 +08:00
|
|
|
|
# 先匹配 2 个字符的罗马数字(如 I_、X_)
|
2025-02-10 09:49:05 +00:00
|
|
|
|
if i + 1 < len(roman) and roman[i : i + 2] in vals:
|
|
|
|
|
total += vals[roman[i : i + 2]]
|
2025-02-10 11:44:48 +08:00
|
|
|
|
i += 2
|
2019-12-20 18:23:15 -05:00
|
|
|
|
else:
|
2025-02-10 11:44:48 +08:00
|
|
|
|
total += vals[roman[i]]
|
|
|
|
|
i += 1
|
2019-12-20 18:23:15 -05:00
|
|
|
|
return total
|
2025-02-10 09:49:05 +00:00
|
|
|
|
|
|
|
|
|
|
2025-02-10 17:48:43 +08:00
|
|
|
|
def int_to_roman(number):
|
2020-12-21 13:55:59 -08:00
|
|
|
|
"""
|
2025-02-10 08:19:12 +00:00
|
|
|
|
Convert an integer to a Roman numeral, supporting Vinculum notation
|
2025-02-10 16:18:58 +08:00
|
|
|
|
(underscore _ represents 1000 times).
|
|
|
|
|
|
|
|
|
|
Given an integer, convert it to a Roman numeral.
|
|
|
|
|
|
2025-02-10 17:25:30 +08:00
|
|
|
|
Reference: https://en.wikipedia.org/wiki/Roman_numerals
|
|
|
|
|
>>> tests = {3: "III", 154: "CLIV", 1009: "MIX", 2500: "MMD", 3999: "MMMCMXCIX"}
|
2020-12-21 13:55:59 -08:00
|
|
|
|
>>> all(int_to_roman(value) == key for key, value in tests.items())
|
|
|
|
|
True
|
|
|
|
|
"""
|
2025-02-10 11:44:48 +08:00
|
|
|
|
if not isinstance(number, int) or number < 1:
|
|
|
|
|
raise ValueError("Input must be a positive integer greater than 0")
|
|
|
|
|
|
2020-12-21 13:55:59 -08:00
|
|
|
|
result = []
|
2023-02-01 14:14:54 +01:00
|
|
|
|
for arabic, roman in ROMAN:
|
2025-02-10 11:44:48 +08:00
|
|
|
|
factor, number = divmod(number, arabic)
|
2020-12-21 13:55:59 -08:00
|
|
|
|
result.append(roman * factor)
|
|
|
|
|
if number == 0:
|
2025-02-10 16:18:58 +08:00
|
|
|
|
break
|
2020-12-21 13:55:59 -08:00
|
|
|
|
return "".join(result)
|
2025-02-10 09:49:05 +00:00
|
|
|
|
|
|
|
|
|
|
2025-02-10 16:33:21 +08:00
|
|
|
|
if __name__ == "__main__":
|
2025-02-10 16:29:23 +08:00
|
|
|
|
import doctest
|
2025-02-10 09:49:05 +00:00
|
|
|
|
|
2025-02-10 16:29:23 +08:00
|
|
|
|
doctest.testmod()
|