Integer to roman numeral - Solutions ==================================== .. contents:: :local: .. _pwp-i2rn-ex1: Exercise -------- Solution ^^^^^^^^ .. interactive_code_block:: :caption: A function that converts an integer to a roman numeral def integer_to_roman_numeral(integer): """Convert an integer to a roman numeral.""" # Create a dict with the roman symbols and their integer values roman_symbols = { 'M':1000, 'CM':900, 'D':500, 'CD':400, 'C': 100, 'XC': 90, 'L': 50, 'XL': 40, 'X': 10, 'IX': 9, 'V': 5, 'IV': 4, 'I': 1 } # Create a variable to be able to add the roman symbols roman_numeral = '' # Substract the values of the roman symbols as long as the integer is greater then 0 while integer > 0: # Iterate over the dict with the roman symbols for roman_symbol, value in roman_symbols.items(): # Calculate how many times the roman symbol has to be added repetitions = integer // value # Add the symbol(s) to the result roman_numeral += roman_symbol * repetitions # Substract the values from the integer integer -= value * repetitions return roman_numeral # Create a dict with test values test_values = {'CXXIII': 123, 'CCCXXI':321, 'CDXLIV':444, 'CMXCIX': 999, 'MMXXI': 2021} # Print a table with test values and the result of the function print(' Test values | Result') print('----------------+-------') for roman_numeral, integer in test_values.items(): print('{:6} | {:>6} | {:>6}'.format(integer, roman_numeral, integer_to_roman_numeral(integer))) Shorter but less explicit solution ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The code above can be shortened by using less variables but, since *only* the variable ``repetitions`` can be removed and it's important to use round brackets ``(integer // value)`` to maintain the order of precedence of the operators, the overall benefit is small. .. interactive_code_block:: :caption: A function that converts an integer to a roman numeral - shorter but less explicit solution def integer_to_roman_numeral(integer): """Convert an integer to a roman numeral.""" # Create a dict with the roman symbols and their integer values roman_symbols = { 'M':1000, 'CM':900, 'D':500, 'CD':400, 'C': 100, 'XC': 90, 'L': 50, 'XL': 40, 'X': 10, 'IX': 9, 'V': 5, 'IV': 4, 'I': 1 } # Create a variable to be able to add the roman symbols roman_numeral = '' # Substract the values of the roman symbols as long as the integer is greater then 0 while integer > 0: # Iterate over the dict with the roman symbols for roman_symbol, value in roman_symbols.items(): # Add the symbol(s) to the result roman_numeral += roman_symbol * (integer // value) # Substract the values from the integer integer -= value * (integer // value) return roman_numeral # Create a dict with test values test_values = {'CXXIII': 123, 'CCCXXI':321, 'CDXLIV':444, 'CMXCIX': 999, 'MMXXI': 2021} # Print a table with test values and the result of the function print(' Test values | Result') print('----------------+-------') for roman_numeral, integer in test_values.items(): print('{:6} | {:>6} | {:>6}'.format(integer, roman_numeral, integer_to_roman_numeral(integer)))