Searching & max / min · Busca & 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.
Busca linear
- Uma busca linear examina cada item em sequência até encontrar o alvo.
- É um método padrão nomeado no currículo.
- Use um laço e lembre-se quando encontrar uma correspondência.
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 · Busca linear, passo a passo
A linear search checks each item in turn until it finds the target. · Uma busca linear verifica cada item sequencialmente até encontrar o alvo.
Finding the maximum
- Start by assuming the first item is the biggest.
- Then check the rest; if one is bigger, remember it instead.
Encontrando o máximo
- Comece assumindo que o primeiro item é o maior.
- Depois verifique o resto; se algum for maior, lembre-se dele em vez deste.
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.
Encontrando o mínimo
- A mesma ideia, mas mantenha o valor menor visto até agora.
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.
Na prova: métodos padrão
- Busca linear e encontrar máximo / mínimo / média são nomeados na seção 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.
Erros comuns
- Busca linear verifica cada item em sequência até encontrar o alvo.
- Para max ou min, comece pelo primeiro item, depois compare o resto contra ele.
Now you try
- Loop through the list to search or compare.
- Press Check answer to test your code.
Agora você tenta
- Iterar pela lista para pesquisar ou comparar.
- Clique em Check answer para testar seu código.
Write contains(nums, target) that returns True if · se target is in the list, using a linear search · busca linear loop (do not use the in keyword). · Escreva contains(nums, target) que retorne True se target estiver na lista, usando um laço de busca linear (não use a palavra-chave in).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
The list nums is set. Find the largest value with a loop and store it in biggest (do not use max()). · A lista nums está definida. Encontre o maior valor com um laço e armazene-o em biggest (não use max()).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Find the smallest value in nums with a loop and store it in smallest (do not use min()). · Encontre o menor valor em nums com um laço e armazene-o em smallest (não use min()).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.