Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by Dracunos
def checkio(data):
if data == 0:
return ''
elif data < 4:
return ''.join(['I' for x in range(data)])
elif data == 4:
return 'IV'
elif data < 9:
return 'V' + checkio(data - 5)
elif data == 9:
return 'IX'
elif data < 40:
return 'X' + checkio(data - 10)
elif data < 50:
return 'XL' + checkio(data - 40)
elif data < 90:
return 'L' + checkio(data - 50)
elif data < 100:
return 'XC' + checkio(data - 90)
elif data < 400:
return 'C' + checkio(data - 100)
elif data < 500:
return 'CD' + checkio(data - 400)
elif data < 900:
return 'D' + checkio(data - 500)
elif data < 1000:
return 'CM' + checkio(data - 900)
else:
return 'M' + checkio(data - 1000)
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'
Dec. 23, 2015