from bibimages import * ################################################################## ### Exercice 1 ################################################################## def mystere(L): cpt = 0 cptMax = 0 for i in L : if i > 0: cpt = cpt + 1 else: if cpt > cptMax : cptMax = cpt cpt = 0 ### print(i, cpt, cptMax) if cpt > cptMax : cptMax = cpt return cptMax precipitations = [9.9, 0, 5.5, 7.8, 0,10.5, 0] ## 1) ## on affiche ici les valeurs des variables à la fin de chaque tour de boucle ## ## i | 9.9 | 0 | 5.5 | 7.8 | 0 | 10.5 | 0 ## cpt | 1 | 0 | 1 | 2 | 0 | 1 | 0 ## cptMax | 0 | 1 | 1 | 1 | 2 | 2 | 2 ## 2) print(mystere(precipitations)) #valeur retournée : 2 ## 3) print(mystere([0, 10.3, 0, 0, 7.9, 8.9, 6.3])) # valeur retournée :3 ## 4) # De manière générale, la fonction mystere retourne la longueur de la plus grande # sous-séquence de nombres strictement positifs dans la liste passée en paramètre. ## 5) def sansPluie(L): for i in L: if i > 0: return False return True ################################################################## ### Exercice 2 ################################################################## def sommeMultiplesDe3(L): somme = 0 for i in L: if i%3 ==0: somme = somme + i if i == -1: return somme return somme #Vérifions sur les exemples du sujet print(sommeMultiplesDe3([1,3,4,3,3,1,2,2,6]), "=", 15) print(sommeMultiplesDe3([1,3,4,27,18,-1,2,2,6]), "=", 48) ################################################################## ### Exercice 3 ################################################################## # 1.) def coinUnPixel(img): l = largeurImage(img) h = hauteurImage(img) for x in range(l): colorierPixel(img,x,0,(255,0,0)) for y in range(h): colorierPixel(img,l-1,y,(255,0,0)) #Testons cette fonction sur un exemple monImage = nouvelleImage(300,200) coinUnPixel(monImage) afficherImage(monImage) # 2.) def coin(img, p): l = largeurImage(img) h = hauteurImage(img) for x in range(l): for dy in range(p): colorierPixel(img,x,dy,(255,0,0)) for y in range(h): for dx in range(p): colorierPixel(img,l-1-dx,y,(255,0,0)) #Testons cette fonction sur un exemple monImage = nouvelleImage(300,200) coin(monImage,3) afficherImage(monImage) ################################################################## ### Exercice 4 ################################################################## def traitement1(img): H = hauteurImage(img) L = largeurImage(img) for x in range(L//2): for y in range(H): c = couleurPixel(img, x, y) colorierPixel(img, L//2 + x, y, c) def traitement2(img): H = hauteurImage(img) L = largeurImage(img) for y in range(H): for x in range(L//2): c1 = couleurPixel(img, x, y) c2 = couleurPixel(img, L//2 + x, y) colorierPixel(img, x, y, c2) colorierPixel(img, L//2 + x, y, c1) theiere = ouvrirImage("teapot.png") traitement1(theiere) afficherImage(theiere) #produit l'image D theiere = ouvrirImage("teapot.png") traitement2(theiere) afficherImage(theiere) #produit l'image B ################################################################## ### Exercice 5 ################################################################## def estLeNegatif(img1,img2): H = hauteurImage(img1) L = largeurImage(img1) for y in range(H): for x in range(L): (r_img1, g_img1, b_img1) = couleurPixel(img1, x, y) (r_img2, g_img2, b_img2) = couleurPixel(img2, x, y) # comparaison du tuple (r,g,b) autorisé : (r_img2, g_img2, b_img2) != ((255 - r_img1),(255 - g_img1),(255 - b_img1)) if ((255 - r_img1) != r_img2) or ((255 - g_img1) != g_img2) or ((255 - b_img1) != b_img2): return False return True m1 = ouvrirImage("MarilyneGris.png") m2 = ouvrirImage("MarilyneGrisNegatif.png") print(estLeNegatif(m1,m2)) print(estLeNegatif(m1,m1))