Chiffres romains vers chiffres arabes et inversement.
def a2r(n): if n not in range(1,5000): raise ValueError("Entre 1 et 4999.") chiffres=["I","V","X","L","C","D"] r="" for i in range(3): n,u=divmod(n,10) if u in range(1,4): r=chiffres[2*i]*u+r elif u==4: r=chiffres[2*i]+chiffres[2*i+1]+r elif u in range(5,9): r=chiffres[2*i+1]+chiffres[2*i]*(u-5)+r elif u==9: r=chiffres[2*i]+chiffres[2*i+2]+r return "M"*n+r def r2a(c): valeurs={"M":1000,"D":500,"C":100,"L":50,"X":10,"V":5,"I":1,"\n":0} liste=[valeurs[i] for i in c] for i in range(len(c)-1): if liste[i]<liste[i+1]: liste[i]*=-1 resultat=sum(liste) if a2r(resultat)==c: return resultat raise ValueError("Nombre mal construit.")