Feat(NSI): début du chapitre sur la recherche par dichotomie
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
a47ec0b88a
commit
76b313324e
|
@ -0,0 +1,556 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3dbe9854",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Temps d'exécution et complexité\n",
|
||||
"\n",
|
||||
"Dans ce TP, vous allez chercher à évaluer la rapidité d'exécution de différents programmes.\n",
|
||||
"\n",
|
||||
"## Recherche d'élément\n",
|
||||
"\n",
|
||||
"1. Programmer et tester la fonction `chercher` qui prend en argument une liste de nombres et un nombre et qui renvoie si ce nombre est dans la liste"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "68971c87",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Tests"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "35634668",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Fonction\n",
|
||||
"def chercher(liste, nombre):\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ab9adcbe",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bd03de42",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"On voudra tester votre fonction sur des grandes listes de nombre construites aléatoirements avec le module `random`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "bdda9aa6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from random import randint\n",
|
||||
"# import de la fonction randint dans le module random"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e6a937aa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(randint(1, 10))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2935612a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Le résultat est un nombre aléatoire entre 1 et 10.\n",
|
||||
"\n",
|
||||
"Les listes seront construites de la manière suivante"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d9d32862",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"liste_aleatoire = [randint(0, 200) for i in range(1000)]\n",
|
||||
"print(liste_aleatoire)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5d4e98c8",
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ebf38f09",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 10 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3a57efc6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 100 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6279eb6d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 1000 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dc1c177d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 10000 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fad0261b",
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "08421e3c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from time import time\n",
|
||||
"# import de la fonction time dans le module time"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "bf3e19cd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(time())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "92829a6b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"le résultat est le nombre de secondes écoulée depuis le 1 janvier 1970.\n",
|
||||
"\n",
|
||||
"3. Pour mesurer le temps d'exécution, on enregistre le temps avant l'exécution de la fonction puis on faire la différente avec le temps après l'exécution.\n",
|
||||
"\n",
|
||||
"Combien de temps prend la commande `cherche([1, 7, 8, 3, 9, 3], 9)` à s'exécuter?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "278f29e6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"t = time()\n",
|
||||
"chercher([1, 7, 8, 3, 9, 3], 9)\n",
|
||||
"print(time() - t)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c1d58926",
|
||||
"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?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c05e0ee0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 10 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "75ff703a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 100 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e8f6ebd2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 1000 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d19b52db",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 10000 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e87c612a",
|
||||
"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)*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "26d66477",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "66448c0a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 10 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a549e4e3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 100 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "30bf2e23",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 1000 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "994b1e70",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 10000 nombres"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "07144eae",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"On va maintenant tracer le temps d'exécution en fonction de la taille de la liste.\n",
|
||||
"\n",
|
||||
"Pour cela, vous allez utiliser la librairy graphique `matplotlib`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c1834454",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f2882fd2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# taille des listes\n",
|
||||
"x = [10, 100, 1000, 10000]\n",
|
||||
"# Temps\n",
|
||||
"t = [1, 2, 3, 4]\n",
|
||||
"# le graphique\n",
|
||||
"fig, ax = plt.subplots()\n",
|
||||
"ax.plot(x, t)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "67489459",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"6. Tracer le graphique des temps d'exécution de votre fonction `chercher`. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5d1f57ba",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "976edf11",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"7. Comparer les temps "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f3c72341",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "899bea56",
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a79511a2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Tests"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1cb5630c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Fonction"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d8e43d4f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"2. 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": "fec2675f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a5fb9697",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "462ee355",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a1ca0ba6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8abf0381",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "eaaff40e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"3. 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": "4dc8d464",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1b775dbe",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "49807880",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "43933ce7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "321d4456",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "27a4391a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"4. 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": "a0d4f074",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ce6c0a6e",
|
||||
"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 "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ebb87f94",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
<!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>
|
|
@ -0,0 +1,46 @@
|
|||
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');
|
||||
});
|
|
@ -0,0 +1,28 @@
|
|||
Recherche par dichotomie et complexité
|
||||
######################################
|
||||
|
||||
:date: 2023-03-13
|
||||
:modified: 2023-03-13
|
||||
:authors: Benjamin Bertrand
|
||||
:tags: Python, Compexité
|
||||
:category: 1NSI
|
||||
:summary: Mesurer le temps d'execution, calculer une complexité et recherche par dichotomie.
|
||||
|
||||
|
||||
Éléments du programme
|
||||
=====================
|
||||
|
||||
Contenus
|
||||
--------
|
||||
|
||||
Capacités attendues
|
||||
-------------------
|
||||
|
||||
Commentaires
|
||||
------------
|
||||
|
||||
Progression
|
||||
===========
|
||||
|
||||
Étape 1:
|
||||
--------
|
|
@ -0,0 +1,61 @@
|
|||
<!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>
|
|
@ -2,7 +2,7 @@ Spécialité première NSI
|
|||
#######################
|
||||
|
||||
:date: 2022-07-25
|
||||
:modified: 2023-02-21
|
||||
:modified: 2023-03-13
|
||||
:authors: Bertrand Benjamin
|
||||
:category: 1NSI
|
||||
:tags: Progression
|
||||
|
@ -80,7 +80,7 @@ Période 4
|
|||
Programmation Javascript, corpus et interaction sur le web.
|
||||
|
||||
.. big_button::
|
||||
:title: Recherche pas dichotomie
|
||||
:title: Recherche par dichotomie
|
||||
|
||||
.. big_button::
|
||||
:title: Dictionnaires
|
||||
|
|
Loading…
Reference in New Issue