Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tp-bigdata
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LOGIN-BIGDATA
tp-bigdata
Commits
900478fb
Commit
900478fb
authored
2 months ago
by
Helene Coullon
Browse files
Options
Downloads
Patches
Plain Diff
corrections TP4
parent
f7e0812b
No related branches found
No related tags found
No related merge requests found
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tp4-5-6/tp4/corrige-tp-python.ipynb
+92
-0
92 additions, 0 deletions
tp4-5-6/tp4/corrige-tp-python.ipynb
tp4-5-6/tp4/corrige-tp-spark.ipynb
+1055
-0
1055 additions, 0 deletions
tp4-5-6/tp4/corrige-tp-spark.ipynb
with
1147 additions
and
0 deletions
tp4-5-6/tp4/corrige-tp-python.ipynb
0 → 100644
+
92
−
0
View file @
900478fb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Map reduce en Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercice"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n",
"\n",
"On vous donne une liste de points en 2D, chaque point étant représenté par un tuple (x, y).\n",
"\n",
"- Utilisez map pour calculer la distance entre chaque paire de points consécutifs.\n",
"- Utilisez ensuite reduce pour calculer la distance totale du trajet reliant tous les points.\n",
"\n",
"```python\n",
"points = [(0, 0), (3, 4), (7, 1), (10, 10)]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mRunning cells with 'Python 3.12.3' requires the ipykernel package.\n",
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
"\u001b[1;31mCommand: '/bin/python3 -m pip install ipykernel -U --user --force-reinstall'"
]
}
],
"source": [
"from functools import reduce\n",
"import math\n",
"\n",
"# Liste de points\n",
"points = [(0, 0), (3, 4), (7, 1), (10, 10)]\n",
"\n",
"# Fonction pour calculer la distance entre deux points\n",
"def distance(point1, point2):\n",
" return math.sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2)\n",
"\n",
"# Utilisation de `map` pour calculer les distances entre points consécutifs\n",
"distances = list(map(lambda i: distance(points[i], points[i + 1]), range(len(points) - 1)))\n",
"\n",
"# Utilisation de `reduce` pour calculer la distance totale\n",
"total_distance = reduce(lambda x, y: x + y, distances)\n",
"\n",
"# Affichage du résultat\n",
"print(\"Distance totale du trajet :\", total_distance)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% 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.
Click to expand it.
tp4-5-6/tp4/corrige-tp-spark.ipynb
0 → 100644
+
1055
−
0
View file @
900478fb
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment