#! /usr/bin/env python3 # TD7 p.86 # 2.7 iteration avec for # 2.7.2 bases ############################################################### def q_2_7_2(): print("1. croissant") for i in range(10): # range(10) renvoie [0, 1, 2, ..., 9] print(i+1) # d'ou le +1 # autre solution #for i in range(1, 11): # print(i) print("2. décroissant") for i in range(10, 0, -1): print(i) print("7. multiples") for i in range(50): if (i%3 == 0) or (i%5 == 0): print(i) #q_2_7_2() # 2.7.3 sommes ############################################################## def q_2_7_3(): def f1(n): s = 0 for i in range(1, n+1): s += i return s def f2(n): s = 0 for i in range(1, n+1): if i%2 ==1 : s += i return s def f3(n): s = 0 for i in range(1, n+1): if i%2 ==1 : s += i*i return s n = int(input("entrez un entier : ")) print(n, f1(n), f2(n), f3(n)) #q_2_7_3() # 2.7.4 suites ############################################################## def q_2_7_4(): def growing(l): if l == []: return True u = l[0] for v in l: if v < u: return False u = v return True def decreasing(l): if l == []: return True u = l[0] for v in l: if v > u: return False u = v return True l = [3, 5, 8] print(growing(l), decreasing(l)) l = [3, 5, 2] print(growing(l), decreasing(l)) #q_2_7_4() # 2.7.5 comptons ############################################################ def q_2_7_5(): n = int(input("Combien de moutons :")) if n > 0: print("1 mouton") for i in range(1, n): print(i+1, "moutons") #q_2_7_5() # 2.7.6 règle ############################################################### def q_2_7_6(): def ruler(n, k): res = "" for i in range(n): if i%k == 0: res += '|' else: res += '-' return res n = int(input("longueur : ")) k = int(input("intervalle : ")) print(ruler(n, k)) #q_2_7_6() # 2.7.7 accumulateurs ####################################################### def q_2_7_7(): def fact(n): f = 1 for i in range(1, n+1): f *= i return f print(fact(5)) def pow(n, a): p = 1 for _ in range(a): p *= n for _ in range(-a): p /= n return p print(pow(2, 4), pow(2, -4)) #q_2_7_7() # 2.7.8 statistiques ####################################################### def q_2_7_8(): import random def nb_pile(t): pile = 0 for _ in range(t): if random.choice(['pile', 'face']) == 'pile': pile += 1 return pile def liste_nbpile(n, t): l = [] for _ in range(n): l.append(nb_pile(t)) return l T = 20 liste_pile = liste_nbpile(120, T) # 12 fois T tirages def compte_parties(liste_pile, x): c = 0 for t in liste_pile: if t == x: c += 1 return c def liste_cpt_parties(liste_pile): # version qui parcourt T+1 fois la liste comptes = [] for x in range(T+1): comptes.append(compte_parties(liste_pile, x)) return comptes comptes = liste_cpt_parties(liste_pile) def liste_cpt_parties(liste_pile): # version qui parcourt une fois la liste comptes = [] for x in range(T+1): comptes.append(0) for n in liste_pile: comptes[n] += 1 return comptes print(comptes == liste_cpt_parties(liste_pile)) for i in range(T+1): print(format(i, "2"), '*'*comptes[i]) #q_2_7_8() # 2.7.9/10 chaînes ########################################################## def q_2_7_9(): def strip(s): result = "" for c in s: if c == ' ': continue result += c return result print(strip("bonjour tout le monde")) def inverse(s): result = "" for c in s: result = c + result return result print(inverse("inf101")) def palindrome(s): return s == inverse(s) print(palindrome("kayak"), palindrome("toto")) def pal_level(s): if palindrome(s): # vrai palindrome return 2 if palindrome(strip(s)): # pseudo-palindrome return 1 return 0 # pas un palindrome print(pal_level("rats live on no evil star"), pal_level("eve reve"), pal_level("toto")) #q_2_7_9() # 2.7.11 zip ################################################################ def q_2_7_11(): def zip_chaine(a, b): result = [] for c_a in a: for c_b in b: result.append(c_a+c_b) return result def zip_liste(a, b): result = [] for c_a in a: for c_b in b: result.append((c_a, c_b)) # (c_a, c_b) est une paire constituée de 2 valeurs return result print(zip_chaine("ab", "cd")) print(zip_liste([1, 2], [3, 4])) q_2_7_11()