Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
OrderedDict divmod solution in Clear category for Roman Numerals by Striga
from collections import OrderedDict
def checkio(data):
result = ''
roman = OrderedDict()
roman[1000] = "M"
roman[900] = "CM"
roman[500] = "D"
roman[400] = "CD"
roman[100] = "C"
roman[90] = "XC"
roman[50] = "L"
roman[40] = "XL"
roman[10] = "X"
roman[9] = "IX"
roman[5] = "V"
roman[4] = "IV"
roman[1] = "I"
while data > 0:
for value in roman.keys():
quotient, remainder = divmod(data, value) #or data//value
result += roman[value] * quotient
data -= (value * quotient)
return result
if __name__ == '__main__':
assert checkio(6) == 'VI', '6'
assert checkio(76) == 'LXXVI', '76'
assert checkio(499) == 'CDXCIX', '499'
assert checkio(3888) == 'MMMDCCCLXXXVIII', '3888'
Nov. 6, 2020
Comments: