<div dir="ltr"><div>''' traduce numeri romani ,in decimali e viceversa '''</div><div>import unittest</div><div>num_romani = 'MDCLXVI'</div><div>ammessi = {'alfa':1001,'M':1000,'D':500,'C':100,'L':50,'X':10,'V':5,'I':   1}</div><div>def controllaLegalita(numax):</div><div><span class="" style="white-space:pre">    </span>''' torna None se condizioni di errore</div><div><span class="" style="white-space:pre">         </span>il valore decimale se corretto</div><div><span class="" style="white-space:pre">     </span>'''</div><div><span class="" style="white-space:pre">    </span>if numax == "": return # caso della stringa vuota</div><div><span class="" style="white-space:pre">        </span>num = numax.upper()</div><div><span class="" style="white-space:pre">        </span>scoP = adjPP = adjP = 'alfa'</div><div><span class="" style="white-space:pre">       </span>diFila = valorD = 0</div><div><span class="" style="white-space:pre">        </span>for pos,  car in enumerate(num):</div><div><span class="" style="white-space:pre">          </span>if car not in num_romani:return</div><div><span class="" style="white-space:pre">            </span>if ammessi[adjP] >= ammessi[car]:</div><div><span class="" style="white-space:pre">                       </span>if ammessi[scoP] <= ammessi[car]:return</div><div><span class="" style="white-space:pre">                 </span>if adjP == car:</div><div><span class="" style="white-space:pre">                            </span>if car in 'DLV' or diFila == 2:return</div><div><span class="" style="white-space:pre">                              </span>diFila +=1</div><div><span class="" style="white-space:pre">                 </span>else:</div><div><span class="" style="white-space:pre">                              </span>diFila = 0</div><div><span class="" style="white-space:pre">                 </span>adjPP = adjP # se dopo avrò uno sconto dovrò riaggiornare adjP!</div><div><span class="" style="white-space:pre">                  </span>adjP  = car</div><div><span class="" style="white-space:pre">                       </span>valorD += ammessi[car]</div><div><span class="" style="white-space:pre">                     </span>continue</div><div><span class="" style="white-space:pre">           </span>if ammessi[adjPP] < ammessi[car] or not(ammessi[car]//10 <= ammessi[adjP] < ammessi[car]//2):return</div><div><span class="" style="white-space:pre">               </span>if ammessi[adjPP] < ammessi[adjP]:return</div><div><span class="" style="white-space:pre">                </span>valorD += ammessi[car] -2 * ammessi[adjP]</div><div><span class="" style="white-space:pre">          </span>scoP = adjP</div><div><span class="" style="white-space:pre">                </span>adjP = car</div><div><span class="" style="white-space:pre">         </span>diFila = 2 # non si può inserire un MCM</div><div><span class="" style="white-space:pre">           </span>continue</div><div><span class="" style="white-space:pre">   </span>return valorD</div><div><br></div><div>def parto_da_qui(nome):</div><div><span class="" style="white-space:pre">       </span>while True:</div><div><span class="" style="white-space:pre">                </span>a = input("un numero romano or 'fine' ?")</div><div><span class="" style="white-space:pre">                </span>if a == 'fine':break</div><div><span class="" style="white-space:pre">       </span>a = controllaLegalita(nome)</div><div><span class="" style="white-space:pre">        </span>print(a)</div><div>if __name__ == '__main__':</div><div><span class="" style="white-space:pre">  </span>unittest.main()</div><div><br></div><div><br></div></div>