Advertisement
Guest User

program02

a guest
Oct 19th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.79 KB | None | 0 0
  1. ''' In determinate occasioni ci capita di dover scrivere i numeri in lettere,
  2. ad esempio quando dobbiamo compilare un assegno.
  3. Puo' capitare che alcuni numeri facciano sorgere in noi qualche dubbio.
  4.  
  5. Le perplessita' nascono soprattutto nella scrittura dei numeri composti con 1 e 8.
  6. Tutti i numeri come venti, trenta, quaranta, cinquanta, ecc... elidono la vocale
  7. finale (la "i" per 20, la "a" per tutti gli altri) fondendola con la vocale iniziale
  8. del numero successivo; scriveremo quindi ventuno, ventotto, trentotto,
  9. cinquantuno ecc...
  10.  
  11. Il numero cento, nella formazione dei numeri composti con uno e otto, non si comporta
  12. cosi'; il numero "cento" e tutte le centinaia (duecento, trecento, ecc...),
  13. infatti, non elidono la vocale finale. Dunque non scriveremo centuno,  trecentotto ma centouno,
  14. trecentootto, ecc...
  15.  
  16. I numeri composti dalle centinaia e dalla decina "ottanta" invece tornano ad elidere
  17. la vocale finale; scriveremo quindi centottanta, duecentottanta,  ecc...,
  18. non centoottanta, duecentoottanta, ...
  19.  
  20. Il numero "mille" non elide in nessun numero composto la vocale finale; scriveremo
  21. quindi milleuno,  milleotto, milleottanta, ecc...
  22.  
  23. Altri esempi sono elencati nel file grade02.txt
  24.  
  25.  
  26. Scrivere una funzione conv(n) che prende in input un intero n, con 0<n<1000000000000,
  27. e restituisce in output una stringa con il numero espresso in lettere
  28.  
  29. ATTENZIONE: NON USATE LETTERE ACCENTATE.
  30. ATTENZIONE: Se il grader non termina entro 30 secondi il punteggio dell'esercizio e' zero.
  31. '''
  32.  
  33.  
  34. def conv(n):
  35.     stl=""
  36.     if(n <=20):
  37.         stl=st2(n)
  38.     elif n <100:
  39.         stl=st3(n)
  40.     elif n < 1000:
  41.         stl=st4(n)
  42.     elif n < 10000:
  43.         stl=st5(n)
  44.     elif n < 100000:
  45.         stl=st6(n)
  46.     elif n < 1000000:
  47.         stl=st7(n)
  48.     elif n < 10000000:
  49.         stl=st8(n)
  50.     elif n < 100000000:
  51.         stl=st9(n)
  52.     elif n < 1000000000:
  53.        stl=st10(n)
  54.        
  55.        
  56.     return stl
  57.  
  58. def st1(n1):
  59.     stl=""
  60.     if n1 == 1:
  61.         stl+= "uno"
  62.     if n1 == 2:
  63.         stl+= "due"
  64.     if n1 == 3:
  65.         stl+= "tre"
  66.     if n1 == 4:
  67.         stl+= "quattro"
  68.     if n1 == 5:
  69.         stl+= "cinque"
  70.     if n1 == 6:
  71.         stl+= "sei"
  72.     if n1 == 7:
  73.         stl+= "sette"
  74.     if n1 == 8:
  75.         stl+= "otto"
  76.     if n1 == 9:
  77.         stl+= "nove"
  78.     if n1 == 10:
  79.         stl+= "dieci"
  80.     return stl
  81.  
  82. def st2(n2):
  83.     stl=""
  84.     if(n2 >=1 and n2 <= 10):
  85.         stl+=st1(n2)
  86.     if(n2>10 and n2 <20):
  87.         d="dici"
  88.         if n2==11:
  89.             stl+="un" + d
  90.         if n2==12:
  91.             stl+="do" + d
  92.         if n2==13:
  93.             stl+="tre" + d
  94.         if n2==14:
  95.             stl+="quattor" + d
  96.         if n2==15:
  97.             stl+="quin" + d
  98.         if n2==16:
  99.             stl+="se" + d
  100.         if n2==17:
  101.             stl+= d + "assette"
  102.         if n2==18:
  103.             stl+= d + "otto"
  104.         if n2==19:
  105.             stl+= d + "annove"
  106.     return stl
  107.  
  108. def st3(n3):
  109.     stl=""
  110.     if(n3 < 20):
  111.         stl+=st2(n3)
  112.     else:  
  113.         if(n3 >= 20 and n3 < 30 ):
  114.             n0=int(str(n3)[1:])
  115.             v="venti"
  116.             if(n3==20):
  117.                 stl+=v
  118.             elif n0 == 1 or n0==8:
  119.                 stl+="vent" + st1(n0)
  120.             else:
  121.                 stl+= v + st1(n0)
  122.         if(n3 >= 30 and n3 < 40 ):
  123.             n0=int(str(n3)[1:])
  124.             t="trenta"
  125.             if(n3==30):
  126.                 stl+=t
  127.             elif n0 == 1 or n0==8:
  128.                 stl+="trent" + st1(n0)
  129.             else:
  130.                 stl+= t + st1(n0)
  131.         if(n3 >= 40 and n3 < 50 ):
  132.             n0=int(str(n3)[1:])
  133.             t="quaranta"
  134.             if(n3==40):
  135.                 stl+=t
  136.             elif n0 == 1 or n0==8:
  137.                 stl+="quarant" + st1(n0)
  138.             else:
  139.                 stl+=t + st1(n0)
  140.         if(n3 >= 50 and n3 < 60 ):
  141.             n0=int(str(n3)[1:])
  142.             t="cinquanta"
  143.             if(n3==50):
  144.                 stl+=t
  145.             elif n0 == 1 or n0==8:
  146.                 stl+="cinquant" + st1(n0)
  147.             else:
  148.                 stl+=t+ st1(n0)
  149.         if(n3 >= 60 and n3 < 70 ):
  150.             n0=int(str(n3)[1:])
  151.             t="sessanta"
  152.             if(n3==60):
  153.                 stl+=t
  154.             elif n0 == 1 or n0==8:
  155.                 stl+="sessant" + st1(n0)
  156.             else:
  157.                 stl+=t+ st1(n0)
  158.         if(n3 >= 70 and n3 < 80 ):
  159.             n0=int(str(n3)[1:])
  160.             t="settanta"
  161.             if(n3==70):
  162.                 stl+=t
  163.             elif n0 == 1 or n0==8:
  164.                 stl+="settant" + st1(n0)
  165.             else:
  166.                 stl+=t+ st1(n0)
  167.         if(n3 >= 80 and n3 < 90 ):
  168.             n0=int(str(n3)[1:])
  169.             t="ottanta"
  170.             if(n3==80):
  171.                 stl+=t
  172.             elif n0 == 1 or n0==8:
  173.                 stl+="ottant" + st1(n0)
  174.             else:
  175.                 stl+=t+ st1(n0)
  176.         if(n3 >= 90 and n3 < 100 ):
  177.             n0=int(str(n3)[1:])
  178.             t="novanta"
  179.             if(n3==90):
  180.                 stl+=t
  181.             elif n0 == 1 or n0==8:
  182.                 stl+="novant" + st1(n0)
  183.             else:
  184.                 stl+=t+ st1(n0)
  185.     return stl
  186.  
  187. def st4(n4):
  188.     stl=""
  189.     s1=""
  190.     s2=""
  191.     c="cento"
  192.     if(n4<100):
  193.         stl+=st3(n4)
  194.     else:
  195.         if(n4 <=199):
  196.             s2=int(str(n4)[1:3])
  197.             stl+=c + st3(s2)
  198.         else:
  199.             s1=int(str(n4)[:1])
  200.    
  201.             s2=int(str(n4)[1:3])
  202.            
  203.             s3=int(str(s2)[:2])
  204.             if(s3>=80 and s3<90):
  205.                
  206.                 stl=st1(s1) + "cent" + st3(s2)
  207.             else:
  208.                  stl=st1(s1) + c + st3(s2)
  209.  
  210.     return stl
  211.  
  212. def st5(n5):
  213.     stl=""
  214.     s1=int(str(n5)[:1])
  215.     s2=int(str(n5)[1:4])
  216.     if(n5<1000):
  217.         stl+=st4(n5)
  218.     else:
  219.         if(s1==1):
  220.             stl+="mille" + st4(s2)
  221.         else:
  222.             stl+= st1(s1) + "mila" + st4(s2)
  223.     return stl
  224.  
  225. def st6(n6):
  226.     stl=""
  227.     s1=int(str(n6)[:2])
  228.     s2=int(str(n6)[2:5])
  229.     if(n6<10000):
  230.         stl+=st5(n6)
  231.     else:  
  232.         stl+= st3(s1) + "mila" + st4(s2)
  233.     return stl
  234.  
  235. def st7(n7):
  236.     stl=""
  237.     s1=int(str(n7)[:2])
  238.     s2=int(str(n7)[2:6])
  239.     if(n7<100000):
  240.         stl+=st6(n7)
  241.     else:
  242.         stl+= st4(s1) + "mila" + st4(s2)
  243.     return stl
  244.  
  245. def st8(n8):
  246.     stl=""
  247.     s1=int(str(n8)[:1])
  248.     s2=int(str(n8)[1:7])
  249.     if(s1==1):
  250.         stl+="un milione" + st7(s2)
  251.     else:
  252.         stl+= st1(s1) + "milioni" + st7(s2)
  253.     return stl
  254.  
  255. def st9(n9):
  256.     stl=""
  257.     s1=int(str(n9)[:2])
  258.     s2=int(str(n9)[2:8])
  259.     stl+= st3(s1) + "milioni" + st7(s2)
  260.     return stl
  261.  
  262. def st10(n10):
  263.     stl=""
  264.     s1=int(str(n10)[:3])
  265.     s2=int(str(n10)[3:9])
  266.  
  267.     stl+= st4(s1) + "milioni" + st7(s2)
  268.     return stl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement