Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by Karooo
def checkio(data):
roman = ('I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M')
print(data)
i = 0
romnum = ''
while data > 0:
r = data % 10
print('r',r)
data //= 10
print('d',data)
if r < 4:
romnum = r * roman[i] + romnum
if r == 4:
romnum = roman[i+1] + romnum
if r >= 5 and r < 9:
romnum = roman[i+2] + (r - 5) * roman[i] + romnum
if r == 9:
romnum = roman[i+3] + romnum
i += 4
return romnum
return ""
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
print(checkio(222))
assert checkio(6) == 'VI', '6'
assert checkio(76) == 'LXXVI', '76'
assert checkio(499) == 'CDXCIX', '499'
assert checkio(3888) == 'MMMDCCCLXXXVIII', '3888'
print('Done! Go Check!')
Nov. 26, 2018