Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
template solution in Clear category for Roman Numerals by abe.dillon
low, mid, high = "{0[0]}", "{0[1]}", "{0[2]}"
templates = ("",low,low*2,low*3,low+mid,mid,mid+low,mid+low*2,mid+low*3,low+high)
numerals = "IVXLCDM__"
def checkio(num):
chars, n = [], 0
while num:
num, place = divmod(num, 10)
template = templates[place]
chars.append(template.format(numerals[n:n+4]) if template else "")
n += 2
return "".join(chars[::-1])
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'
Feb. 22, 2015