Searching & max / min · Recherche et max / min
Linear search
- A linear search looks at each item in turn until it finds the target.
- It is a named standard method in the syllabus.
- Use a loop and remember when you find a match.
Recherche linéaire
- Une recherche linéaire examine chaque élément tour à tour jusqu'à trouver la cible.
- C'est une méthode standard nommée dans le programme.
- Utilisez une boucle et notez quand vous trouvez une correspondance.
nums = [4, 9, 2, 7]
target = 9
found = False
for n in nums:
if n == target:
found = True
print(found) # True
Linear search, step by step · Recherche linéaire, étape par étape
A linear search checks each item in turn until it finds the target. · Une recherche linéaire vérifie chaque élément à tour de rôle jusqu'à trouver la cible.
Finding the maximum
- Start by assuming the first item is the biggest.
- Then check the rest; if one is bigger, remember it instead.
Trouver le maximum
- Commencez par supposer que le premier élément est le plus grand.
- Ensuite, vérifiez le reste ; si l'un est plus grand, notez-le à la place.
nums = [4, 9, 2, 7]
biggest = nums[0]
for n in nums:
if n > biggest:
biggest = n
print(biggest) # 9
Finding the minimum
- The same idea, but keep the smallest value seen so far.
Trouver le minimum
- Même principe, mais gardez la valeur la plus petite vue jusqu'à présent.
nums = [4, 9, 2, 7]
smallest = nums[0]
for n in nums:
if n < smallest:
smallest = n
print(smallest) # 2
In the exam: standard methods
- Linear search and finding maximum / minimum / average are named in section 7.
En examen : méthodes standards
- La recherche linéaire et la recherche maximum / minimum / moyenne sont nommées dans la section 7.
Found ← FALSE
FOR I ← 1 TO 4
IF Number[I] = Target
THEN
Found ← TRUE
ENDIF
NEXT I
Common mistakes
- Linear search checks each item in turn until it finds the target.
- For max or min, start from the first item, then compare the rest against it.
Erreurs courantes
- La recherche linéaire vérifie chaque élément tour à tour jusqu'à trouver la cible.
- Pour max ou min, commencez par le premier élément, puis comparez le reste à celui-ci.
Now you try
- Loop through the list to search or compare.
- Press Check answer to test your code.
À vous maintenant
- Parcourir la liste pour chercher ou comparer.
- Appuyez sur Vérifier la réponse pour tester votre code.
Write contains(nums, target) that returns True if · si target is in the list, using a linear search · recherche linéaire loop (do not use the in keyword). · Écrivez contains(nums, target) qui retourne True si target est dans la liste, en utilisant une boucle de recherche linéaire (n'utilisez pas le mot-clé in).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
The list nums is set. Find the largest value with a loop and store it in biggest (do not use max()). · La liste nums est définie. Trouvez la valeur maximale avec une boucle et stockez-la dans biggest (n'utilisez pas max()).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Find the smallest value in nums with a loop and store it in smallest (do not use min()). · Trouvez la valeur minimale dans nums avec une boucle et stockez-la dans smallest (n'utilisez pas min()).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.