Non-corso Python Esercizi di lettura

Da raspibo.

Chi ha difficolta' a scrivere codice Python puo' provare a fare questi esercizi di "lettura" di codice Python.

Gli esercizi proposti sono programmi funzionanti.

Come svolgimento dell'esercizio si deve:

  • determinare lo scopo del programma
  • spiegare il significato di ogni riga (non banale) del codice

Indice

1

def dd(s):
  d,m,y=s.split('.')
  d,m,y=int(d),int(m),int(y)
  md=[0,31,28,31,30,31,30,31,31,30,31,30,31]
  if y % 4 == 0: md[2]=29
  dy=d
  for i in range(1,m):
    dy += md[i]
  return dy

if __name__=="__main__":
  data=input("Dammi una data (es. 14.03.2014) ")
  print("il risultato della funzione misteriosa e' ",dd(data))

2

def imin(l):
  im,mn=0,l[0]
  for i in range(1,len(l)):
    if l[i]<mn: im,mn=i,l[i]
  return im

if __name__="__main__":
  l=input('dammi in input una lista di elementi (es: [1,2,3]): ')
  print("l'output della funzione misteriosa e': ",imin(l))

3

def r13(s):
        e=''
        for c in s:
                if 'A'<=c<='Z':
                        e+=chr(((ord(c)-ord('A'))+13)%26+ord('A'))
                elif 'a'<=c<='z':
                        e+=chr(((ord(c)-ord('a'))+13)%26+ord('a'))
                else:
                        e+=c
        return e

if __name__=="__main__":
        s=input("scrivi una semplice frase (es: 'Ciao Mondo') ")
        r=r13(s)
        print("il risultato della funzione misteriosa e': ",r)
        print("consiglio: prova a eseguire nuovamente il programma scrivendo")
        print("la frase: ",r)

4

def pf(n):
        f=[]
        for i in range(2,n+1):
                while n % i == 0:
                        f.append(i)
                        n //= i
                if n == 1:
                        break
        return f

if __name__=="__main__":
        n=int(input("dammi un numero (consigliato da 2 a 1000): "))
        print("il risultato della funzione misteriosa e' ",pf(n))

5

(richiede il modulo turtle)

import turtle
def cy(r):
        turtle.setup(400,400)
        turtle.penup()
        turtle.hideturtle()
        turtle.speed(0)
        for i in range(1000):
                x,y=random.randint(-200,200),random.randint(-200,200)
                if (x*x+y*y)**0.5 < r:
                        color="red"
                else:
                        color="blue"
                turtle.goto(x,y)
                turtle.dot(5,color)

if __name__=="__main__":
        r=float(input("scrivi un numero (consigliato da 100 a 200): "))
        print("guarda il risultato della funzione misteriosa")
        cy(r)
        turtle.exitonclick()

6

(richiede il modulo turtle)

import turtle

def poly(n):
        for i in range(n):
                turtle.forward(50)
                turtle.left(360/n)

if __name__=="__main__":
        n=int(input("dammi un intero (consigliato da 3 a 12): "))
        poly(n)
        turtle.exitonclick()

7

Questo e' contorto. Se vi piace... iniziate a preoccuparvi: state diventando informatici ;-)

f="f={0}{1}{0};print(f.format(chr(34),f))";print(f.format(chr(34),f))

NB: non e' necessario capire questo programma, e' solo per chi si vuole cimentare in una sfida...

8

def pf(n):
        f=[]
        for i in range(2,n+1):
                while n % i == 0:
                        f.append(i)
                        n //= i
                if n == 1:
                        break
        return f

def fgcd(a,b):
        def rgcd(fa,fb):
                if fa and fb:
                        if fa[0]==fb[0]:
                                return fa[:1]+rgcd(fa[1:],fb[1:])
                        elif fa[0]<fb[0]:
                                return rgcd(fa[1:],fb)
                        else:
                                return rgcd(fa,fb[1:])
                else:
                        return []
        return rgcd(pf(a),pf(b))

def flcm(a,b):
        def rlcm(fa,fb):
                if fa and fb:
                        if fa[0]==fb[0]:
                                return fa[:1]+rlcm(fa[1:],fb[1:])
                        elif fa[0]<fb[0]:
                                return fa[:1]+rlcm(fa[1:],fb)
                        else:
                                return fa[:1]+rlcm(fa,fb[1:])
                else:
                        return fa if fa else fb
        return rlcm(pf(a),pf(b))

def mul(l):
        rv=1
        for el in l:
                rv *= el
        return rv

def gcd(x,y):
        return mul(fgcd(x,y))

def lcm(x,y):
        return mul(flcm(x,y))

if __name__=="__main__":
        a,b=input("Inserisci due numeri (es: 48 36): ").split()
        a,b=int(a),int(b)
        print("pf({})={}".format(a,pf(a)))
        print("pf({})={}".format(b,pf(b)))
        print("fgcd({},{})={}".format(a,b,fgcd(a,b)))
        print("flcm({},{})={}".format(a,b,flcm(a,b)))
        print("gcd({},{})={}".format(a,b,gcd(a,b)))
        print("lcm({},{})={}".format(a,b,lcm(a,b)))

9

def div(n):
        return {x for x in range(1,n+1) if n % x == 0}

#alternativa:
#def div(n):
#       d=set()
#       for x in range(1,n+1):
#               if n % x == 0: d.add(x)
#       return d

def gcd(a,b):
        return max(div(a) & div(b))

def mul(n,limit):
        return {x*n for x in range(1,limit//n + 1)}

#alternativa
#def mul(n,limit):
#       d=set()
#       for x in range(1,limit//n + 1):
#               d.add(x*n)
#       return d

def lcm(a,b):
        return min(mul(a,a*b) & mul(b,a*b))

if __name__=="__main__":
        a,b=input("Inserisci due numeri (es: 48 36): ").split()
        a,b=int(a),int(b)
        print("div({})={}".format(a,div(a)))
        print("div({})={}".format(b,div(b)))
        print("mul({},{})={}".format(a,a*b,mul(a,a*b)))
        print("mul({},{})={}".format(b,a*b,mul(b,a*b)))
        print("gcd({},{})={}".format(a,b,gcd(a,b)))
        print("lcm({},{})={}".format(a,b,lcm(a,b)))

10

def sdiv(n):
        return {x for x in range(2,n) if n % x == 0}

#alternativa:
#def sdiv(n):
#       d=set()
#       for x in range(2,n):
#               if n % x == 0: d.add(x)
#       return d

def pr(n):
        return not sdiv(n)

def pdiv(n):
        return {x for x in sdiv(n) if pr(x)}

#alternativa:
#def pdiv(n):
#       d=set()
#       for x in sdiv(n):
#               if pr(x): d.add(x)
#       return d

def gcd(a,b):
        csdiv=pdiv(a) & pdiv(b)
        r=1
        for x in csdiv:
                while a % (r*x) == 0 and b % (r*x) == 0:
                        r *= x
        return r

def lcm(a,b):
        return a * b // gcd(a,b)

if __name__=="__main__":
        a,b=input("Inserisci due numeri (es: 48 36): ").split()
        a,b=int(a),int(b)
        print("pdiv({})={}".format(a,pdiv(a)))
        print("pdiv({})={}".format(b,pdiv(b)))
        print("gcd({},{})={}".format(a,b,gcd(a,b)))
        print("lcm({},{})={}".format(a,b,lcm(a,b)))

11

def tartline(l):
        l[:0]=[1]
        for i in range(1,len(l)-1):
                l[i] += l[i+1]

l=[]
for i in range(12):
        tartline(l)
        print("{:^60}".format(l))

12

def sdiv(n):
        return {x for x in range(2,n) if n % x == 0}

#alternativa:
#def sdiv(n):
#       d=set()
#       for x in range(2,n):
#               if n % x == 0: d.add(x)
#       return d

def pr(n):
        return not sdiv(n)

N=int(input("dammi il numero massimo (es 100): "))
print("ecco i numeri ***** fino a",N,":",[n for n in range(1,N+1) if pr(n)])


13

vf=[("Nella vecchia fattoria","Quante bestie ha zio Tobia","C'e' la capra","capra","ca"),
        ("Attaccato a un carrettino","C'è un quadrupede piccino","L'asinel","nel","nè"),
        ("Tra le casse e i ferri rotti", "Dove i topi son grassotti","C'è un bel gatto","gatto","ga"),
        ("Tanto grasso e tanto grosso", "Sempre sporco a più non posso", "C'è il maiale","iale","ia"),
        ("Poi sull'argine del fosso", "Alle prese con un osso", "C'è un bel cane","cane","ca"),
        ("Nella stalla silenziosa", "Dopo aver mangiato a iosa", "Dorme il bue","bue","bu")]
refrain="ia ia o"

def recverse(m,n):
        v1,v2,an1,an2,an3=vf[m]
        if n==m:
                print(v1,refrain)
                print(v2,refrain)
        else:
                recverse(m+1,n)
        print(an1,an2,an3,an3,an2)

for i in range(len(vf)):
        recverse(0,i)
        print(vf[0][0],refrain)
        print()

14

import turtle

def hser(size, level):
        if level==0:
                turtle.forward(size)
        else:
                hser(size,level-1)
                turtle.left(45)
                turtle.forward(size * 2**0.5)
                turtle.left(45)
                hser(size,level-1)
                turtle.right(90)
                turtle.forward(size)
                turtle.right(90)
                hser(size,level-1)
                turtle.left(45)
                turtle.forward(size * 2**0.5)
                turtle.left(45)
                hser(size,level-1)

def ser(size, level):
        turtle.penup()
        pos=(2**(level+2)-3) * size // 2 #posizione iniziale
        turtle.setpos(-pos,pos)
        turtle.pendown()
        hser(size,level)
        turtle.right(90)
        hser(size,level)
        turtle.right(90)
        hser(size,level)
        turtle.right(90)
        hser(size,level)
        turtle.right(90)

turtle.speed(0)
turtle.hideturtle()
ser(2,1)
ser(2,2)
ser(2,3)
ser(2,4)
turtle.exitonclick()

15

import turtle
import colorsys
import sys

def arrowstep(level, length, angle):
        if level:
                arrowstep(level-1, length/2, -angle)
                turtle.left(angle)
                arrowstep(level-1, length/2, angle)
                turtle.left(angle)
                arrowstep(level-1, length/2, -angle)
        else:
                turtle.forward(length)

def arrow(level, length):
        if level & 1:
                turtle.left(60)
                arrowstep(level, length, -60)
        else:
                arrowstep(level, length, -60)

turtle.speed(0)
turtle.hideturtle()
turtle.colormode(1)
nmax=6
width=256
xmin= -width//2
ymin= -width*(3**0.5)//4
for n in range(nmax):
        turtle.penup()
        turtle.setposition(xmin,ymin)
        turtle.setheading(0)
        turtle.pencolor(colorsys.hls_to_rgb(1.0*n/6,0.5,1))
        turtle.pendown()
        arrow(n,256)

turtle.exitonclick()

16

def av(l):
        return sum(l)/len(l)

def rg(l):
        return max(l)-min(l)

def md(l):
        lx=len(l)
        ls=sorted(l)
        if lx%2:
                return ls[lx//2]
        else:
                return (ls[lx//2-1]+ls[lx//2])/2

if __name__=="__main__":
        l=input("dammi una lista di numeri separati da virgole (es: 1,2,3,4.4): ")
        l=[float(x) for x in l.split(',')]
        print("i valori trovati dalle tre funzioni sono: {:.4f} {:.4f} {:.4f}".format(av(l),rg(l),md(l)))

17

import turtle
import sys

rows,cols=4,4
buttonsize=50
topdisplay=2*buttonsize

stack=[0]
fresh=True

def num(x):
        global fresh,stack
        if fresh:
                stack[:0]=[x]
                fresh=False
        else:
                stack[0]=stack[0]*10+x

enter,add,sub,mul,div=0,int.__add__,int.__sub__,int.__mul__,int.__floordiv__

def op(x):
        global fresh,stack
        if x!=enter and len(stack)>1:
                stack=[x(stack[1],stack[0])]+stack[2:]
        fresh=True

def off(x):
        sys.exit(0)

buttons={ 
        (0,0):('0',num,0), (1,0):('1',num,1), (1,1):('2',num,2), (1,2):('3',num,3), (2,0):('4',num,4), 
        (2,1):('5',num,5), (2,2):('6',num,6), (3,0):('7',num,7), (3,1):('8',num,8), (3,2):('9',num,9), 
        (3,3):('+',op,add), (2,3):('-',op,sub), (1,3):('*',op,mul), (0,3):('/',op,div),
        (0,2):('ent',op,enter), (0,1):('off',off,0)}

def click(x,y):
        col=x//buttonsize
        row=y//buttonsize
        if (row,col) in buttons:
                label,op,val=buttons[row,col]
                op(val)
                drawcalc()

def drawcalc():
        def drawbutton(col,row,label):
                turtle.setpos(row*buttonsize,col*buttonsize)
                turtle.pendown()
                for _ in range(4):
                        turtle.forward(buttonsize)
                        turtle.left(90)
                turtle.penup()
                turtle.setpos(row*buttonsize+buttonsize//2,col*buttonsize+buttonsize//2)
                turtle.write(label,align="center")
        turtle.clear()
        turtle.penup()
        for row,col in buttons:
                drawbutton(row,col,buttons[row,col][0])
        turtle.setpos(row*buttonsize*(rows-1),col*buttonsize*(cols+1))
        turtle.write(str(stack[0]),align="right")
        turtle.update()

turtle.screensize(cols*buttonsize+1, rows*buttonsize+topdisplay+1)
turtle.setup(cols*buttonsize+1, rows*buttonsize+topdisplay+1)
turtle.setworldcoordinates(0,0,cols*buttonsize, rows*buttonsize+topdisplay)
turtle.hideturtle()
turtle.speed(10)
turtle.tracer(0)
drawcalc()
turtle.onscreenclick(click)
turtle.listen()
turtle.mainloop()

18

import turtle
import colorsys
import time

N=6
frompin=list(range(N,0,-1))
auxpin=[]
topin=[]

def rectangle(size, level, pin):
        turtle.penup()
        turtle.setpos(2*N*10*pin,level*10)
        turtle.setheading(0)
        turtle.pendown()
        turtle.fillcolor(colorsys.hls_to_rgb(1.0*(size-1)/N,0.5,1))
        turtle.begin_fill()
        turtle.forward(size*10)
        turtle.left(90)
        turtle.forward(10)
        turtle.left(90)
        turtle.forward(size*20)
        turtle.left(90)
        turtle.forward(10)
        turtle.left(90)
        turtle.forward(size*10)
        turtle.end_fill()

def showhanoi():
        turtle.clear()
        lf,la,lt=map(len,(frompin,auxpin,topin))
        for i in range(lf):
                rectangle(frompin[i], i, -1)
        for i in range(la):
                rectangle(auxpin[i], i, 0)
        for i in range(lt):
                rectangle(topin[i], i, 1)
        turtle.update()
        time.sleep(0.2)

def hanoi(n,f,a,t):
        if n==1:
                t.append(f.pop())
                showhanoi()
        else:
                hanoi(n-1,f,t,a)
                hanoi(1,f,a,t)
                hanoi(n-1,a,f,t)

turtle.hideturtle()
turtle.speed(10)
turtle.tracer(0)
showhanoi()
hanoi(N,frompin,auxpin,topin)
turtle.exitonclick()
Strumenti personali
Namespace

Varianti
Azioni
Navigazione
Strumenti