Searching & max / min · Búsqueda y máx / 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.
Búsqueda lineal
- Una búsqueda lineal examina cada elemento uno tras otro hasta encontrar el objetivo.
- Es un método estándar con nombre específico en el programa de estudios.
- Utilice un bucle y recuerde cuándo encuentra una coincidencia.
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 · Búsqueda lineal, paso a paso
A linear search checks each item in turn until it finds the target. · Una búsqueda lineal revisa cada elemento sucesivamente hasta encontrar el objetivo.
Finding the maximum
- Start by assuming the first item is the biggest.
- Then check the rest; if one is bigger, remember it instead.
Encontrar el máximo
- Comience asumiendo que el primer elemento es el más grande.
- Luego revise los demás; si alguno es mayor, guárdelo como nuevo valor de referencia.
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.
Encontrar el mínimo
- La misma idea, pero conserve el valor más pequeño visto hasta ahora.
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 el examen: métodos estándar
- La búsqueda lineal y la búsqueda del máximo / mínimo / promedio están nombradas en la sección 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.
Errores comunes
- La búsqueda lineal revisa cada elemento uno tras otro hasta encontrar el objetivo.
- Para el máximo o mínimo, comience desde el primer elemento y compare los demás contra él.
Now you try
- Loop through the list to search or compare.
- Press Check answer to test your code.
Ahora practique
- Recorra la lista para buscar o comparar.
- Presione Comprobar respuesta para verificar su código.
Write contains(nums, target) that returns True if target is in the list, using a linear search · búsqueda lineal loop (do not use the in keyword). · Escribe contains(nums, target) que devuelva True si target está en la lista, usando un bucle de búsqueda lineal (no uses la palabra clave in).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
The list nums is set. Find the largest value with a loop and store it in biggest (do not use max()). · La lista nums está establecida. Encuentra el valor más grande con un bucle y guárdalo en biggest (no uses max()).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Find the smallest value in nums with a loop and store it in smallest (do not use min()). · Encuentra el valor más pequeño en nums con un bucle y guárdalo en smallest (no uses min()).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.