Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
comprehensive solution in Creative category for Roman Numerals by Renelvon
SYMBOLS = {0:['I', 'V', 'X'], 1:['X', 'L', 'C'], 2:['C', 'D', 'M'], 3:['M']}
ENCODING = [[], #0
[(0, 1)], [(0, 2)], [(0, 3)], [(0, 1), (1, 1)], #1, 2, 3, 4
[(1, 1)], [(1, 1), (0, 1)], [(1, 1), (0, 2)], #5, 6, 7
[(1, 1), (0, 3)], [(0, 1), (2, 1)]] #8, 9
def checkio(data):
digits = [int(x) for x in str(data)]
digits.reverse()
roman = [''.join(
[
SYMBOLS[order][i] * rep
for i, rep in ENCODING[digit]
])
for order, digit in enumerate(digits)
]
roman.reverse()
return ''.join(roman)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(6) == 'VI', '6'
assert checkio(76) == 'LXXVI', '76'
assert checkio(499) == 'CDXCIX', '499'
assert checkio(3888) == 'MMMDCCCLXXXVIII', '3888'
Feb. 13, 2014
Comments: