diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 7a22dd68b..12a9e49b2 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,20 +1,33 @@ ROMAN_NUMERALS = { - 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" + 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", } + def roman_to_int(roman): result = 0 i = 0 while i < len(roman): - if i + 1 < len(roman) and roman[i:i+2] in ROMAN_NUMERALS: - result += ROMAN_NUMERALS[roman[i:i+2]] + if i + 1 < len(roman) and roman[i : i + 2] in ROMAN_NUMERALS: + result += ROMAN_NUMERALS[roman[i : i + 2]] i += 2 else: result += ROMAN_NUMERALS[roman[i]] i += 1 return result + def int_to_roman(number): result = "" for value, numeral in ROMAN_NUMERALS.items(): @@ -23,6 +36,8 @@ def int_to_roman(number): number -= value return result + if __name__ == "__main__": import doctest + doctest.testmod()