#encoding:utf8 ### Exercice 1 (2 pts) s=0 for i in range(1, 3): # list(range(1,3)) = [1,2] for j in [2, 4, 3]: s = s + i * j print (s, i ,j) # i : | 1 | 1 | 1 | 2 | 2| 2 # j : | 2 | 4 | 3 | 2 | 4| 3 # s : 0 | 2 | 6 | 9 | 13|21|27 ### Exercice 2 (5 pts : 2 + 0,5 + 0,5 + 2) def mystere2(L): m = 0 for i in L: if i < m: return False else: m = i return True # 1) #i : | 0 | 2 | 3 | 7 | 10 #m : 0 | 0 | 2 | 3 | 7 | 10 # 2) print(mystere2([0, 2, 3, 7, 10])) # True # 3) print(mystere2([5,3,1,7,11])) # False # 4) # La fonction mystere2 retourne True si les éléments # de L sont triés dans l'ordre croissant et False sinon. ### Exercice 3 (5 pts : 1 + 0,5 + 2 + 2) # 1) def estDiviseur(i,n): return n%i == 0 # 2) # 2,3,5,7,11,13 # 3) def estPremier(n): for i in range(2,n): if estDiviseur(i,n): return False return n >= 2 # 4) def nbPremiers(n): nb = 0 for i in range (2,n): if estPremier(i): nb = nb + 1 return nb print(nbPremiers(15)) # valeur affichée : 6 ### Exercice 4 (3 pts : 2 + 1) from PIL.Image import * # 1) def masquerGauche(img, c): (l,h) = img.size for x in range (l//2): #difficulté : trouver le bon "range for y in range (h): Image.putpixel(img,(x,y),c) #test de la fonction masquerGauche monImage = open("ocean.png") masquerGauche(monImage,(128, 128, 128)) Image.show(monImage) # 2) def masquerBas(img, c): (l,h) = img.size for x in range(l): for y in range (h//2,h): #difficulté : trouver le bon "range Image.putpixel(img,(x,y),c) #test de la fonction masquerGauche monImage = open("ocean.png") masquerBas(monImage,(128, 128, 128)) Image.show(monImage) ### Exercice 5 (2 pts) def permutation(img): (l,h)= img.size for x in range(l): for y in range(h): (r,g,b) = Image.getpixel(img,(x,y)) Image.putpixel(img,(x,y),(g,b,r)) #permutation des composantes #test de la fonction permutation monImage = open("ocean.png") permutation(monImage) Image.show(monImage) ### Exercice 6 (2,5 pts) def ecrireDessus(img1, img2, c): (l,h) = img1.size for x in range(l): for y in range(h): c2 = Image.getpixel(img2, (x,y)) if c2 == c: Image.putpixel(img1, (x,y), c) #test de la fonction ecrireDessus monImage1 = open("ocean.png") monImage2 = open("texte.png") ecrireDessus(monImage1, monImage2, (0,0,0)) Image.show(monImage1)