Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple solution with tuple solution in Clear category for Roman Numerals by CaptainFest
def checkio(data):
num = ('I', 'V', 'X', 'L', 'C', 'D', 'M') # 1 = 'I', 5 = 'V'
rim_num = '' # 10 = 'X', 50= 'L'
for i in range(len(str(data))): # 100 ='C', 500='D'
cifra = data // (10 ** i) % 10 # 1000='M'
if cifra != 0:
if cifra < 4:
rim_num = cifra * num[i*2] + rim_num
if cifra == 4:
rim_num = (num[i*2] + num[i*2+1]) + rim_num
if cifra == 5:
rim_num = num[i*2+1] + rim_num
if 5 < cifra < 9:
rim_num = (num[i*2+1] + (cifra - 5) * num[2*i]) + rim_num
if cifra == 9:
rim_num = (num[i*2] + num[i*2+2]) + rim_num
return rim_num
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!')
Aug. 5, 2018