diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 75af2ac72882..2e1d5f7026a5 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,4 +1,16 @@ ROMAN = [ + (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"), @@ -15,40 +27,26 @@ ] -def roman_to_int(roman: str) -> int: - """ - 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. - https://en.wikipedia.org/wiki/Roman_numerals - >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999} - >>> all(roman_to_int(key) == value for key, value in tests.items()) - True - """ - vals = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} - total = 0 - place = 0 - while place < len(roman): - if (place + 1 < len(roman)) and (vals[roman[place]] < vals[roman[place + 1]]): - total += vals[roman[place + 1]] - vals[roman[place]] - place += 2 +def roman_to_int(roman): + vals = {roman: arabic for arabic, roman in ROMAN} + i, total = 0, 0 + while i < len(roman): + if i + 1 < len(roman) and roman[i + 1] == "_": + total += vals[roman[i] + "_"] + i += 2 else: - total += vals[roman[place]] - place += 1 + total += vals[roman[i]] + i += 1 return total -def int_to_roman(number: int) -> str: - """ - Given a integer, convert it to an roman numeral. - https://en.wikipedia.org/wiki/Roman_numerals - >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999} - >>> all(int_to_roman(value) == key for key, value in tests.items()) - True - """ +def int_to_roman(number): + if not isinstance(number, int) or number <= 0: + raise ValueError("Input must be a positive integer greater than 0") + result = [] for arabic, roman in ROMAN: - (factor, number) = divmod(number, arabic) + factor, number = divmod(number, arabic) result.append(roman * factor) if number == 0: break