Feat(NSI): fin du notebook sur la complexité
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
76b313324e
commit
e3ffe1e626
@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3dbe9854",
|
||||
"id": "6e073068",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Temps d'exécution et complexité\n",
|
||||
@ -17,7 +17,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "68971c87",
|
||||
"id": "3a77b4e6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -27,7 +27,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "35634668",
|
||||
"id": "50901256",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -39,14 +39,14 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ab9adcbe",
|
||||
"id": "bf3ab537",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bd03de42",
|
||||
"id": "40a8b167",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"On voudra tester votre fonction sur des grandes listes de nombre construites aléatoirements avec le module `random`."
|
||||
@ -55,7 +55,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "bdda9aa6",
|
||||
"id": "6e6e62eb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -66,7 +66,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e6a937aa",
|
||||
"id": "6e64bfac",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -75,7 +75,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2935612a",
|
||||
"id": "28f0fdac",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Le résultat est un nombre aléatoire entre 1 et 10.\n",
|
||||
@ -86,7 +86,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d9d32862",
|
||||
"id": "58200552",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -96,7 +96,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5d4e98c8",
|
||||
"id": "fc9b3bf3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"2. Vérifier que votre fonction `chercher` fonctionne avec des listes aléatoires à 10, 100, 1000 et 1000 nombres entre 0 et 100."
|
||||
@ -105,7 +105,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ebf38f09",
|
||||
"id": "ebf2bbc5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -115,7 +115,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3a57efc6",
|
||||
"id": "1b3b40c9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -125,7 +125,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6279eb6d",
|
||||
"id": "1c5b634f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -135,7 +135,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dc1c177d",
|
||||
"id": "e2ad8221",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -144,7 +144,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fad0261b",
|
||||
"id": "26a61e11",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"On cherche maintenant à évaluer la rapidité d'exécution de votre fonction. Pour cela on utilisera le module `time` qui permet d'accéder au temps."
|
||||
@ -153,7 +153,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "08421e3c",
|
||||
"id": "bb9f1569",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -164,7 +164,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "bf3e19cd",
|
||||
"id": "6502ae21",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -173,7 +173,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "92829a6b",
|
||||
"id": "8d97d48f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"le résultat est le nombre de secondes écoulée depuis le 1 janvier 1970.\n",
|
||||
@ -186,7 +186,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "278f29e6",
|
||||
"id": "eab8639d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -197,7 +197,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c1d58926",
|
||||
"id": "a053fc3b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"4. Combien de temps met votre fonction pour chercher un nombre dans une liste de 10 nombres? 100 nombres? 1000nombres? 10 000 nombres?"
|
||||
@ -206,7 +206,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c05e0ee0",
|
||||
"id": "17f17db3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -216,7 +216,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "75ff703a",
|
||||
"id": "98444ddb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -226,7 +226,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e8f6ebd2",
|
||||
"id": "be9d12d2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -236,7 +236,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d19b52db",
|
||||
"id": "b9621131",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -245,7 +245,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e87c612a",
|
||||
"id": "57840abf",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"5. Le soucis de cette méthode est que l'on est pas à l'abris d'une liste \"facile\". Vous devez faire la même chose en calculant 5 fois le temps pour 5 listes différentes puis faire la moyenne des temps d'exécution. *(je vous encourage à programmer une fonction `moyenne` pour calculer cette moyenne)*"
|
||||
@ -254,7 +254,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "26d66477",
|
||||
"id": "b67b8fea",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
@ -262,7 +262,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "66448c0a",
|
||||
"id": "531e00f3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -272,7 +272,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a549e4e3",
|
||||
"id": "16502cac",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -282,7 +282,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "30bf2e23",
|
||||
"id": "e2ea7281",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -292,7 +292,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "994b1e70",
|
||||
"id": "370f6d59",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -301,7 +301,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "07144eae",
|
||||
"id": "6c13e06b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"On va maintenant tracer le temps d'exécution en fonction de la taille de la liste.\n",
|
||||
@ -312,7 +312,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c1834454",
|
||||
"id": "10e8f28a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -322,7 +322,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f2882fd2",
|
||||
"id": "4318058e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -337,7 +337,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "67489459",
|
||||
"id": "7818c04e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"6. Tracer le graphique des temps d'exécution de votre fonction `chercher`. "
|
||||
@ -346,14 +346,14 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5d1f57ba",
|
||||
"id": "a312000a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "976edf11",
|
||||
"id": "846b0e02",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"7. Comparer les temps "
|
||||
@ -362,37 +362,38 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f3c72341",
|
||||
"id": "0bc4af7b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "899bea56",
|
||||
"id": "4e3e4ace",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Fonction max\n",
|
||||
"\n",
|
||||
"On souhaite étudier le temps d'exécution d'une fonction `maximum`.\n",
|
||||
"\n",
|
||||
"1. Progammer et tester la fonction `maximum` qui prend en argument une liste de nombre et qui renvoie le plus grand élément de la liste."
|
||||
"1. Progammer et tester la fonction `maximum` qui prend en argument une liste de liste de nombres et qui renvoie le plus grand élément parmi toutes les listes."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a79511a2",
|
||||
"id": "0e5424b5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Tests"
|
||||
"# Tests\n",
|
||||
"assert maximum([[1, 2, 3], [7, 3, 1]]) == 7"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1cb5630c",
|
||||
"id": "a0562579",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -401,7 +402,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d8e43d4f",
|
||||
"id": "fee2f5da",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"2. Mesurer le temps d'exécution de votre fonction sur des listes avec 10, 100, 1000, 10000 et 100000 nombres."
|
||||
@ -410,7 +411,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "fec2675f",
|
||||
"id": "9004c51c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
@ -418,7 +419,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a5fb9697",
|
||||
"id": "3d16cef9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
@ -426,7 +427,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "462ee355",
|
||||
"id": "f1914122",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
@ -434,7 +435,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a1ca0ba6",
|
||||
"id": "91d57e06",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
@ -442,14 +443,14 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8abf0381",
|
||||
"id": "b83b39b1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "eaaff40e",
|
||||
"id": "4e4324ba",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"3. Reprendre la question précédente en faisant la moyenne des temps d'exécution sur 5 listes pour chaque taille."
|
||||
@ -458,7 +459,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4dc8d464",
|
||||
"id": "30d640cd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
@ -466,7 +467,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1b775dbe",
|
||||
"id": "bb771796",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
@ -474,7 +475,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "49807880",
|
||||
"id": "3a36f920",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
@ -482,7 +483,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "43933ce7",
|
||||
"id": "79e54764",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
@ -490,14 +491,14 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "321d4456",
|
||||
"id": "530204ee",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "27a4391a",
|
||||
"id": "ef7c19a2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"4. Tracer le graphique de la moyenne de temps d'exécution en fonction de la taille des listes."
|
||||
@ -506,27 +507,241 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a0d4f074",
|
||||
"id": "fff676a4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ce6c0a6e",
|
||||
"id": "deb97308",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Recherche par dichotomie.\n",
|
||||
"\n",
|
||||
"La recherche par dichotomie permet de grandement améliorer la vitesse de recherche d'un élément dans un liste. **À condition que cette liste soit triée en ordre croissant**.\n",
|
||||
"\n",
|
||||
"1. Chercher la meilleur stratégie pour gagner "
|
||||
"1. Chercher et expliquer la meilleur stratégie pour gagner [au jeu du plus ou moins](https://raw.opytex.org/1NSI/09_Recherche_par_dichotomie_et_complexite/plus_moins/plus_moins.html)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "c78246e0",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "65cf15cd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"La résolution du jeux précédent est très similaire à la situation de recherche par dichotomie.\n",
|
||||
"\n",
|
||||
"2. Pour vous entrainé à la recherche par dichotomie, établir un algorithme qui permet de trouver en le moins de coup possible (en moyenne) un élément dans une liste triée. Pour tester votre stratégie vous pouvez utiliser [ce jeu]()."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a8b722a9",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "73ccf876",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"3. On suppose maintenant que la liste `L = [x0, x1, . . . , x]` est triée en ordre croissant, c'est\n",
|
||||
"à dire :\n",
|
||||
" \n",
|
||||
" x0 ⩽ x1 ⩽ · · · ⩽ xn\n",
|
||||
" \n",
|
||||
"On cherche parmi les éléments de `L` l'élément x.\n",
|
||||
" \n",
|
||||
"On désigne par `L[i..j]` les éléments de L dont les indices sont compris entre i (inclu) et j\n",
|
||||
"(inclu). \n",
|
||||
"\n",
|
||||
"On suppose que parmi les éléments de `L[g..d]`, il y a un élément x. Et on considère un\n",
|
||||
"indice m tel que g ⩽ m ⩽ d.\n",
|
||||
"\n",
|
||||
"Si L[m]<x, dans quelle nouvelle zone est-on sûr que x se trouve ?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "bf3961fa",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "645c4300",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Si L[m]>x, dans quelle nouvelle zone est-on sûr que x se trouve ?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "fb42b270",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7c6f53e5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Que peut-on affirmer si aucune des deux conditions précédentes n'est valide ?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "d8cf7292",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6d046b52",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"4. Compléter le fonction suivante en respectant la spécification de la docstring"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "0afedcb2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def dichotomie(L, x):\n",
|
||||
" \"\"\" Renvoie un indice de l'élément x dans L si x est dedans, et renvoie None sinon\n",
|
||||
" \n",
|
||||
" On supposera que la liste L est triée en ordre croissant.\n",
|
||||
" \"\"\"\n",
|
||||
" g = 0\n",
|
||||
" d = len(L)-1\n",
|
||||
" while g <= l:\n",
|
||||
" m = (g + d) // 2\n",
|
||||
" if L[m] < x:\n",
|
||||
" ...\n",
|
||||
" elif L[m] > x:\n",
|
||||
" ...\n",
|
||||
" else:\n",
|
||||
" ...\n",
|
||||
" return None"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5e3bde1e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"5. Mesurer le temps d'exécution de votre fonction sur des listes avec 10, 100, 1000, 10000 et 100000 nombres."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ebb87f94",
|
||||
"id": "603757b0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6959bcfb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9a834ec6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f3e9a104",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6093b13a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "db5d9f35",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"6. Reprendre la question précédente en faisant la moyenne des temps d'exécution sur 5 listes pour chaque taille."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "906ce312",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "69213d32",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "51e69361",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d8d068cb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b3628a5f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a6966564",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"7. Tracer le graphique de la moyenne de temps d'exécution en fonction de la taille des listes."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "96687215",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
|
@ -0,0 +1,96 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Recherche par dichotomie</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Jeu de recherche</h1>
|
||||
<section id="setup">
|
||||
|
||||
<p> Je crée une liste de
|
||||
<input type="number" name="quantite" value="100", id="quantite">
|
||||
nombres aléatoires compris entre 1 et
|
||||
<input type="number" name="max" value="100", id="max">
|
||||
triés en ordre croissant.
|
||||
</p>
|
||||
<p>Vous devez trouver la position d'un nombre de cette liste en le moins de coup possibles.</p>
|
||||
<button id="start-game" type="button" onclick="startGame()"> Lancer la partie </button>
|
||||
</section>
|
||||
|
||||
<section id="game">
|
||||
</section>
|
||||
|
||||
<script>
|
||||
// Génère un nombre aléatoire entre 1 et 100
|
||||
let randomNumber = 0;
|
||||
let numbers;
|
||||
let index;
|
||||
let tries;
|
||||
|
||||
function generateNormalDistribution(mean, stdDev) {
|
||||
let u = 0, v = 0;
|
||||
while (u === 0) u = Math.random(); // En évitant 0
|
||||
while (v === 0) v = Math.random();
|
||||
const z1 = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
|
||||
return Math.floor(mean + z1 * stdDev);
|
||||
}
|
||||
|
||||
function startGame() {
|
||||
const maximum = parseInt(document.getElementById("max").value)
|
||||
const length = parseInt(document.getElementById("quantite").value)
|
||||
tries = 0;
|
||||
|
||||
const mean = Math.floor(Math.random() * maximum);
|
||||
const stdDev = 10;
|
||||
numbers = Array.from({length: length}, () => {
|
||||
let x;
|
||||
do {
|
||||
x = generateNormalDistribution(mean, stdDev);
|
||||
} while (x < 1 || x > maximum);
|
||||
return x;
|
||||
});
|
||||
numbers.sort((a, b) => a - b);
|
||||
|
||||
index = Math.floor(Math.random() * length);
|
||||
const aTrouver = numbers[index]
|
||||
|
||||
document.getElementById("game").innerHTML = `<h2> Trouve la position du nombre ${aTrouver} dans une liste de ${length} nombres entre 1 et ${maximum}</h2>`+
|
||||
"<label for='guess'> Vous pensez qu'il est en quelle position? </label></br>" +
|
||||
"<input type='text' id='guess'></br>" +
|
||||
`<button id="subbutton" type='button' onclick='guessNumber()'>Je tente !</button>` +
|
||||
"<div id='result'></div>"
|
||||
document.getElementById("start-game").innerHTML = "Réinitialiser la partie"
|
||||
}
|
||||
|
||||
function guessNumber() {
|
||||
tries++
|
||||
document.getElementById("subbutton").innerHTML = `Je tente (déja ${tries} essais)!`
|
||||
// Récupère la valeur saisie par l'utilisateur
|
||||
const guess = parseInt(document.getElementById("guess").value);
|
||||
const length = parseInt(document.getElementById("quantite").value)
|
||||
|
||||
// Vérifie si la valeur est un nombre
|
||||
if (isNaN(guess)) {
|
||||
document.getElementById("result").innerHTML = "<p>Veuillez saisir un nombre valide.</p>";
|
||||
return;
|
||||
}
|
||||
|
||||
// Vérifie si la valeur est comprise entre 1 et 100
|
||||
if (guess < 1 || guess > length) {
|
||||
document.getElementById("result").innerHTML = "<p>Le nombre doit être compris entre 1 et " + length + "</p>";
|
||||
return;
|
||||
}
|
||||
|
||||
// Compare la valeur saisie avec le nombre aléatoire
|
||||
if (guess < index) {
|
||||
document.getElementById("result").innerHTML = "<p>Il est après!</p>";
|
||||
} else if (guess > index) {
|
||||
document.getElementById("result").innerHTML = "<p>Il est avant!</p>";
|
||||
} else {
|
||||
document.getElementById("result").innerHTML = `<p>Bravo, vous avez trouvé le nombre en ${tries}!</p>`;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -1,61 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Jeu du plus ou moins</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Jeu du plus ou moins</h1>
|
||||
<section id="setup">
|
||||
|
||||
<p>Devinez un nombre entre 0 et
|
||||
<input type="number" name="max" value="100", id="max">
|
||||
</p>
|
||||
<button id="start-game" type="button" onclick="startGame()"> Lancer la partie </button>
|
||||
</section>
|
||||
<section id="game">
|
||||
</section>
|
||||
|
||||
<script>
|
||||
// Génère un nombre aléatoire entre 1 et 100
|
||||
let randomNumber = 0;
|
||||
|
||||
function startGame() {
|
||||
const maximum = parseInt(document.getElementById("max").value)
|
||||
randomNumber = Math.floor(Math.random() * maximum) + 1;
|
||||
document.getElementById("game").innerHTML = "<h2> Trouve un nombre entre 0 et " + maximum + "</h2>"+
|
||||
"<input type='text' id='guess'>" +
|
||||
"<button type='button' onclick='guessNumber()'>Guess</button>" +
|
||||
"<p id='result'></p>"
|
||||
document.getElementById("start-game").innerHTML = "Réinitialiser la partie"
|
||||
}
|
||||
|
||||
function guessNumber() {
|
||||
// Récupère la valeur saisie par l'utilisateur
|
||||
const guess = parseInt(document.getElementById("guess").value);
|
||||
const maximum = parseInt(document.getElementById("max").value)
|
||||
|
||||
// Vérifie si la valeur est un nombre
|
||||
if (isNaN(guess)) {
|
||||
document.getElementById("result").innerHTML = "Veuillez saisir un nombre valide.";
|
||||
return;
|
||||
}
|
||||
|
||||
// Vérifie si la valeur est comprise entre 1 et 100
|
||||
if (guess < 1 || guess > maximum) {
|
||||
document.getElementById("result").innerHTML = "Le nombre doit être compris entre 1 et " + maximum;
|
||||
return;
|
||||
}
|
||||
|
||||
// Compare la valeur saisie avec le nombre aléatoire
|
||||
if (guess < randomNumber) {
|
||||
document.getElementById("result").innerHTML = "Plus grand.";
|
||||
} else if (guess > randomNumber) {
|
||||
document.getElementById("result").innerHTML = "Plus petit.";
|
||||
} else {
|
||||
document.getElementById("result").innerHTML = "Bravo, vous avez trouvé le nombre !";
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -1,46 +0,0 @@
|
||||
const popup = document.getElementById('popup');
|
||||
|
||||
|
||||
function closeSection1 () {
|
||||
let section = document.getElementById('section1');
|
||||
section.style.display = "none";
|
||||
popup.classList.remove('hidden');
|
||||
|
||||
}
|
||||
document.getElementById("close-section1").addEventListener('click', closeSection1)
|
||||
|
||||
function closeSection2 () {
|
||||
let section = document.getElementById('section2');
|
||||
popup.classList.remove('hidden');
|
||||
|
||||
let p = document.createElement('p')
|
||||
p.innerHTML = "Je disparais pas!"
|
||||
section.appendChild(p)
|
||||
}
|
||||
|
||||
document.getElementById("close-section2").addEventListener('click', closeSection2)
|
||||
|
||||
let compteur = 0
|
||||
function closeSection3 () {
|
||||
let popup = document.getElementById('popup');
|
||||
let section = document.getElementById('section3');
|
||||
if (compteur > 2) {
|
||||
section.style.display = "none";
|
||||
popup.classList.remove('hidden');
|
||||
} else {
|
||||
compteur = compteur + 1;
|
||||
popup.classList.remove('hidden');
|
||||
let p = document.createElement('p')
|
||||
p.innerHTML = "Restez! Je vous fait une promo de 10%!"
|
||||
section.appendChild(p)
|
||||
}
|
||||
|
||||
}
|
||||
document.getElementById("close-section3").addEventListener('click', closeSection3)
|
||||
|
||||
|
||||
const closePopupLink = document.getElementById('close-popup');
|
||||
closePopupLink.addEventListener('click', (event) => {
|
||||
event.preventDefault();
|
||||
popup.classList.add('hidden');
|
||||
});
|
@ -1,11 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Jeu du plus ou moins</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Jeu du plus ou moins</h1>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Jeu du plus ou moins</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Jeu du plus ou moins</h1>
|
||||
<section id="setup">
|
||||
|
||||
<p>Devinez un nombre entre 0 et
|
||||
@ -16,46 +16,51 @@
|
||||
<section id="game">
|
||||
</section>
|
||||
|
||||
<script>
|
||||
// Génère un nombre aléatoire entre 1 et 100
|
||||
let randomNumber = 0;
|
||||
<script>
|
||||
// Génère un nombre aléatoire entre 1 et 100
|
||||
let randomNumber = 0;
|
||||
let tries = 0;
|
||||
|
||||
function startGame() {
|
||||
const maximum = parseInt(document.getElementById("max").value)
|
||||
randomNumber = Math.floor(Math.random() * maximum) + 1;
|
||||
document.getElementById("game").innerHTML = "<h2> Trouve un nombre entre 0 et " + maximum + "</h2>"+
|
||||
"<input type='text' id='guess'>" +
|
||||
"<button type='button' onclick='guessNumber()'>Guess</button>" +
|
||||
"<p id='result'></p>"
|
||||
document.getElementById("start-game").innerHTML = "Réinitialiser la partie"
|
||||
}
|
||||
function startGame() {
|
||||
const maximum = parseInt(document.getElementById("max").value)
|
||||
randomNumber = Math.floor(Math.random() * maximum) + 1;
|
||||
tries = 0
|
||||
document.getElementById("game").innerHTML = "<h2> Trouve un nombre entre 0 et " + maximum + "</h2>"+
|
||||
"<input type='text' id='guess'>" +
|
||||
`<button id="subbutton" type='button' onclick='guessNumber()'>Je tente !</button>` +
|
||||
"<p id='result'></p>"
|
||||
document.getElementById("start-game").innerHTML = "Réinitialiser la partie"
|
||||
}
|
||||
|
||||
function guessNumber() {
|
||||
// Récupère la valeur saisie par l'utilisateur
|
||||
const guess = parseInt(document.getElementById("guess").value);
|
||||
const maximum = parseInt(document.getElementById("max").value)
|
||||
function guessNumber() {
|
||||
// Récupère la valeur saisie par l'utilisateur
|
||||
const guess = parseInt(document.getElementById("guess").value);
|
||||
const maximum = parseInt(document.getElementById("max").value)
|
||||
|
||||
// Vérifie si la valeur est un nombre
|
||||
if (isNaN(guess)) {
|
||||
document.getElementById("result").innerHTML = "Veuillez saisir un nombre valide.";
|
||||
return;
|
||||
}
|
||||
tries++
|
||||
document.getElementById("subbutton").innerHTML = `Je tente (déja ${tries} essais)!`
|
||||
|
||||
// Vérifie si la valeur est comprise entre 1 et 100
|
||||
if (guess < 1 || guess > maximum) {
|
||||
document.getElementById("result").innerHTML = "Le nombre doit être compris entre 1 et " + maximum;
|
||||
return;
|
||||
}
|
||||
// Vérifie si la valeur est un nombre
|
||||
if (isNaN(guess)) {
|
||||
document.getElementById("result").innerHTML = "Veuillez saisir un nombre valide.";
|
||||
return;
|
||||
}
|
||||
|
||||
// Compare la valeur saisie avec le nombre aléatoire
|
||||
if (guess < randomNumber) {
|
||||
document.getElementById("result").innerHTML = "Plus grand.";
|
||||
} else if (guess > randomNumber) {
|
||||
document.getElementById("result").innerHTML = "Plus petit.";
|
||||
} else {
|
||||
document.getElementById("result").innerHTML = "Bravo, vous avez trouvé le nombre !";
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
// Vérifie si la valeur est comprise entre 1 et 100
|
||||
if (guess < 1 || guess > maximum) {
|
||||
document.getElementById("result").innerHTML = "Le nombre doit être compris entre 1 et " + maximum;
|
||||
return;
|
||||
}
|
||||
|
||||
// Compare la valeur saisie avec le nombre aléatoire
|
||||
if (guess < randomNumber) {
|
||||
document.getElementById("result").innerHTML = "Plus grand.";
|
||||
} else if (guess > randomNumber) {
|
||||
document.getElementById("result").innerHTML = "Plus petit.";
|
||||
} else {
|
||||
document.getElementById("result").innerHTML = "Bravo, vous avez trouvé le nombre en "+ tries + " essais.";
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user