129 lines
3.3 KiB
Markdown
129 lines
3.3 KiB
Markdown
|
|
# Aide - TP01 Spotify
|
||
|
|
|
||
|
|
## Rappels utiles
|
||
|
|
|
||
|
|
### Acceder aux elements d'un dictionnaire
|
||
|
|
```python
|
||
|
|
chanson = {"titre": "Blinding Lights", "artiste": "The Weeknd", "duree": 203}
|
||
|
|
print(chanson["titre"]) # Blinding Lights
|
||
|
|
print(chanson.get("note", 0)) # 0 (valeur par defaut si cle absente)
|
||
|
|
```
|
||
|
|
|
||
|
|
### Convertir des secondes en minutes:secondes
|
||
|
|
```python
|
||
|
|
duree = 203 # secondes
|
||
|
|
minutes = duree // 60 # Division entiere
|
||
|
|
secondes = duree % 60 # Reste (modulo)
|
||
|
|
print(minutes, ":", secondes) # 3 : 23
|
||
|
|
```
|
||
|
|
|
||
|
|
### Parcourir une liste de dictionnaires
|
||
|
|
```python
|
||
|
|
for chanson in catalogue:
|
||
|
|
print(chanson["titre"])
|
||
|
|
```
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## Exercice 1
|
||
|
|
|
||
|
|
### 1.1 - Affichage formate
|
||
|
|
- Utilisez print() avec des virgules : `print("Titre:", titre)`
|
||
|
|
- Pour le formatage minutes:secondes, pensez a `//` et `%`
|
||
|
|
- Concatenation avec `+` si besoin : `str(minutes) + ":" + str(secondes)`
|
||
|
|
|
||
|
|
### 1.2 - Statistiques
|
||
|
|
- Initialisez un dictionnaire vide pour le resultat
|
||
|
|
- Parcourez la liste avec une boucle for pour calculer la somme
|
||
|
|
- `round(valeur)` arrondit a l'entier
|
||
|
|
|
||
|
|
```python
|
||
|
|
total = 0
|
||
|
|
for chanson in catalogue:
|
||
|
|
total = total + chanson["duree"]
|
||
|
|
```
|
||
|
|
|
||
|
|
### 1.3 - Artistes uniques
|
||
|
|
- Parcourez et ajoutez dans une liste si pas deja present
|
||
|
|
- `sorted(liste)` trie par ordre alphabetique
|
||
|
|
|
||
|
|
```python
|
||
|
|
artistes = []
|
||
|
|
for chanson in catalogue:
|
||
|
|
if chanson["artiste"] not in artistes:
|
||
|
|
artistes.append(chanson["artiste"])
|
||
|
|
artistes.sort()
|
||
|
|
```
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## Exercice 2
|
||
|
|
|
||
|
|
### 2.1 - Recherche
|
||
|
|
- Utilisez `if critere == "artiste":` pour differencier les cas
|
||
|
|
- Pour la recherche insensible a la casse : `"star".lower() in titre.lower()`
|
||
|
|
- Renvoyez une nouvelle liste (ne modifiez pas l'originale)
|
||
|
|
|
||
|
|
### 2.2 - Ajouter/Retirer
|
||
|
|
- Pour verifier si un titre existe deja :
|
||
|
|
```python
|
||
|
|
for chanson in playlist:
|
||
|
|
if chanson["titre"] == nouveau_titre:
|
||
|
|
return False # Deja present
|
||
|
|
```
|
||
|
|
|
||
|
|
- Pour retirer, parcourez avec l'indice :
|
||
|
|
```python
|
||
|
|
for i in range(len(playlist)):
|
||
|
|
if playlist[i]["titre"] == titre:
|
||
|
|
playlist.pop(i)
|
||
|
|
return True
|
||
|
|
```
|
||
|
|
|
||
|
|
### 2.3 - Top artistes
|
||
|
|
- Creez d'abord un dictionnaire `{artiste: total_streams}`
|
||
|
|
- Parcourez le catalogue et cumulez les streams
|
||
|
|
- Triez avec `sorted(dico.items(), key=lambda x: x[1], reverse=True)`
|
||
|
|
|
||
|
|
```python
|
||
|
|
compteur = {}
|
||
|
|
for chanson in catalogue:
|
||
|
|
artiste = chanson["artiste"]
|
||
|
|
if artiste not in compteur:
|
||
|
|
compteur[artiste] = 0
|
||
|
|
compteur[artiste] = compteur[artiste] + chanson["streams"]
|
||
|
|
```
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## Exercice 3 (Bonus)
|
||
|
|
|
||
|
|
### 3.1 - Filtrage
|
||
|
|
```python
|
||
|
|
# Filtrer les chansons courtes
|
||
|
|
resultat = []
|
||
|
|
for chanson in catalogue:
|
||
|
|
if chanson["duree"] < 200:
|
||
|
|
resultat.append(chanson["titre"])
|
||
|
|
```
|
||
|
|
|
||
|
|
### 3.2 - Recommandations
|
||
|
|
1. D'abord, recuperez les artistes de la playlist utilisateur
|
||
|
|
2. Ensuite, filtrez le catalogue pour ces artistes
|
||
|
|
3. Excluez les chansons deja dans la playlist
|
||
|
|
4. Triez par streams decroissant
|
||
|
|
|
||
|
|
### 3.3 - Playlist automatique
|
||
|
|
- Triez d'abord le catalogue par streams decroissant
|
||
|
|
- Parcourez et ajoutez si `duree_actuelle + chanson["duree"] <= duree_cible + tolerance`
|
||
|
|
- Arretez quand `duree_actuelle >= duree_cible - tolerance`
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## Pieges a eviter
|
||
|
|
|
||
|
|
1. **Ne pas modifier la liste originale** lors des recherches
|
||
|
|
2. **Attention aux comparaisons de chaines** : utilisez `.lower()` pour ignorer la casse
|
||
|
|
3. **Division par zero** : verifiez que la liste n'est pas vide avant de calculer une moyenne
|
||
|
|
4. **Arrondi** : `round(x, 2)` pour 2 decimales, `round(x)` pour un entier
|