Skip to content
Snippets Groups Projects
Commit 900478fb authored by Helene Coullon's avatar Helene Coullon
Browse files

corrections TP4

parent f7e0812b
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Map reduce en Python
%% Cell type:markdown id: tags:
## Exercice
%% Cell type:markdown id: tags:
Ecrire un programme qui calcule la distance Euclidienne totale entre une série de points en 2D connectés séquentiellement l'un après l'autre.
On vous donne une liste de points en 2D, chaque point étant représenté par un tuple (x, y).
- Utilisez map pour calculer la distance entre chaque paire de points consécutifs.
- Utilisez ensuite reduce pour calculer la distance totale du trajet reliant tous les points.
```python
points = [(0, 0), (3, 4), (7, 1), (10, 10)]
```
%% Cell type:code id: tags:
``` python
from functools import reduce
import math
# Liste de points
points = [(0, 0), (3, 4), (7, 1), (10, 10)]
# Fonction pour calculer la distance entre deux points
def distance(point1, point2):
return math.sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2)
# Utilisation de `map` pour calculer les distances entre points consécutifs
distances = list(map(lambda i: distance(points[i], points[i + 1]), range(len(points) - 1)))
# Utilisation de `reduce` pour calculer la distance totale
total_distance = reduce(lambda x, y: x + y, distances)
# Affichage du résultat
print("Distance totale du trajet :", total_distance)
```
%% Output
Running cells with 'Python 3.12.3' requires the ipykernel package.
Run the following command to install 'ipykernel' into the Python environment.
Command: '/bin/python3 -m pip install ipykernel -U --user --force-reinstall'
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment