print("### Dictionnaires") print("### CORRECTION DE https://cgouygou.github.io/1NSI/T02_TypesConstruits/T2.3_Dictionnaires/T2.3_Dictionnaires/#234-exercices") def exo1(): print("\n\n### EXERCICE 1") dressing = {"pantalons": 3, "pulls": 4, "tee-shirts": 8} dressing["chemises"] = 5 print("#1. Avec 5 chemises :", dressing) def ajout(habit: str): if habit in dressing.keys(): dressing[habit] = dressing[habit] + 1 else: dressing[habit] = 1 ajout("chemises") print("#2. et 3. Ajout avec une fonction :", dressing) exo1() def exo2(): print("\n\n### EXERCICE 2") lst = ['5717', '1133', '5545', '4031', '6398', '2734', '3070', '1346', '7849', '7288', '7587', '6217', '8240', '5733', '6466', '7972', '7341', '6616', '5061', '2441', '2571', '4496', '4831', '5395', '8584', '3033', '6266', '2452', '6909', '3021', '5404', '3799', '5053', '8096', '2488', '8519', '6896', '7300', '5914', '7464', '5068', '1386', '9898', '8313', '1072', '1441', '7333', '5691', '6987', '5255'] nb = {} for e in lst: for l in e: chiffre = int(l) if chiffre in nb: nb[chiffre] += 1 else: nb[chiffre] = 1 max_value = 0 for e in nb.keys(): if nb[e] > max_value: max_value = nb[e] max_key = e print("# Le maximum est", max_key, "qui contient",nb[max_key], "occurence(s)") print(nb) exo2() def exo3(): print("\n\n### EXERCICE 3") exemple_pokemons = { 'Bulbizarre': (70, 7), 'Herbizarre': (100, 13), 'Abo': (200, 7), 'Jungko': (170, 52) } exemple_pokemons['Bulbizarre'] = (60, 10) print(exemple_pokemons) def plus_grand(pokemons: dict) -> tuple: nom_plus_grand = '' taille_max = 0 for nom in pokemons : if pokemons[nom][0] > taille_max: nom_plus_grand = nom taille_max = pokemons[nom][0] return (nom_plus_grand, taille_max) print(plus_grand(exemple_pokemons)) exo3() def exo4(): print("\n\n### EXERCICE 4") # Exercice précédent sur le lion de Némée : https://cgouygou.github.io/1NSI/T02_TypesConstruits/T2.1_Listes/T2.1_Listes3/#2111-exercices # Le lion de Némée : https://pydefis.callicode.fr/defis/Herculito01Lion/txt print("# Le lion de Némée") #1. Écrire une fonction prenant en paramètre une lettre et qui renvoie sa «valeur». def valeurLettre(lettre:str) -> int: return ord(lettre) - 64 assert valeurLettre('A') == 1 #2. Écrire une fonction prenant en paramètre une chaîne de caractères et qui renvoie sa «valeur» def valeurMot(mot:str) -> int: somme = 0 for lettre in mot: somme += valeurLettre(lettre) return somme assert valeurMot('A') == 1 assert valeurMot("BABA") == 6 assert valeurMot("ARTEMIS") == 85 assert valeurMot("ASCLEPIOS") == 99 divinites = 'ARTEMIS ASCLEPIOS ATHENA ATLAS CHARON CHIRON CRONOS DEMETER EOS ERIS EROS GAIA HADES HECATE HEPHAISTOS HERA HERMES HESTIA HYGIE LETO MAIA METIS MNEMOSYNE NYX OCEANOS OURANOS PAN PERSEPHONE POSEIDON RHADAMANTHE SELENE THEMIS THETIS TRITON ZEUS'.split() print("\n# Les divinités :", divinites) #3. Construire un dictionnaire dont les clés sont les noms des divinités et les valeurs leur «valeur» dico ={d:valeurMot(d) for d in divinites} print("\n# Le dictionnaire :", dico) #Défi : les citer dans l'ordre import operator dico_trie = sorted(dico.items(), key=operator.itemgetter(1)) print("\n# Les divinités triées :", dico_trie) exo4() def exo5(): print("\n\n### EXERCICE 5") dates = { "Alan": (23, 6, 1912), "Grace": (9, 12, 1906), "Linus": (28, 12, 1969), "Guido": (31, 1, 1956), "Ada": (10, 12, 1815), "Tim": (8, 6, 1955), "Dennis": (9, 9, 1941), "Hedy": (9, 11, 1914), "Steve": (24, 2, 1955) } dates["Margaret"] = (17, 8, 1936) dates["John"] = (28, 12, 1903) print(dates) def calendrier(dico:dict) -> dict: mois = {1:"janvier", 2:"février", 3:"mars", 4:"avril", 5:"mai", 6:"juin", 7:"juillet", 8:"août", 9:"septembre", 10:"octobre", 11:"novembre", 12:"décembre"} res = {} # Pour chaque nom de mois, on initialise avec une liste vide for m in mois.values(): res[m] = [] # On remplit le dictionnaire résultat for nom in dico: num_mois = dico[nom][1] nom_mois = mois[num_mois] res[nom_mois].append(nom) return res print(calendrier(dates)) def plus_jeune(dico:dict) -> str: annee_max = 0 nom_max = None for nom in dico: if dico[nom][2] > annee_max: annee_max = dico[nom][2] nom_max = nom return nom_max print(plus_jeune(dates)) exo5() def exo6(): print("\n\n### EXERCICE 6") dico_gen = { 'UUU': 'F', 'UUC': 'F', 'UUG': 'L', 'UUA': 'L', 'UCU': 'S', 'UCC': 'S', 'UCG': 'S', 'UCA': 'S', 'UAU': 'Y', 'UAC': 'Y', 'UAG': 'X', 'UAA': 'X', 'UGU': 'C', 'UGC': 'C', 'UGG': 'W', 'UGA': 'X', 'CUU': 'L', 'CUC': 'L', 'CUG': 'L', 'CUA': 'L', 'CCU': 'P', 'CCC': 'P', 'CCG': 'P', 'CCA': 'P', 'CGU': 'R', 'CGC': 'R', 'CGG': 'R', 'CGA': 'R', 'CAU': 'H', 'CAC': 'H', 'CAG': 'Q', 'CAA': 'Q', 'ACU': 'T', 'ACC': 'T', 'ACG': 'T', 'ACA': 'T', 'AUG': 'M', 'AUU': 'I', 'AUC': 'I', 'AUA': 'I', 'AAU': 'N', 'AAC': 'N', 'AAG': 'K', 'AAA': 'K', 'AGU': 'S', 'AGC': 'S', 'AGG': 'R', 'AGA': 'R', 'GUU': 'V', 'GUC': 'V', 'GUG': 'V', 'GUA': 'V', 'GCU': 'A', 'GCC': 'A', 'GCG': 'A', 'GCA': 'A', 'GGU': 'G', 'GGC': 'G', 'GGG': 'G', 'GGA': 'G', 'GAU': 'D', 'GAC': 'D', 'GAG': 'E', 'GAA': 'E' } def traduction(arn:str) -> str: assert(len(arn)%3 == 0) # la chaîne d'ARN doit être un multiple de 3 index = 0 res = "" while(index < len(arn)): codon = arn[index] + arn[index+1] + arn[index+2] res += dico_gen[codon] index += 3 return res assert(traduction('UUCAGUGGG') == 'FSG') print(traduction('UUCAGUGGG')) exo6() def exo7(): print("\n\n### EXERCICE 7") def min_et_max(tab:list) -> dict: assert(len(tab) > 0) res = {"min":tab[0], "max":tab[0]} for i in range(1,len(tab)): if tab[i] < res['min']: res['min'] = tab[i] elif(tab[i] > res['max']): res['max'] = tab[i] return res assert min_et_max([0, 1, 4, 2, -2, 9, 3, 1, 7, 1]) == {'min': -2, 'max': 9} assert min_et_max([0, 1, 2, 3]) == {'min': 0, 'max': 3} assert min_et_max([3]) == {'min': 3, 'max': 3} assert min_et_max([1, 3, 2, 1, 3]) == {'min': 1, 'max': 3} assert min_et_max([-1, -1, -1, -1, -1]) == {'min': -1, 'max': -1} print(min_et_max([0, 1, 4, 2, -2, 9, 3, 1, 7, 1])) exo7() def exo8(): print("\n\n### EXERCICE 8") jours = ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"] mois = [ "janvier", "février", "mars", "avril", "mai", "juin", "juillet", "aout", "septembre", "octobre", "novembre", "décembre", ] duree_mois = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] print("#1.a. :", jours[1]) print("#1.b. :", jours[18 % 7]) def indice_jourApres(nom_jour:str, n:int) -> int: indice_jour = (jours.index(nom_jour) + n) % 7 return indice_jour nom_jour = "mardi" n = 3 indice_jour = indice_jourApres(nom_jour, n) print("#2.", n, "jours après",nom_jour," c'est", jours[indice_jour], "à l'indice", indice_jour) print("#3.a. Il y a", duree_mois[mois.index("mars")], "jours en mars") print("#3.b.") def moisApres(numero_mois:int, x:int) -> str: indice_mois = (numero_mois-1 + x) % 12 print("\t#", x, "mois après le mois \'", mois[numero_mois - 1], "\' c'est le mois \'", mois[indice_mois],"\'") return mois[indice_mois] assert moisApres(4, 5) == "septembre" assert moisApres(8, 4) == "décembre" assert moisApres(10, 3) == "janvier" numero_mois = 10 print("#4.a.", mois[numero_mois - 1]) def jour_suivant(date:tuple) -> tuple: def nom_jourApres(nom_jour:str, n:int) -> str: return jours[(jours.index(nom_jour) + n) % 7] # date est un tuple (nom_jour, numero_jour, numero_mois, annee) nom = date [0] jour = date[1] mois = date[2] annee = date[3] nom_jour = nom_jourApres(nom, 1) if(jour == duree_mois[mois-1]): numero_jour = 1 numero_mois = mois + 1 else: numero_jour = jour + 1 numero_mois = mois if(numero_jour == 1 and numero_mois == 13): annee = annee + 1 numero_mois = 1 else: annee = annee return (nom_jour, numero_jour, numero_mois, annee) assert jour_suivant(("samedi", 21, 10, 1995)) == ("dimanche", 22, 10, 1995) assert jour_suivant(("mardi", 31, 10, 1995)) == ("mercredi", 1, 11, 1995) assert jour_suivant(("dimanche", 31, 12, 2000)) == ("lundi", 1, 1, 2001) exo8()