#!/usr/bin/python3 -B ################ ## ## ## Exercice 3 ## ## ## ################ contacts = {'Chloé': '0601020304' , 'Quentin': '0710203040' , 'Lyes' : '0623344556'} ## Question 3.1 contacts['Chloé'] = '0611223344' ## Question 3.2 contacts['Sarah'] = '0145444342' ## Question 3.3 print(contacts['Lyes']) ## Question 3.4 contacts.pop('Chloé') ## Question 3.5 for nom in contacts: numéro = contacts[nom] print(f"{clé} : {numéro}") ##ENDPROF ################ ## ## ## Exercice 4 ## ## ## ################ ## Question 4.1 def inverse_liste_contacts(contacts) : res = dict() for nom in contacts : tél = contacts[nom] # ici nom est la clé et tel la valeur res[tél] = nom # ici tél est la clé et nom la valeur return res ## Question 4.2 def affiche_liste_appels(L , contacts) : contacts_inv = inverse_liste_contacts2(contacts) for e in L : (heure,tel)=e if tel in contacts_inv : nom = contacts_inv[tel] print(heure, nom) else : print(heure ,tel) ################ ## ## ## Exercice 5 ## ## ## ################ ## Question 5.1 def sauvegarde(contacts, chemin): fichier = open(chemin, 'w', encoding="utf-8") for nom in contacts: numéro = contacts[nom] fichier.write(f"{nom},{numéro}\n") fichier.close() ## Question 5.2 def découper(chaîne): liste = chaîne.split(",") assert len(liste) == 2 numéro = liste[1] if numéro[-1]=="\n": numéro = numéro[:-1] # jusqu’au dernier caractère, exclu return [ liste[0], numéro ] ## Question 5.3 def importer(chemin): fichier = open(chemin, 'r', encoding="utf-8") dico = dict() for ligne in fichier: [nom, numéro] = découper(ligne) dico[nom] = numéro fichier.close() return dico ################ ## ## ## Exercice 6 ## ## ## ################ ## Question 6.1 contacts2 = {'Maison Chloé': '0901020304', 'Maison Lyes': '0901020304','Alex': '0412345678'} inverse_liste_contacts(contacts2) # On a perdu Chloé ! ## Question 6.2 def inverse_liste_contacts2(contacts) : res = dict() for nom in contacts : tél = contacts[nom] if res.get(tél,None)==None: res[tél] = [nom] else: res[tél].append(nom) return res ################ ## ## ## Exercice 7 ## ## ## ################ ## Question 7.1 def nim_gagnant(n): if n<=0: return True for i in [1,2,3]: if not nim_gagnant(n-i): return True return False ## Question 7.2 mem=dict() def nim_memo(n): if n<=0: return True if n in mem: return mem[n] for i in [1,2,3]: if not nim_memo(n-i): mem[n]=True return True mem[n]=False return False ## Question 7.3 d1 = chronomètre(nim_gagnant,33) d2 = chronomètre(nim_memo,33) d1/d2 # La différence est impressionnante (un facteur d’environ 200 000)