Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by atikin88
def checkio(data):
rom_dig = (('I','V'),('X','L'),('C','D'),('M',''))
rom=''
for i in range(3,-1,-1):
foo = data // (10**i)
data = data - foo * (10**i)
if not foo:
continue
if foo == 4:
rom += rom_dig[i][0] + rom_dig[i][1]
elif foo == 9:
rom += rom_dig[i][0] + rom_dig[i + 1][0]
else:
rom += rom_dig[i][1]*(foo//5) + rom_dig[i][0]*(foo%5)
return rom
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!')
Feb. 12, 2020