Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Roman Numerals by juestr
from functools import reduce
roman = 'MDCLXVI'
divisors = (100, 10, 1)
conv_stages = list(zip(divisors, roman[2::2], roman[1::2], roman[0::2]))
def checkio(n):
def extract10(result, stage):
nonlocal n
divisor, rsym1, rsym5, rsym10 = stage
q10, n = divmod(n, divisor)
q5, r5 = divmod(q10, 5)
q4, q1 = divmod(r5, 4)
parts = (result, rsym5, rsym1, rsym5, rsym10)
mults = (1, q5 and not q4, max(q1, q4), q4 and not q5, q4 and q5)
return ''.join(s * mult for s, mult in zip(parts, mults))
m, n = divmod(n, 1000)
return reduce(extract10, conv_stages, roman[0] * m)
May 21, 2019
Comments: