# TODO # Exercice 1 def mystere(s : str) -> str : char= s[0] resultat = char for i in range(1, len(s)): if s[i] != char: char = s[i] resultat = resultat + char return resultat # char |"l"| | |"a"| | | | |"l"| | |"a"| | | | # resultat| - |"l"| | |"la"| | | | |"lal"| | |"lala"| | | # i | - |- | 1 | | |2|3|4| | |5| | |6|7| print(mystere("laaalaaa")) #lala print(mystere("ouuups")) #oups #print(mystere("")) l'erreur vient de s[0] dans une chaine vide : string index out of range # Ajouter # if s == "": # return "" # au tout début de la fonction #retourne une nouvelle chaine obtenue à partir de la chaine s (supposée non vide) #en paramètre en enlevant les caractères doublons adjacents # O(n) où n est le nombre de caractères de la chaine s # Exercice 2 # 1/ x = 1 y = 2 p = x >= 1 and y < 10 q = p and x == y r = not(p or q) print("p = ",p," q = ", q, " r = ",r) #p = True q = False r = False # 2/ # [7, 8, 12] for x in range(0,21): resultat = x >= 5 and x <= 12 and (x % 4 == 0 or x == 7) print("x = ", x, " resultat = ", resultat) # 3/ def bissextile(annee: int) -> bool: if annee % 4 == 0 and not(annee % 100 == 0 and annee % 400 != 0): return True return False # ou def bissextileV2(annee: int) -> bool: if annee % 100 == 0 and annee % 400 != 0: return False return annee % 4 == 0 listeAnnees=[2000,2004,2024,2400,2001,2023,2100,2200,2204] for an in listeAnnees: print("bissextile(", an, ") =",bissextile(an), bissextileV2(an)) # Exercice 3 def listeVersEntier(L: list) -> int: nb = 0 for chiffre in L: nb = nb * 10 nb = nb + chiffre return nb def listeVersEntierV2(L: list) -> int: nb = 0 puissance = 1 for i in range(len(L)-1, -1, -1): nb = nb + L[i] * puissance puissance = puissance * 10 return nb LNB1 = [0,1,0,2,4] print(listeVersEntier(LNB1)) print(listeVersEntierV2(LNB1)) # Exercice 4 # l'index est incorrect, il commence effectivement à "fin" qui est exactement après la fin de la liste L1: # for i in range(fin-1, -1, -1) # L n'a jamais été défini en effet, ajouter au début: # L = [0] * len(L1) # On ne peut effectivement pas ajouter un entier et une liste: # L[i] = L1[i] + L2[i] # Il y a une apostrophe qui traîne sur la ligne, construisant ainsi une chaîne # sommeList([1, 3, 5], [2, -3, -8]) # Exercice 5 #1. def premierMot(s: str) -> str: s2 = "" for i in range(len(s)): if s[i] != " ": s2 = s2 + s[i] else: return s2 return s2 def premierMotV2(s: str) -> str: s2 = "" for c in s: if c != " ": s2 = s2 + c else: return s2 return s2 #2. O(n) où n est le nombre de caractères de la chaine s #3. Il suffit de partir de la fin de la liste par exemple, en modifiant le range : #range(len(s)-1,-1,-1) ou range(-1,-len(s)-1,-1) ou en changeant l'index i # et de concaténer en début s2 = s[i] + s2 def dernierMot(s: str) -> str: s2 = "" for i in range(len(s)-1,-1,-1): if s[i] != " ": s2 = s[i] + s2 else: return s2 return s2 def dernierMotV2(s: str) -> str: s2 = "" for c in s: if c != " ": s2 = s2 + c else: s2 = "" return s2 for phrase in [ "Hello world", "Hello", " Hello" ]: print(premierMot(phrase)) print(premierMotV2(phrase)) print(dernierMot(phrase)) print(dernierMotV2(phrase)) # Exercice 6 from math import sqrt def distanceEucliOrigin(x:int, y:int) -> int: return int(sqrt(x*x + y*y)) from bibimages import * def DegradeEuclidien(img:image): l = largeurImage(img) h = hauteurImage(img) for x in range(l): for y in range(h): l = distanceEucliOrigin(x, y) colorierPixel(img, x, y, (l, l, l)) i = nouvelleImage(300,200) DegradeEuclidien(i) #afficherImage(i) ecrireImage(i, "degrade.png")