Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by marcelina.gorzelana
def checkio(data):
res = ""
rom = {1 : 'I', 5 : 'V', 10 : 'X', 50 : 'L', 100 : 'C', 500 : 'D', 1000 : 'M'}
s = str(data)
for i in range (len(s)-1, -1, -1):
#print(int(s[i]) * pow(10, len(s)-i-1))
if int(s[i]) * pow(10, len(s)-i-1):
if int(s[i]) < 4:
for j in range(int(s[i])%5):
res = rom[pow(10, len(s)-i-1)] + res
elif int(s[i]) == 4:
res = rom[pow(10, len(s)-i-1)] + rom[5 * pow(10, len(s)-i-1)] + res
elif int(s[i]) == 9:
res = rom[pow(10, len(s)-i-1)] + rom[pow(10, len(s)-i)] + res
else:
for j in range(int(s[i])%5):
res = rom[pow(10, len(s)-i-1)] + res
res = rom[5 * pow(10, len(s)-i-1)] + res
#print((int(s[i]) * pow(10, len(s)-i-1)) % (5 * pow(10, len(s)-i-1)))
#print(res)
return res
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'
Nov. 20, 2016