2022-2023/1NSI/10_Dictionnaire/1E_dictionnaire.ipynb
Bertrand Benjamin 90558ac36b
All checks were successful
continuous-integration/drone/push Build is passing
Feat(NSI): exercices sur les dictionnaires
2023-03-26 16:40:46 +02:00

623 lines
13 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "3a9c129c",
"metadata": {},
"source": [
"# Dictionnaire en Python\n",
"\n",
"## Construction d'un mémo sur les dictionnaires\n",
"\n",
"Cette partie à pour but de vous faire découvrir les dictionnaires. Vous devez vous constituer un mémo des commandes pour les manipuler.\n",
"\n",
"On a déjà vu que l'on pouvait stocker plusieurs informations dans une `list` ou un `tuple`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d76bb1c8",
"metadata": {},
"outputs": [],
"source": [
"info_perso = (\"Bob\", \n",
" \"Lacanaille\",\n",
" 2, \n",
" \"0637382716\",\n",
" \"1 rue de la boutifaille\",\n",
" \"01000\",\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "f801055d",
"metadata": {},
"source": [
"Pour accéder au nom de cette personne, on peut alors faire"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8521c91b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Lacanaille'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"info_perso[1]"
]
},
{
"cell_type": "markdown",
"id": "80409f49",
"metadata": {},
"source": [
"Ça marche mais ce n'est pas très explicite. Ce n'est pas très clair quelle élément est le nom, le prénom, si le 2 est l'age ou autre chose.\n",
"\n",
"Il existe une stucture de données appellée `tuple nommé` dans laquelle les clés ne sont pas des nombres (0 à 5 ici) mais des chaines de caractères ou d'autres types. En python, ces tuples nommés sont appellé `dictionnaires`.\n",
"\n",
"1. Compléter le dictionnaire ci-dessous pour ajouter les informations sur Bob Lacanaille."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ab63d16c",
"metadata": {},
"outputs": [],
"source": [
"info_perso = {\n",
" \"prenom\": \"Bob\", \n",
" \"nom\": \"Lacanaille\",\n",
" \"age\": 2,\n",
" \n",
"}"
]
},
{
"cell_type": "markdown",
"id": "043de027",
"metadata": {},
"source": [
"`info_perso` est bien un dictionnaire"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "970c028a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(info_perso)"
]
},
{
"cell_type": "markdown",
"id": "de3f48c3",
"metadata": {},
"source": [
"Pour accéder aux valeurs, il faut maintenant indiqué la clé de l'information recherchée"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "00b292e8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Lacanaille'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"info_perso[\"nom\"]"
]
},
{
"cell_type": "markdown",
"id": "88acf0ee",
"metadata": {},
"source": [
"2. Quelles sont les clés du dictionnaire `info_perso`?"
]
},
{
"cell_type": "raw",
"id": "c29b2e1a",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "d310f5ba",
"metadata": {},
"source": [
"3. Comment accéder à l'age? Au code postal."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9799858",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "6f975598",
"metadata": {},
"source": [
"Les dictionnaires sont dits `mutables` c'est à dire que l'on peut les modifier.\n",
"\n",
"On peut modifier les valeurs associées aux clés"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a40058b0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'prenom': 'Bob', 'nom': 'Lessage', 'age': 10}\n"
]
}
],
"source": [
"info_perso[\"nom\"] = \"Lessage\"\n",
"info_perso[\"age\"] = 10\n",
"print(info_perso)"
]
},
{
"cell_type": "markdown",
"id": "037e117b",
"metadata": {},
"source": [
"Ou ajouter des nouvelles clés"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "694daacb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'prenom': 'Bob', 'nom': 'Lessage', 'age': 10, 'yeux': 'bleu', 'lieu de naissance': 'Ile perdue'}\n"
]
}
],
"source": [
"info_perso[\"yeux\"] = \"bleu\"\n",
"info_perso[\"lieu de naissance\"] = \"Ile perdue\"\n",
"print(info_perso)"
]
},
{
"cell_type": "markdown",
"id": "ad8d985c",
"metadata": {},
"source": [
"4. Construire un dictionnaire sur le contenu d'un panier que l'on stockera dans la variable `panier`.\n",
"\n",
"| Contenu | Quantité |\n",
"|-----------|----------|\n",
"| Poires | 20 |\n",
"| Pommes | 10 |\n",
"| Orange | 12 |\n",
"| Mots doux | 2 |\n",
"| Gateaux | 0 |"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "efd7013c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "fe33e112",
"metadata": {},
"source": [
"5. Ajouter dans ce panier 5 champignons et 2 épis de maïs."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76f4d571",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "4407a25c",
"metadata": {},
"source": [
"`len` permet d'avoir le nombre d'éléments dans un dictionnaire.\n",
"\n",
"6. Calculer la longueur des dictionnaires `info_perso` et `panier`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5148ffb2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "e7bf0d24",
"metadata": {},
"source": [
"Il y a plusieurs façon de faire des boucles sur les dictionnaires"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "47568544",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"----\n",
"prenom\n",
"Bob\n",
"----\n",
"nom\n",
"Lessage\n",
"----\n",
"age\n",
"10\n",
"----\n",
"yeux\n",
"bleu\n",
"----\n",
"lieu de naissance\n",
"Ile perdue\n"
]
}
],
"source": [
"for cle in info_perso:\n",
" print(\"----\")\n",
" print(cle)\n",
" print(info_perso[cle])"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "5df06b20",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"----\n",
"prenom\n",
"Bob\n",
"----\n",
"nom\n",
"Lessage\n",
"----\n",
"age\n",
"10\n",
"----\n",
"yeux\n",
"bleu\n",
"----\n",
"lieu de naissance\n",
"Ile perdue\n"
]
}
],
"source": [
"for cle, valeur in info_perso.items():\n",
" print(\"----\")\n",
" print(cle)\n",
" print(valeur)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "7be1fbef",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bob\n",
"Lessage\n",
"10\n",
"bleu\n",
"Ile perdue\n"
]
}
],
"source": [
"for valeur in info_perso.values():\n",
" print(valeur)"
]
},
{
"cell_type": "markdown",
"id": "2bf475ba",
"metadata": {},
"source": [
"7. Faire une boucle sur `panier` pour afficher un texte du type\n",
"\n",
" Dans mon panier, il y 4 carrotes\n",
" \n",
"pour tout ce qui se trouve dans le panier."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0895e2e9",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "6cb53a9f",
"metadata": {},
"source": [
"Voici les prix à l'unité des aliments qui ont été mis dans le panier.\n",
"\n",
"| Contenu | Prix |\n",
"|-----------|------|\n",
"| Poires | 2 |\n",
"| Pommes | 1 |\n",
"| Orange | 5 |\n",
"| Mots doux | 0 |\n",
"| Gateaux | 10 |\n",
"| Champignons | 1 |\n",
"| Mais | 15 |\n",
"\n",
"8. Mettre les prix dans un dictionnaire puis calculer le prix de mon panier."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d2c3b52",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "6f0a1e25",
"metadata": {},
"source": [
"## Temps d'accès à un élément\n",
"\n",
"Dans cette partie, on compare le temps d'accès à un élément entre une `liste` et un `dictionnaire`."
]
},
{
"cell_type": "markdown",
"id": "2edee797",
"metadata": {},
"source": [
"1. On construit 5 dictionnaires avec 10, 100, 1000, 10000 et 100000 clés"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "5d889ef3",
"metadata": {},
"outputs": [],
"source": [
"tailles = [10, 100, 1000, 10000, 100000]\n",
"dictionnaires = {}\n",
"for taille in tailles:\n",
" dictionnaires[taille] = {i: i for i in range(taille)}"
]
},
{
"cell_type": "markdown",
"id": "755895e1",
"metadata": {},
"source": [
"2. On va calculer le temps pour accéder à un élément aléatoirement dans chaque liste."
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "98870503",
"metadata": {},
"outputs": [],
"source": [
"from time import time\n",
"from random import randint"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "8cd70e84",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[9.059906005859375e-07, 7.62939453125e-07, 1.1920928955078125e-06, 1.3828277587890625e-06, 1.52587890625e-06]\n"
]
}
],
"source": [
"moyennes = []\n",
"nb_essais = 5\n",
"for taille in tailles:\n",
" le_dictionnaire = dictionnaires[taille]\n",
" temps = []\n",
" for i in range(nb_essais):\n",
" pos = randint(0, taille-1)\n",
" start = time()\n",
" le_dictionnaire[pos]\n",
" end = time()\n",
" temps.append(end-start)\n",
" moyennes.append(sum(temps)/nb_essais)\n",
"print(moyennes)"
]
},
{
"cell_type": "markdown",
"id": "be8d6144",
"metadata": {},
"source": [
"3. Que peut-on dire sur le temps d'accès à un élément d'un dictionnaire?"
]
},
{
"cell_type": "raw",
"id": "7c3b0bac",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "79d9d967",
"metadata": {},
"source": [
"4. Faire la même chose pour des listes de taille 10 à 10000 éléments"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "685baf07",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ba70d29f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "748a5a0a",
"metadata": {},
"source": [
"5. Que peut-on dire sur le temps d'accès à un élément d'une liste"
]
},
{
"cell_type": "raw",
"id": "4356fba7",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "5c7b15dc",
"metadata": {},
"source": [
"## Jeu du scabble\n",
"\n",
"Dans cette partie, vous allez programmer des fonctions necessaires pour calculer les points au jeu du scrabble.\n",
"\n",
"Pour cela, vous devez compléter le fichier `scrabble.py`."
]
},
{
"cell_type": "markdown",
"id": "56b04766",
"metadata": {},
"source": [
"## Casser le chiffrement de César\n",
"\n",
"Dans un précédent exercice, vous avez programmer des fonctions qui permettait de chiffrer avec la méthode de César (décalage de l'alphabet).\n",
"\n",
"En partant du principe que dans un texte le caractère le plus courant est l'espace, vous aller pouvoir casser ce chiffrement.\n",
"\n",
"Pour cela, vous devez compléter le fichier `cesar.py`"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}