Introduction ;

.

Exercice N°1 :

Enoncé Exemple :

Écrire une fonction moyenne prenant en paramètres une liste d’entiers et qui renvoie la
moyenne des valeurs de cette liste.

 

>>> moyenne([10,20,30,40,60,110])
45.0

REPONSE

def moyenne(liste):

    somme=0

    long=len(liste)

    for i in range(long):

        somme=liste[i]+somme

    return somme/long

.

Exercice N°2 :

Chercher :

On travaille sur des dessins en noir et blanc obtenu à partir de pixels noirs et blancs :

La figure « cœur » ci-contre va servir d’exemple. On la représente par une grille de nombres, c’est-à-dire par une liste composée de sous-listes de mêmes longueurs. Chaque sous-liste représentera donc une ligne du dessin.

 

Dans le code ci-dessous, la fonction affiche permet d’afficher le dessin. Les pixels noirs
(1 dans la grille) seront représentés par le caractère "*" et les blancs (0 dans la grille) par
deux espaces.
La fonction zoomListe prend en argument une liste liste_depart et un entier k. Elle
renvoie une liste où chaque élément de liste_depart est dupliqué k fois.
La fonction zoomDessin prend en argument la grille dessin et renvoie une grille où
toutes les lignes de dessin sont zoomées k fois et répétées k fois.
Soit le code ci-dessous :

image/27.PNG

coeur = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \

         [0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0], \

         [0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0], \

         [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0], \

         [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], \

         [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], \

         [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], \

         [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0], \

         [0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0], \

         [0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0], \

         [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], \

         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

 

def affiche(dessin):

    ''' affichage d'une grille : les 1 sont représentés par

        des "*" , les 0 par deux espaces "  " '''

    for ligne in dessin:

        for col in ligne:

            if col == 1:

                print(" *",end="")

            else:

                print("  ",end="")

        print()

 

 

def zoomListe(liste_depart,k):

    '''renvoie une liste contenant k fois chaque

       élément de liste_depart'''

    liste_zoom = ...

    for elt in ... :

        for i in range(k):

            ...

    return liste_zoom

 

def zoomDessin(grille,k):

    '''renvoie une grille où les lignes sont zoomées k fois

       ET répétées k fois'''

    grille_zoom=[]

    for elt in grille:

        liste_zoom = ...

        for i in range(k):

            ... .append(...)

    return grille_zoom

.

REPONSE

.

Programme 

coeur = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \

         [0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0], \

         [0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0], \

         [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0], \

         [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], \

         [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], \

         [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], \

         [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0], \

         [0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0], \

         [0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0], \

         [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], \

         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

 

def affiche(dessin):

    ''' affichage d'une grille : les 1 sont représentés par

        des "*" , les 0 par deux espaces "  " '''

    a=chr(10687)

    a2=chr(11088)

    a3=chr(9617)

    a4=chr(9728)

    a5=chr(10033)

    a6=chr(9926)

    numero=[10687,11088,9617,9728,10033,9926]

    choix=[a,a2,a3,a4,a5,a6]

    for k in range(len(choix)):

        print('CARACTERE  n° : ',numero[k], '  est :  ',choix[k])

        for ligne in dessin:

            for col in ligne:

                if col == 1:

                    print(" "+choix[k],end="")

                else:

                    print("  ",end="")

            print()

def zoomListe(liste_depart,k):

    '''renvoie une liste contenant k fois chaque

       élément de liste_depart'''

    liste_zoom =[]

    for elt in liste_depart:

        for i in range(k):

            liste_zoom.append(elt)   

    return liste_zoom

def zoomDessin(grille,k):

    '''renvoie une grille où les lignes sont zoomées k fois

       ET répétées k fois'''

    grille_zoom=[]

    for elt in grille:     

        liste_zoom = zoomListe(elt,k)

        for i in range(k):        

            grille_zoom.append(liste_zoom)

    return grille_zoom

print(affiche(coeur))

print(affiche(zoomDessin(coeur,3)))

RESULTATS affiche(coeur)

CARACTERE  n° :  10687   est :   ⦿

                         

       ⦿ ⦿       ⦿ ⦿     

     ⦿     ⦿   ⦿     ⦿   

   ⦿         ⦿         ⦿ 

   ⦿                   ⦿ 

   ⦿                   ⦿ 

     ⦿               ⦿   

       ⦿           ⦿     

         ⦿       ⦿       

           ⦿   ⦿         

             ⦿           

                         

CARACTERE  n° :  11088   est :  

                         

                   

                     

                      

                       

                       

                       

                       

                       

                       

                        

                          

CARACTERE  n° :  9617   est :  

                         

                   

                     

                      

                       

                       

                       

                       

                       

                       

                        

                         

CARACTERE  n° :  9728   est :  

                         

                   

                     

                      

                       

                       

                       

                       

                       

                       

                        

                         

CARACTERE  n° :  10033   est :  

                         

                   

                     

                      

                       

                       

                       

                       

                       

                       

                        

                         

CARACTERE  n° :  9926   est :  

                         

                   

                     

                      

                       

                       

                       

                       

                       

                       

                        

                         

None

RESULTATS affiche(zoomDessin(coeur,3))

CARACTERE  n° :  10687   est :   ⦿

                                                                             

                                                                             

                                                                              

                   ⦿ ⦿ ⦿ ⦿ ⦿ ⦿                   ⦿ ⦿ ⦿ ⦿ ⦿ ⦿                 

                   ⦿ ⦿ ⦿ ⦿ ⦿ ⦿                   ⦿ ⦿ ⦿ ⦿ ⦿ ⦿                 

                   ⦿ ⦿ ⦿ ⦿ ⦿ ⦿                   ⦿ ⦿ ⦿ ⦿ ⦿ ⦿                 

             ⦿ ⦿ ⦿             ⦿ ⦿ ⦿       ⦿ ⦿ ⦿             ⦿ ⦿ ⦿           

             ⦿ ⦿ ⦿             ⦿ ⦿ ⦿       ⦿ ⦿ ⦿             ⦿ ⦿ ⦿           

             ⦿ ⦿ ⦿             ⦿ ⦿ ⦿       ⦿ ⦿ ⦿             ⦿ ⦿ ⦿           

       ⦿ ⦿ ⦿                         ⦿ ⦿ ⦿                         ⦿ ⦿ ⦿     

       ⦿ ⦿ ⦿                         ⦿ ⦿ ⦿                         ⦿ ⦿ ⦿     

       ⦿ ⦿ ⦿                         ⦿ ⦿ ⦿                         ⦿ ⦿ ⦿     

       ⦿ ⦿ ⦿                                                       ⦿ ⦿ ⦿     

       ⦿ ⦿ ⦿                                                       ⦿ ⦿ ⦿     

       ⦿ ⦿ ⦿                                                       ⦿ ⦿ ⦿     

       ⦿ ⦿ ⦿                                                       ⦿ ⦿ ⦿     

       ⦿ ⦿ ⦿                                                       ⦿ ⦿ ⦿     

       ⦿ ⦿ ⦿                                                       ⦿ ⦿ ⦿     

             ⦿ ⦿ ⦿                                           ⦿ ⦿ ⦿           

             ⦿ ⦿ ⦿                                           ⦿ ⦿ ⦿           

             ⦿ ⦿ ⦿                                           ⦿ ⦿ ⦿           

                   ⦿ ⦿ ⦿                               ⦿ ⦿ ⦿                 

                   ⦿ ⦿ ⦿                               ⦿ ⦿ ⦿                 

                   ⦿ ⦿ ⦿                               ⦿ ⦿ ⦿                 

                         ⦿ ⦿ ⦿                   ⦿ ⦿ ⦿                       

                         ⦿ ⦿ ⦿                   ⦿ ⦿ ⦿                       

                         ⦿ ⦿ ⦿                   ⦿ ⦿ ⦿                       

                               ⦿ ⦿ ⦿       ⦿ ⦿ ⦿                             

                               ⦿ ⦿ ⦿       ⦿ ⦿ ⦿                             

                               ⦿ ⦿ ⦿       ⦿ ⦿ ⦿                             

                                     ⦿ ⦿ ⦿                                    

                                     ⦿ ⦿ ⦿                                   

                                     ⦿ ⦿ ⦿                                   

CARACTERE  n° :  11088   est :  

                                                                              

                                                                             

                                                                             

                                                       

                                                       

                                                       

                                                         

                                                         

                                                         

                                                              

                                                              

                                                              

                                                                   

                                                                   

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                   

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                   

                                                                    

                                                                        

                                                                        

                                                                         

                                                                             

                                                                             

                                                                              

CARACTERE  n° :  9617   est :  

                                                                             

                                                                             

                                                                              

                                                       

                                                       

                                                       

                                                         

                                                         

                                                         

                                                              

                                                              

                                                              

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                         

                                                                        

                                                                        

                                                                             

                                                                             

                                                                             

CARACTERE  n° :  9728   est :  

                                                                              

                                                                             

                                                                             

                                                       

                                                        

                                                       

                                                         

                                                          

                                                         

                                                              

                                                              

                                                              

                                                                   

                                                                    

                                                                   

                                                                   

                                                                   

                                                                   

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                   

                                                                   

                                                                   

                                                                    

                                                                        

                                                                        

                                                                        

                                                                             

                                                                             

                                                                              

CARACTERE  n° :  10033   est :  

                                                                             

                                                                             

                                                                              

                                                       

                                                       

                                                       

                                                         

                                                         

                                                         

                                                              

                                                              

                                                              

                                                                    

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                   

                                                                   

                                                                   

                                                                   

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                    

                                                                   

                                                                   

                                                                         

                                                                        

                                                                        

                                                                             

                                                                              

                                                                             

CARACTERE  n° :  9926   est :