Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Speedy category for Roman Numerals by JamesNippoc
D = [1,5,10,50,100,500,1000]
R = ['I','V','X','L','C','D','M']
def ecrire(d,n):
if n==0: return ""
q,r = d//(10**(n-1)),d%(10**(n-1))
if q==9: return R[(n-1)*2]+R[n*2]+ecrire(d- D[n*2]-D[(n-1)*2],n)
if q==4: return R[(n-1)*2]+R[n*2-1]+ecrire(d-D[n*2-1]-D[(n-1)*2],n)
if q<=3: return q*R[(n-1)*2]+ ecrire(r, n-1)
if q>=5: return R[n*2-1]+ecrire(d-5*10**(n-1),n)
def checkio(d):
return ecrire(d,4)
#These "asserts" using only for self-checking and not necessary for auto-testing
May 22, 2014
Comments: