ajout du TP de rentrée Z-Event
This commit is contained in:
@@ -0,0 +1,568 @@
|
||||
# Corrigé du TP Z-Event
|
||||
|
||||
---
|
||||
|
||||
## Partie 1 : Le compteur (variables et types)
|
||||
|
||||
### 1.1. État de la cagnotte
|
||||
|
||||
**Question 1.1** :
|
||||
|
||||
```python
|
||||
pourcentage = round(collecte / objectif * 100, 2)
|
||||
|
||||
print(f"Objectif atteint à {pourcentage} %") # Objectif atteint à 73.5 %
|
||||
```
|
||||
|
||||
**Question 1.2** :
|
||||
|
||||
```python
|
||||
reste = objectif - collecte
|
||||
|
||||
print(f"Il manque {reste} euros") # Il manque 662549.25 euros
|
||||
```
|
||||
|
||||
### 1.2. Le piège du compteur
|
||||
|
||||
**Question 1.3** :
|
||||
|
||||
```python
|
||||
don_1 = "25"
|
||||
don_2 = "40"
|
||||
|
||||
total = don_1 + don_2
|
||||
print(total) # affiche 2540 : sur deux chaînes, + est une concaténation
|
||||
|
||||
# Correction : convertir les chaînes en nombres avant d'additionner
|
||||
total = int(don_1) + int(don_2)
|
||||
print(total) # 65
|
||||
```
|
||||
|
||||
## Partie 2 : L'annonce des dons (fonctions)
|
||||
|
||||
### 2.1. Formater un don
|
||||
|
||||
**Question 2.1** :
|
||||
|
||||
```python
|
||||
def formater_don(pseudo, montant):
|
||||
return f"{pseudo} vient de donner {montant} euros"
|
||||
|
||||
# Test
|
||||
print(formater_don("Nyx_42", 5.0)) # Nyx_42 vient de donner 5.0 euros
|
||||
print(formater_don("Marion_NSI", 100.0)) # Marion_NSI vient de donner 100.0 euros
|
||||
```
|
||||
|
||||
### 2.2. Repérer les gros dons
|
||||
|
||||
**Question 2.2** :
|
||||
|
||||
```python
|
||||
def est_gros_don(montant, seuil=50):
|
||||
return montant >= seuil
|
||||
|
||||
# Test
|
||||
print(est_gros_don(120.0)) # True
|
||||
print(est_gros_don(50.0)) # True
|
||||
print(est_gros_don(20.0)) # False
|
||||
print(est_gros_don(20.0, 10)) # True
|
||||
```
|
||||
|
||||
### 2.3. Les paliers
|
||||
|
||||
**Question 2.3** :
|
||||
|
||||
```python
|
||||
def palier(total):
|
||||
if total < 500000:
|
||||
return "Départ"
|
||||
elif total < 1000000:
|
||||
return "Palier 1"
|
||||
elif total < 2000000:
|
||||
return "Palier 2"
|
||||
else:
|
||||
return "Objectif"
|
||||
|
||||
# Test
|
||||
print(palier(120000)) # Départ
|
||||
print(palier(750000)) # Palier 1
|
||||
print(palier(1837450.75)) # Palier 2
|
||||
print(palier(2500000)) # Objectif
|
||||
```
|
||||
|
||||
### 2.4. Le message complet
|
||||
|
||||
**Question 2.4** :
|
||||
|
||||
```python
|
||||
def annonce_regie(pseudo, montant):
|
||||
message = formater_don(pseudo, montant)
|
||||
if est_gros_don(montant):
|
||||
message = message + " [GROS DON]"
|
||||
return message
|
||||
|
||||
# Test
|
||||
print(annonce_regie("Poulpe3000", 2.0)) # Poulpe3000 vient de donner 2.0 euros
|
||||
print(annonce_regie("Anonyme", 250.0)) # Anonyme vient de donner 250.0 euros [GROS DON]
|
||||
```
|
||||
|
||||
## Partie 3 : L'historique des dons (boucles et listes)
|
||||
|
||||
### 3.1. Le total collecté
|
||||
|
||||
**Question 3.1** :
|
||||
|
||||
```python
|
||||
def total_dons(liste_dons):
|
||||
total = 0
|
||||
for don in liste_dons:
|
||||
total = total + don["montant"]
|
||||
return total
|
||||
|
||||
# Test
|
||||
print(total_dons(dons)) # 1449.5
|
||||
print(total_dons([])) # 0
|
||||
```
|
||||
|
||||
### 3.2. Le don le plus élevé
|
||||
|
||||
**Question 3.2** :
|
||||
|
||||
```python
|
||||
def plus_gros_don(liste_dons):
|
||||
record = liste_dons[0] # on part du premier don, surtout pas de 0
|
||||
for don in liste_dons:
|
||||
if don["montant"] > record["montant"]:
|
||||
record = don
|
||||
return record
|
||||
|
||||
# Test
|
||||
record = plus_gros_don(dons)
|
||||
print(record["pseudo"], record["montant"]) # Anonyme 500.0
|
||||
```
|
||||
|
||||
### 3.3. Compter et filtrer
|
||||
|
||||
**Question 3.3** :
|
||||
|
||||
```python
|
||||
def compter_gros_dons(liste_dons, seuil=50):
|
||||
compteur = 0
|
||||
for don in liste_dons:
|
||||
if est_gros_don(don["montant"], seuil):
|
||||
compteur = compteur + 1
|
||||
return compteur
|
||||
|
||||
# Test
|
||||
print(compter_gros_dons(dons)) # 7
|
||||
print(compter_gros_dons(dons, 100)) # 4
|
||||
```
|
||||
|
||||
**Question 3.4** :
|
||||
|
||||
```python
|
||||
def donateurs(liste_dons):
|
||||
pseudos = []
|
||||
for don in liste_dons:
|
||||
if don["pseudo"] not in pseudos:
|
||||
pseudos.append(don["pseudo"])
|
||||
return pseudos
|
||||
|
||||
# Test
|
||||
print(donateurs(dons))
|
||||
# ['Nyx_42', 'TitiLeBg', 'Camille_R', 'Poulpe3000', 'Anonyme', 'Sam_du_59', 'Lulu_2007', 'Marion_NSI', 'Kevin_B']
|
||||
```
|
||||
|
||||
### 3.4. Le bandeau des derniers dons
|
||||
|
||||
**Question 3.5** :
|
||||
|
||||
```python
|
||||
def derniers_dons(liste_dons, n):
|
||||
return liste_dons[-n:]
|
||||
|
||||
# Test
|
||||
for don in derniers_dons(dons, 3):
|
||||
print(don["heure"], don["pseudo"])
|
||||
# 21:39 Anonyme
|
||||
# 21:48 Poulpe3000
|
||||
# 21:59 Marion_NSI
|
||||
```
|
||||
|
||||
## Partie 4 : Le classement des chaînes (dictionnaires)
|
||||
|
||||
### 4.1. La cagnotte de chaque chaîne
|
||||
|
||||
**Question 4.1** :
|
||||
|
||||
```python
|
||||
def cagnottes(liste_dons):
|
||||
totaux = {}
|
||||
for don in liste_dons:
|
||||
streamer = don["streamer"]
|
||||
if streamer in totaux:
|
||||
totaux[streamer] = totaux[streamer] + don["montant"]
|
||||
else:
|
||||
totaux[streamer] = don["montant"]
|
||||
return totaux
|
||||
|
||||
# Test
|
||||
totaux = cagnottes(dons)
|
||||
print(totaux["Antoine Daniel"]) # 115.0
|
||||
print(totaux["Joueur du Grenier"]) # 140.0
|
||||
print(len(totaux)) # 13
|
||||
```
|
||||
|
||||
### 4.2. La chaîne en tête
|
||||
|
||||
**Question 4.2** :
|
||||
|
||||
```python
|
||||
def meilleure_chaine(totaux):
|
||||
meilleur = None
|
||||
for streamer in totaux:
|
||||
if meilleur is None or totaux[streamer] > totaux[meilleur]:
|
||||
meilleur = streamer
|
||||
return meilleur
|
||||
|
||||
# Test
|
||||
print(meilleure_chaine(cagnottes(dons))) # MisterMV
|
||||
```
|
||||
|
||||
### 4.3. Le détail des dons
|
||||
|
||||
**Question 4.3** :
|
||||
|
||||
```python
|
||||
def dons_par_chaine(liste_dons):
|
||||
detail = {}
|
||||
for don in liste_dons:
|
||||
streamer = don["streamer"]
|
||||
if streamer not in detail:
|
||||
detail[streamer] = []
|
||||
detail[streamer].append(don["montant"])
|
||||
return detail
|
||||
|
||||
# Test
|
||||
detail = dons_par_chaine(dons)
|
||||
print(detail["MisterMV"]) # [20.0, 12.0, 500.0]
|
||||
print(detail["Baghera Jones"]) # [15.5, 5.0, 35.0]
|
||||
```
|
||||
|
||||
### 4.4. Le podium
|
||||
|
||||
**Question 4.4** :
|
||||
|
||||
```python
|
||||
def podium(totaux):
|
||||
classement = sorted(totaux, key=lambda streamer: totaux[streamer], reverse=True)
|
||||
return classement[:3]
|
||||
|
||||
# Test
|
||||
print(podium(cagnottes(dons)))
|
||||
# ['MisterMV', 'Domingo', 'Ultia']
|
||||
```
|
||||
|
||||
## Partie 5 : Le planning et les raids (synthèse)
|
||||
|
||||
### 5.1. Qui est à l'antenne
|
||||
|
||||
**Question 5.1** :
|
||||
|
||||
```python
|
||||
def a_l_antenne(planning, heure):
|
||||
for streamer, debut, fin in planning:
|
||||
if debut < fin:
|
||||
if debut <= heure < fin:
|
||||
return streamer
|
||||
else:
|
||||
# créneau à cheval sur minuit
|
||||
if heure >= debut or heure < fin:
|
||||
return streamer
|
||||
return None
|
||||
|
||||
# Test
|
||||
print(a_l_antenne(planning, 19)) # Antoine Daniel
|
||||
print(a_l_antenne(planning, 22)) # Joueur du Grenier
|
||||
print(a_l_antenne(planning, 1)) # Baghera Jones
|
||||
print(a_l_antenne(planning, 10)) # None
|
||||
```
|
||||
|
||||
### 5.2. Suivre la chaîne des raids
|
||||
|
||||
**Question 5.2** :
|
||||
|
||||
```python
|
||||
def suivre_raids(raids, depart):
|
||||
parcours = [depart]
|
||||
courant = depart
|
||||
while raids.get(courant): # .get évite l'erreur si la clé est absente
|
||||
courant = raids[courant][0]
|
||||
parcours.append(courant)
|
||||
return parcours
|
||||
|
||||
# Test
|
||||
print(suivre_raids(raids, "Antoine Daniel"))
|
||||
# ['Antoine Daniel', 'MisterMV', 'Joueur du Grenier', 'Baghera Jones', 'Ultia']
|
||||
print(suivre_raids(raids, "Horty"))
|
||||
# ['Horty', 'Sylvain Levy']
|
||||
print(suivre_raids(raids, "Ultia"))
|
||||
# ['Ultia']
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Code complet fonctionnel
|
||||
|
||||
```python
|
||||
# Toutes ces données sont inventées pour l'exercice.
|
||||
objectif = 2500000
|
||||
collecte = 1837450.75
|
||||
|
||||
dons = [
|
||||
{"pseudo": "Nyx_42", "streamer": "Antoine Daniel", "montant": 5.0, "heure": "18:04"},
|
||||
{"pseudo": "TitiLeBg", "streamer": "MisterMV", "montant": 20.0, "heure": "18:11"},
|
||||
{"pseudo": "Camille_R", "streamer": "Joueur du Grenier", "montant": 50.0, "heure": "18:23"},
|
||||
{"pseudo": "Nyx_42", "streamer": "Baghera Jones", "montant": 15.5, "heure": "18:37"},
|
||||
{"pseudo": "Poulpe3000", "streamer": "Horty", "montant": 2.0, "heure": "18:41"},
|
||||
{"pseudo": "Anonyme", "streamer": "Ultia", "montant": 120.0, "heure": "18:52"},
|
||||
{"pseudo": "Sam_du_59", "streamer": "Antoine Daniel", "montant": 10.0, "heure": "19:03"},
|
||||
{"pseudo": "Camille_R", "streamer": "Sylvain Levy", "montant": 30.0, "heure": "19:14"},
|
||||
{"pseudo": "Lulu_2007", "streamer": "ZeratoR", "montant": 8.5, "heure": "19:20"},
|
||||
{"pseudo": "TitiLeBg", "streamer": "Etoiles", "montant": 45.0, "heure": "19:33"},
|
||||
{"pseudo": "Marion_NSI", "streamer": "Joueur du Grenier", "montant": 75.0, "heure": "19:47"},
|
||||
{"pseudo": "Poulpe3000", "streamer": "MisterMV", "montant": 12.0, "heure": "19:55"},
|
||||
{"pseudo": "Anonyme", "streamer": "Domingo", "montant": 250.0, "heure": "20:08"},
|
||||
{"pseudo": "Kevin_B", "streamer": "Baghera Jones", "montant": 5.0, "heure": "20:16"},
|
||||
{"pseudo": "Nyx_42", "streamer": "Maghla", "montant": 60.0, "heure": "20:29"},
|
||||
{"pseudo": "Sam_du_59", "streamer": "Horty", "montant": 3.5, "heure": "20:38"},
|
||||
{"pseudo": "Marion_NSI", "streamer": "Antoine Daniel", "montant": 100.0, "heure": "20:44"},
|
||||
{"pseudo": "Lulu_2007", "streamer": "Alphacast", "montant": 18.0, "heure": "20:57"},
|
||||
{"pseudo": "Camille_R", "streamer": "Ultia", "montant": 40.0, "heure": "21:05"},
|
||||
{"pseudo": "Kevin_B", "streamer": "Ponce", "montant": 22.5, "heure": "21:12"},
|
||||
{"pseudo": "TitiLeBg", "streamer": "Joueur du Grenier", "montant": 15.0, "heure": "21:26"},
|
||||
{"pseudo": "Anonyme", "streamer": "MisterMV", "montant": 500.0, "heure": "21:39"},
|
||||
{"pseudo": "Poulpe3000", "streamer": "Sylvain Levy", "montant": 7.5, "heure": "21:48"},
|
||||
{"pseudo": "Marion_NSI", "streamer": "Baghera Jones", "montant": 35.0, "heure": "21:59"},
|
||||
]
|
||||
|
||||
planning = [
|
||||
("Antoine Daniel", 18, 20),
|
||||
("MisterMV", 20, 22),
|
||||
("Joueur du Grenier", 22, 24),
|
||||
("Baghera Jones", 0, 3),
|
||||
("Ultia", 3, 6),
|
||||
]
|
||||
|
||||
raids = {
|
||||
"Antoine Daniel": ["MisterMV"],
|
||||
"MisterMV": ["Joueur du Grenier", "Horty"],
|
||||
"Joueur du Grenier": ["Baghera Jones"],
|
||||
"Baghera Jones": ["Ultia"],
|
||||
"Ultia": [],
|
||||
"Horty": ["Sylvain Levy"],
|
||||
"Sylvain Levy": [],
|
||||
}
|
||||
|
||||
pourcentage = round(collecte / objectif * 100, 2)
|
||||
|
||||
print(f"Objectif atteint à {pourcentage} %") # Objectif atteint à 73.5 %
|
||||
|
||||
reste = objectif - collecte
|
||||
|
||||
print(f"Il manque {reste} euros") # Il manque 662549.25 euros
|
||||
|
||||
don_1 = "25"
|
||||
don_2 = "40"
|
||||
|
||||
total = don_1 + don_2
|
||||
print(total) # affiche 2540 : sur deux chaînes, + est une concaténation
|
||||
|
||||
# Correction : convertir les chaînes en nombres avant d'additionner
|
||||
total = int(don_1) + int(don_2)
|
||||
print(total) # 65
|
||||
|
||||
def formater_don(pseudo, montant):
|
||||
return f"{pseudo} vient de donner {montant} euros"
|
||||
|
||||
# Test
|
||||
print(formater_don("Nyx_42", 5.0)) # Nyx_42 vient de donner 5.0 euros
|
||||
print(formater_don("Marion_NSI", 100.0)) # Marion_NSI vient de donner 100.0 euros
|
||||
|
||||
def est_gros_don(montant, seuil=50):
|
||||
return montant >= seuil
|
||||
|
||||
# Test
|
||||
print(est_gros_don(120.0)) # True
|
||||
print(est_gros_don(50.0)) # True
|
||||
print(est_gros_don(20.0)) # False
|
||||
print(est_gros_don(20.0, 10)) # True
|
||||
|
||||
def palier(total):
|
||||
if total < 500000:
|
||||
return "Départ"
|
||||
elif total < 1000000:
|
||||
return "Palier 1"
|
||||
elif total < 2000000:
|
||||
return "Palier 2"
|
||||
else:
|
||||
return "Objectif"
|
||||
|
||||
# Test
|
||||
print(palier(120000)) # Départ
|
||||
print(palier(750000)) # Palier 1
|
||||
print(palier(1837450.75)) # Palier 2
|
||||
print(palier(2500000)) # Objectif
|
||||
|
||||
def annonce_regie(pseudo, montant):
|
||||
message = formater_don(pseudo, montant)
|
||||
if est_gros_don(montant):
|
||||
message = message + " [GROS DON]"
|
||||
return message
|
||||
|
||||
# Test
|
||||
print(annonce_regie("Poulpe3000", 2.0)) # Poulpe3000 vient de donner 2.0 euros
|
||||
print(annonce_regie("Anonyme", 250.0)) # Anonyme vient de donner 250.0 euros [GROS DON]
|
||||
|
||||
def total_dons(liste_dons):
|
||||
total = 0
|
||||
for don in liste_dons:
|
||||
total = total + don["montant"]
|
||||
return total
|
||||
|
||||
# Test
|
||||
print(total_dons(dons)) # 1449.5
|
||||
print(total_dons([])) # 0
|
||||
|
||||
def plus_gros_don(liste_dons):
|
||||
record = liste_dons[0] # on part du premier don, surtout pas de 0
|
||||
for don in liste_dons:
|
||||
if don["montant"] > record["montant"]:
|
||||
record = don
|
||||
return record
|
||||
|
||||
# Test
|
||||
record = plus_gros_don(dons)
|
||||
print(record["pseudo"], record["montant"]) # Anonyme 500.0
|
||||
|
||||
def compter_gros_dons(liste_dons, seuil=50):
|
||||
compteur = 0
|
||||
for don in liste_dons:
|
||||
if est_gros_don(don["montant"], seuil):
|
||||
compteur = compteur + 1
|
||||
return compteur
|
||||
|
||||
# Test
|
||||
print(compter_gros_dons(dons)) # 7
|
||||
print(compter_gros_dons(dons, 100)) # 4
|
||||
|
||||
def donateurs(liste_dons):
|
||||
pseudos = []
|
||||
for don in liste_dons:
|
||||
if don["pseudo"] not in pseudos:
|
||||
pseudos.append(don["pseudo"])
|
||||
return pseudos
|
||||
|
||||
# Test
|
||||
print(donateurs(dons))
|
||||
# ['Nyx_42', 'TitiLeBg', 'Camille_R', 'Poulpe3000', 'Anonyme', 'Sam_du_59', 'Lulu_2007', 'Marion_NSI', 'Kevin_B']
|
||||
|
||||
def derniers_dons(liste_dons, n):
|
||||
return liste_dons[-n:]
|
||||
|
||||
# Test
|
||||
for don in derniers_dons(dons, 3):
|
||||
print(don["heure"], don["pseudo"])
|
||||
# 21:39 Anonyme
|
||||
# 21:48 Poulpe3000
|
||||
# 21:59 Marion_NSI
|
||||
|
||||
def cagnottes(liste_dons):
|
||||
totaux = {}
|
||||
for don in liste_dons:
|
||||
streamer = don["streamer"]
|
||||
if streamer in totaux:
|
||||
totaux[streamer] = totaux[streamer] + don["montant"]
|
||||
else:
|
||||
totaux[streamer] = don["montant"]
|
||||
return totaux
|
||||
|
||||
# Test
|
||||
totaux = cagnottes(dons)
|
||||
print(totaux["Antoine Daniel"]) # 115.0
|
||||
print(totaux["Joueur du Grenier"]) # 140.0
|
||||
print(len(totaux)) # 13
|
||||
|
||||
def meilleure_chaine(totaux):
|
||||
meilleur = None
|
||||
for streamer in totaux:
|
||||
if meilleur is None or totaux[streamer] > totaux[meilleur]:
|
||||
meilleur = streamer
|
||||
return meilleur
|
||||
|
||||
# Test
|
||||
print(meilleure_chaine(cagnottes(dons))) # MisterMV
|
||||
|
||||
def dons_par_chaine(liste_dons):
|
||||
detail = {}
|
||||
for don in liste_dons:
|
||||
streamer = don["streamer"]
|
||||
if streamer not in detail:
|
||||
detail[streamer] = []
|
||||
detail[streamer].append(don["montant"])
|
||||
return detail
|
||||
|
||||
# Test
|
||||
detail = dons_par_chaine(dons)
|
||||
print(detail["MisterMV"]) # [20.0, 12.0, 500.0]
|
||||
print(detail["Baghera Jones"]) # [15.5, 5.0, 35.0]
|
||||
|
||||
def podium(totaux):
|
||||
classement = sorted(totaux, key=lambda streamer: totaux[streamer], reverse=True)
|
||||
return classement[:3]
|
||||
|
||||
# Test
|
||||
print(podium(cagnottes(dons)))
|
||||
# ['MisterMV', 'Domingo', 'Ultia']
|
||||
|
||||
def a_l_antenne(planning, heure):
|
||||
for streamer, debut, fin in planning:
|
||||
if debut < fin:
|
||||
if debut <= heure < fin:
|
||||
return streamer
|
||||
else:
|
||||
# créneau à cheval sur minuit
|
||||
if heure >= debut or heure < fin:
|
||||
return streamer
|
||||
return None
|
||||
|
||||
# Test
|
||||
print(a_l_antenne(planning, 19)) # Antoine Daniel
|
||||
print(a_l_antenne(planning, 22)) # Joueur du Grenier
|
||||
print(a_l_antenne(planning, 1)) # Baghera Jones
|
||||
print(a_l_antenne(planning, 10)) # None
|
||||
|
||||
def suivre_raids(raids, depart):
|
||||
parcours = [depart]
|
||||
courant = depart
|
||||
while raids.get(courant): # .get évite l'erreur si la clé est absente
|
||||
courant = raids[courant][0]
|
||||
parcours.append(courant)
|
||||
return parcours
|
||||
|
||||
# Test
|
||||
print(suivre_raids(raids, "Antoine Daniel"))
|
||||
# ['Antoine Daniel', 'MisterMV', 'Joueur du Grenier', 'Baghera Jones', 'Ultia']
|
||||
print(suivre_raids(raids, "Horty"))
|
||||
# ['Horty', 'Sylvain Levy']
|
||||
print(suivre_raids(raids, "Ultia"))
|
||||
# ['Ultia']
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Auteur : Florian Mathieu
|
||||
|
||||
Licence CC BY-SA
|
||||
|
||||
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="Licence Creative Commons" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png" /></a> <br />Ce cours est mis à disposition selon les termes de la <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Licence Creative Commons Attribution - Partage dans les Mêmes Conditions 4.0 International</a>.
|
||||
Reference in New Issue
Block a user