### exercice 1 def mystere(L): s = 0 for i in L : s = 10 * s + i return s # Question 1 # s | 0 | 1 | 12 | 125 | 1253 # i | | 1 | 2 | 5 | 3 # Question 2 print(mystere([1,2,5,3])) # affiche 1253 # Question 3 print(mystere([9,6,8])) # affiche 968 # Question 4 # De manière générale, la fonction mystere(L) retourne le nombre dont la décomposition # en base 10 est codé par la liste L. ### Exercice 2 #question 1 # Remarque : il s'agit de l'exercice 1.4.3 du poly. def moyenneListe(L): s = 0 n = 0 for i in L: s = s + i n = n + 1 return s / n # On peut le faire également comme ceci : def moyenneListeV2(L): s = 0 for i in L: s = s + i return s / len(L) print(moyenneListe([12.0, 9.5, 10.0, 15.0, 10.5])) # affiche 11.4 # Question 2 def nbNotesAuDessus(L): m = moyenneListe(L) nb = 0 for i in L: if i >= m: nb = nb + 1 return nb print(nbNotesAuDessus([12.0, 9.5, 10.0, 15.0, 10.5])) # affiche 2 # Question 3 def existeNoteMoyenne(L): m = moyenneListe(L) for i in L: if i == m: return True return False print(existeNoteMoyenne([12.0, 9.5, 10.0, 15.0, 10.5])) # affiche False print(existeNoteMoyenne([12.0, 10.0, 8.0])) # affiche True # Question 4 def toutesNotesMoyenne(L): m = moyenneListe(L) for i in L: if i != m: return False return True print(toutesNotesMoyenne([12.0, 9.5, 10.0, 15.0, 10.5])) # affiche False print(toutesNotesMoyenne([12.0, 10.0, 8.0])) # affiche False print(toutesNotesMoyenne([10.0, 10.0, 10.0])) # affiche Trure # Question 5 def toutesDifferentesMoyenne(L): return not existeNoteMoyenne(L) #Remarque on peut aussi coder cette fonction dans le même esprit que les 2 dernières print(toutesDifferentesMoyenne([12.0, 9.5, 10.0, 15.0, 10.5])) # affiche True print(toutesDifferentesMoyenne([12.0, 10.0, 8.0])) # affiche False ### Exercice 3 # Question 1 # Nombre de semaines | 0 | 1 | 2 | 3 | 4 # Diamètre (en cm) | 5 | 10 | 20 | 40 | 80 # Question 2 def diametreNenuphar(d, n): for i in range(n): d = 2*d return d # Question 3 print(diametreNenuphar(8,12)) #affiche 32768 # Question 4 def tientDansLeBassin(d, n, db): return diametreNenuphar(d,n) < db print(tientDansLeBassin(5,2,30)) #affiche True print(tientDansLeBassin(5,3,30)) #affiche False ### Exercice 4 from PIL.Image import * monImage=new("RGB",(300,200)) # Question 1 def rectanglePlein(img, x1, x2, y1, y2, c): for x in range(x1, x2+1): for y in range(y1, y2+1): Image.putpixel(img,(x,y),c) def quadrants(img): l = img.width h = img.height rectanglePlein(img, 0,l-1, 0, h-1, (255,255,255)) rectanglePlein(img, 0, l//2-1, 0, h//2-1, (0,0,0)) rectanglePlein(img, l//2,l-1, h//2, h-1, (0,0,0)) quadrants(monImage) Image.show(monImage) def quadrantsV2(img): l = img.width h = img.height for x in range(0, l//2): for y in range(0, h//2): Image.putpixel(img,(x,y),(0,0,0)) for y in range(h//2, h): Image.putpixel(img,(x,y),(255,255,255)) for x in range(l//2, l): for y in range(0, h//2): Image.putpixel(img,(x,y),(255,255,255)) for y in range(h//2, h): Image.putpixel(img,(x,y),(0,0,0)) quadrantsV2(monImage) Image.show(monImage) def quadrantsV3(img): l = img.width h = img.height for x in range(l): for y in range(h): if (x < l//2 and y < h//2) or (x >= l//2 and y >= h//2) : Image.putpixel(img,(x,y),(0,0,0)) else: Image.putpixel(img,(x,y),(255,255,255)) quadrantsV3(monImage) Image.show(monImage) # Question 2 def damier(img, cl, ch): l = img.width h = img.height dl = l//cl dh = h//ch rectanglePlein(img, 0,l-1, 0, h-1, (255,255,255)) for x in range(cl): for y in range(ch): if (x+y)%2 == 0: rectanglePlein(img, x*dl, (x+1)*dl-1, y*dh, (y+1)*dh-1, (0,0,0)) damier(monImage, 6, 5) Image.show(monImage) ###Autre version : def damierV2(img, cl,ch): l = img.width h = img.height dl=l//cl dh=h//ch for x in range(l): for y in range(h): if((x//dl)+(y//dh))%2==0: Image.putpixel(img,(x,y),(0,0,0)) else: Image.putpixel(img,(x,y),(255,255,255)) damierV2(monImage, 6, 5) Image.show(monImage) ### Exercice 5 # Question 1 def entrelacer(img1, img2): l = img1.width h = img1.height for x in range(l): for y in range(h): if y%2 == 1: c = Image.getpixel(img1,(x,y)) Image.putpixel(img2,(x,y), c) def entrelacer2(img1, img2): l = img1.width h = img1.height for x in range(l): for y in range(1,h,2): c = Image.getpixel(img1,(x,y)) Image.putpixel(img2,(x,y), c) # Question 2 # img2 contiendra une image noire. #