Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Should be easy to understand - with comments solution in Clear category for Roman Numerals by Selindian
def checkio(data):
# Define a dict for all cases (don't miss the "before" cases like XC or IV).
roman_arabic = {"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}
roman_number = ""
for item in roman_arabic.items(): # Loop over all items of our dict ...
roman, divisor = item # ... get roman and divisor from item
quotient, data = divmod(data, divisor) # ... use divmod to calculate quotient and remainder (new data)
roman_number += roman * quotient # ... add romain letter multiplied with quotinet to string
return roman_number
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(6) == 'VI', '6'
assert checkio(76) == 'LXXVI', '76'
assert checkio(499) == 'CDXCIX', '499'
assert checkio(3888) == 'MMMDCCCLXXXVIII', '3888'
print('Done! Go Check!')
Sept. 6, 2022
Comments: