ajout exercices, corrections diverses, glossaire
- Ajout des 10 TPs d'évaluation (sans PDF) - Création GLOSSAIRE.md et AMELIORATIONS.md - Corrections f-strings, eval(), sommaires Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
336
representation_construits/evaluation/TP10_Voyage_aide.md
Normal file
336
representation_construits/evaluation/TP10_Voyage_aide.md
Normal file
@@ -0,0 +1,336 @@
|
||||
# Aide - TP10 Planification de Voyage
|
||||
|
||||
## Rappels utiles
|
||||
|
||||
### Structure des villes (tuples)
|
||||
```python
|
||||
# Ville = (nom, pays, langue, monnaie, decalage_horaire)
|
||||
ville = villes[0]
|
||||
nom = ville[0] # "Paris"
|
||||
pays = ville[1] # "France"
|
||||
langue = ville[2] # "Francais"
|
||||
monnaie = ville[3] # "EUR"
|
||||
decalage = ville[4] # 0
|
||||
|
||||
# Parcourir les villes
|
||||
for ville in villes:
|
||||
print(ville[0], "-", ville[1])
|
||||
```
|
||||
|
||||
### Structure des hotels (dictionnaires)
|
||||
```python
|
||||
hotels = {
|
||||
"Paris": [
|
||||
{"nom": "Hotel Lumiere", "etoiles": 4, "prix_nuit": 150, "services": ["wifi", "spa"], "note": 8.5},
|
||||
{"nom": "Petit Paris", "etoiles": 2, "prix_nuit": 65, "services": ["wifi"], "note": 7.2}
|
||||
],
|
||||
"Londres": [...]
|
||||
}
|
||||
|
||||
# Acceder aux hotels de Paris
|
||||
hotels_paris = hotels["Paris"]
|
||||
for h in hotels_paris:
|
||||
print(h["nom"], "-", h["prix_nuit"], "euros/nuit")
|
||||
```
|
||||
|
||||
### Structure des vols (tuples)
|
||||
```python
|
||||
# Vol = (compagnie, depart, arrivee, heure_depart, heure_arrivee, prix, escales)
|
||||
vol = vols[0]
|
||||
compagnie = vol[0] # "Air France"
|
||||
depart = vol[1] # "Paris"
|
||||
arrivee = vol[2] # "Londres"
|
||||
heure_dep = vol[3] # "08:00"
|
||||
heure_arr = vol[4] # "09:15"
|
||||
prix = vol[5] # 120
|
||||
escales = vol[6] # 0
|
||||
```
|
||||
|
||||
### Structure des activites (dictionnaires)
|
||||
```python
|
||||
activites = {
|
||||
"Paris": [
|
||||
{"nom": "Tour Eiffel", "duree": 3, "prix": 25, "type": "monument"},
|
||||
{"nom": "Louvre", "duree": 4, "prix": 17, "type": "musee"}
|
||||
]
|
||||
}
|
||||
|
||||
# Acceder aux activites d'une ville
|
||||
acts_paris = activites.get("Paris", []) # [] si la ville n'existe pas
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Exercice 1
|
||||
|
||||
### 1.1 - Fiche destination
|
||||
```python
|
||||
def fiche_destination(nom_ville, villes, hotels, vols):
|
||||
# Trouver la ville
|
||||
ville = None
|
||||
for v in villes:
|
||||
if v[0] == nom_ville:
|
||||
ville = v
|
||||
break
|
||||
|
||||
if ville == None:
|
||||
return "Ville non trouvee"
|
||||
|
||||
print("===", ville[0], "-", ville[1], "===")
|
||||
print("Langue:", ville[2])
|
||||
print("Monnaie:", ville[3])
|
||||
print("Decalage horaire:", ville[4], "h")
|
||||
|
||||
# Compter les hotels
|
||||
if nom_ville in hotels:
|
||||
nb_hotels = len(hotels[nom_ville])
|
||||
else:
|
||||
nb_hotels = 0
|
||||
print("Hotels disponibles:", nb_hotels)
|
||||
|
||||
# Compter les vols
|
||||
nb_vols = 0
|
||||
for vol in vols:
|
||||
if vol[2] == nom_ville: # arrivee
|
||||
nb_vols = nb_vols + 1
|
||||
print("Vols disponibles:", nb_vols)
|
||||
```
|
||||
|
||||
### 1.2 - Recherche de destinations
|
||||
```python
|
||||
def rechercher_destinations(villes, monnaie, decalage_max):
|
||||
resultats = []
|
||||
|
||||
for ville in villes:
|
||||
# Verifier la monnaie
|
||||
if monnaie is not None and ville[3] != monnaie:
|
||||
continue
|
||||
|
||||
# Verifier le decalage horaire
|
||||
if decalage_max is not None and abs(ville[4]) > decalage_max:
|
||||
continue
|
||||
|
||||
resultats.append(ville)
|
||||
|
||||
return resultats
|
||||
```
|
||||
|
||||
### 1.3 - Statistiques
|
||||
```python
|
||||
def stats_base(villes, hotels, vols):
|
||||
# Prix moyen des hotels
|
||||
total_prix_hotels = 0
|
||||
nb_hotels = 0
|
||||
for ville, liste_hotels in hotels.items():
|
||||
for h in liste_hotels:
|
||||
total_prix_hotels = total_prix_hotels + h["prix_nuit"]
|
||||
nb_hotels = nb_hotels + 1
|
||||
|
||||
if nb_hotels > 0:
|
||||
prix_hotel_moyen = total_prix_hotels / nb_hotels
|
||||
else:
|
||||
prix_hotel_moyen = 0
|
||||
|
||||
# Prix moyen des vols
|
||||
total_prix_vols = 0
|
||||
for vol in vols:
|
||||
total_prix_vols = total_prix_vols + vol[5]
|
||||
|
||||
if len(vols) > 0:
|
||||
prix_vol_moyen = total_prix_vols / len(vols)
|
||||
else:
|
||||
prix_vol_moyen = 0
|
||||
|
||||
return {
|
||||
"nb_destinations": len(villes),
|
||||
"nb_hotels": nb_hotels,
|
||||
"nb_vols": len(vols),
|
||||
"prix_hotel_moyen": round(prix_hotel_moyen, 2),
|
||||
"prix_vol_moyen": round(prix_vol_moyen, 2)
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Exercice 2
|
||||
|
||||
### 2.1 - Trouver les meilleurs vols
|
||||
```python
|
||||
def trouver_vols(vols, destination, critere):
|
||||
# Filtrer les vols vers la destination
|
||||
vols_dest = []
|
||||
for v in vols:
|
||||
if v[2] == destination:
|
||||
vols_dest.append(v)
|
||||
|
||||
if critere == "prix":
|
||||
vols_dest = sorted(vols_dest, key=lambda v: v[5])
|
||||
|
||||
elif critere == "direct":
|
||||
# Direct d'abord (escales = 0), puis par prix
|
||||
vols_dest = sorted(vols_dest, key=lambda v: (v[6], v[5]))
|
||||
|
||||
return vols_dest
|
||||
```
|
||||
|
||||
### 2.2 - Trouver l'hotel ideal
|
||||
```python
|
||||
def trouver_hotel(hotels, ville, budget_nuit, services_requis, etoiles_min):
|
||||
if ville not in hotels:
|
||||
return []
|
||||
|
||||
resultats = []
|
||||
for h in hotels[ville]:
|
||||
# Verifier le budget
|
||||
if budget_nuit is not None and h["prix_nuit"] > budget_nuit:
|
||||
continue
|
||||
|
||||
# Verifier les etoiles
|
||||
if etoiles_min is not None and h["etoiles"] < etoiles_min:
|
||||
continue
|
||||
|
||||
# Verifier les services
|
||||
if services_requis is not None:
|
||||
a_tous_services = True
|
||||
for service in services_requis:
|
||||
if service not in h["services"]:
|
||||
a_tous_services = False
|
||||
break
|
||||
if not a_tous_services:
|
||||
continue
|
||||
|
||||
resultats.append(h)
|
||||
|
||||
# Trier par note decroissante
|
||||
resultats = sorted(resultats, key=lambda h: h["note"], reverse=True)
|
||||
return resultats
|
||||
```
|
||||
|
||||
### 2.3 - Planifier les activites
|
||||
```python
|
||||
def planifier_activites(ville, activites, jours, budget_max):
|
||||
if ville not in activites:
|
||||
return {"planning": [], "budget_utilise": 0}
|
||||
|
||||
acts_ville = activites[ville]
|
||||
|
||||
# Trier par note ou popularite (ici par prix croissant comme exemple)
|
||||
acts_triees = sorted(acts_ville, key=lambda a: a["prix"])
|
||||
|
||||
planning = []
|
||||
budget_utilise = 0
|
||||
|
||||
for act in acts_triees:
|
||||
if budget_utilise + act["prix"] <= budget_max:
|
||||
planning.append(act)
|
||||
budget_utilise = budget_utilise + act["prix"]
|
||||
|
||||
return {
|
||||
"planning": planning,
|
||||
"budget_utilise": budget_utilise
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Exercice 3 (Bonus)
|
||||
|
||||
### 3.1 - Calculer le budget total
|
||||
```python
|
||||
def calculer_budget(vol, hotel, nuits, activites_choisies, activites, ville):
|
||||
budget = {
|
||||
"vol": vol[5],
|
||||
"hotel": hotel["prix_nuit"] * nuits,
|
||||
"activites": 0
|
||||
}
|
||||
|
||||
if ville in activites:
|
||||
for act in activites[ville]:
|
||||
if act["nom"] in activites_choisies:
|
||||
budget["activites"] = budget["activites"] + act["prix"]
|
||||
|
||||
budget["total"] = budget["vol"] + budget["hotel"] + budget["activites"]
|
||||
return budget
|
||||
```
|
||||
|
||||
### 3.2 - Voyage optimal
|
||||
```python
|
||||
def voyage_optimal(destination, budget_max, nuits, villes, hotels, vols):
|
||||
# Trouver les vols
|
||||
vols_dest = []
|
||||
for v in vols:
|
||||
if v[2] == destination:
|
||||
vols_dest.append(v)
|
||||
|
||||
if len(vols_dest) == 0:
|
||||
return None
|
||||
|
||||
# Trouver les hotels
|
||||
if destination not in hotels:
|
||||
return None
|
||||
hotels_dest = hotels[destination]
|
||||
|
||||
# Trier par prix
|
||||
vols_dest = sorted(vols_dest, key=lambda v: v[5])
|
||||
hotels_dest = sorted(hotels_dest, key=lambda h: h["prix_nuit"])
|
||||
|
||||
# Essayer les combinaisons
|
||||
for vol in vols_dest:
|
||||
for hotel in hotels_dest:
|
||||
cout_base = vol[5] + hotel["prix_nuit"] * nuits
|
||||
|
||||
if cout_base <= budget_max:
|
||||
return {
|
||||
"destination": destination,
|
||||
"vol": vol,
|
||||
"hotel": hotel,
|
||||
"budget_total": cout_base
|
||||
}
|
||||
|
||||
return None
|
||||
```
|
||||
|
||||
### 3.3 - Comparateur de destinations
|
||||
```python
|
||||
def comparer_destinations(destinations, budget, nuits, villes, hotels, vols, activites):
|
||||
comparaison = []
|
||||
|
||||
for dest in destinations:
|
||||
voyage = voyage_optimal(dest, budget, nuits, villes, hotels, vols)
|
||||
|
||||
if voyage != None:
|
||||
# Compter les activites possibles
|
||||
budget_restant = budget - voyage["budget_total"]
|
||||
nb_acts = 0
|
||||
if dest in activites:
|
||||
for act in activites[dest]:
|
||||
if act["prix"] <= budget_restant:
|
||||
nb_acts = nb_acts + 1
|
||||
|
||||
# Score = note hotel + nb activites possibles
|
||||
score = voyage["hotel"]["note"] + nb_acts * 0.5
|
||||
|
||||
comparaison.append({
|
||||
"ville": dest,
|
||||
"budget": voyage["budget_total"],
|
||||
"note_hotel": voyage["hotel"]["note"],
|
||||
"nb_activites": nb_acts,
|
||||
"score": score
|
||||
})
|
||||
|
||||
# Trier par score decroissant
|
||||
comparaison = sorted(comparaison, key=lambda x: x["score"], reverse=True)
|
||||
return comparaison
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Pieges a eviter
|
||||
|
||||
1. **Tuples immutables** : impossible de modifier `vol[5] = 100`
|
||||
2. **Services = liste** : utilisez `in` pour verifier si un service est disponible
|
||||
3. **Escales** : 0 = vol direct, > 0 = avec escale(s)
|
||||
4. **Decalage horaire** : peut etre negatif (New York = -6)
|
||||
5. **Activites par ville** : utilisez `.get(ville, [])` pour eviter KeyError si ville sans activites
|
||||
6. **Budget** : vol + (hotel x nuits) + activites
|
||||
7. **Acces par indice pour les tuples** : ville[0]=nom, ville[1]=pays, vol[5]=prix, etc.
|
||||
Reference in New Issue
Block a user