Update roman_numerals.py

This commit is contained in:
lighting9999 2025-02-10 16:18:58 +08:00 committed by GitHub
parent f5cdd52aa0
commit eb8598f239
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,35 +8,46 @@ ROMAN = [
] ]
def roman_to_int(roman: str) -> int: def roman_to_int(roman: str) -> int:
""" """
Convert a Roman numeral to an integer, supporting Vinculum notation (underscore _ represents 1000 times). Convert a Roman numeral to an integer, supporting Vinculum notation
LeetCode No. 13 Roman to Integer (underscore _ represents 1000 times).
    Given a roman numeral, convert it to an integer.
    Input is guaranteed to be within the range from 1 to 3999. LeetCode No. 13 Roman to Integer:
    https://en.wikipedia.org/wiki/Roman_numerals Given a Roman numeral, convert it to an integer.
    >>> all(roman_to_int(key) == value for key, value in tests.items()) Input is guaranteed to be within the range from 1 to 3999.
>>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999, "I_V_": 4000, "X_": 10000, "M_": 1000000}
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}
>>> all(roman_to_int(key) == value for key, value in tests.items()) >>> all(roman_to_int(key) == value for key, value in tests.items())
True True
""" """
vals = { vals = {
"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000,
"I_": 1000, "V_": 5000, "X_": 10000, "L_": 50000, "C_": 100000, "D_": 500000, "M_": 1000000 "I_": 1000, "V_": 5000, "X_": 10000, "L_": 50000, "C_": 100000,
"D_": 500000, "M_": 1000000
} }
i, total = 0, 0 i, total = 0, 0
while i < len(roman): while i < len(roman):
if i + 1 < len(roman) and (roman[i:i+2] in vals): # 处理 `_` 记法 if i + 1 < len(roman) and roman[i:i+2] in vals:
total += vals[roman[i:i+2]] total += vals[roman[i:i+2]]
i += 2 i += 2
else: else:
total += vals[roman[i]] total += vals[roman[i]]
i += 1 i += 1
return total return total
def int_to_roman(number: int) -> str:
def int_to_roman(number: int) -> str:
""" """
Convert an integer to a Roman numeral, supporting Vinculum notation (underscore _ represents 1000 times). Convert an integer to a Roman numeral, supporting Vinculum notation
 Given a integer, convert it to an roman numeral. (underscore _ represents 1000 times).
    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} Given an integer, convert it to a Roman numeral.
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}
>>> all(int_to_roman(value) == key for key, value in tests.items()) >>> all(int_to_roman(value) == key for key, value in tests.items())
True True
""" """
@ -48,9 +59,9 @@ def roman_to_int(roman: str) -> int:
factor, number = divmod(number, arabic) factor, number = divmod(number, arabic)
result.append(roman * factor) result.append(roman * factor)
if number == 0: if number == 0:
break break
return "".join(result) return "".join(result)
if __name__ == "__main__": if __name__ == "__main__":
import doctest import doctest
doctest.testmod() doctest.testmod()