# 2.6.4 – 2.6.6 def in_list(l, u): for e in l: if e == u: return True # on peut s'arrêter dès qu'on a trouvé return False # si on sort de la boucle, c'est que u n'est pas dans la liste def index(l, u): for i in range(len(l)): if l[i] == u: return i return -1 # consigne de l'énoncé def count(l, u): c = 0 for e in l: if e == u: c += 1 return c """ l = [4, 7, 2, 7] print(in_list(l, 3), index(l, 3), count(l, 3)) print(in_list(l, 7), index(l, 7), count(l, 7)) """ # 2.6.7 – 2.6.8 def minimum(l): m = float("inf") # minimum du début de la liste for e in l: if e < m: m = e return m def maximum(l): m = float("-inf") i_m = None for i in range(len(l)): e = l[i] if e > m: m = e i_m = i return i_m def somme(l): s = 0 for e in l: s += e return s def moyenne(l): s = somme(l) return s/len(l) """ l = [56, 3, 8, 4] print("minimum :", minimum(l)) print("indice du maximum (valeur) :", maximum(l), l[maximum(l)]) print("somme :", somme(l)) print("moyenne :", moyenne(l)) """ # 2.6.14 p.84 def compte_carac(l, c): n = 0 for e in l: if e == c: n += 1 return n def compte_alphab(l): counts = [] for o in range(ord('a'), ord('z')+1): c = chr(o) counts.append(compte_carac(l, c)) return counts def compte_alphab2(l): counts = [] for o in range(ord('a'), ord('z')+1): counts.append(0) for c in l: i = ord(c) - ord('a') counts[i] += 1 return counts t = "coucou" print(t, "contient", compte_carac(t, 'u'), "u") print(compte_alphab2(t)) # 2.6.15 voir correction TP6 3.6.3