Year 10 Computing — Searching a list
Follow a search that goes through a list from the front until it finds what it wants, and work out where it stops and how many values it had to look at.
Name: ________________________
1.Why does a search have to look at every value in a list before it can say that the value it wants is not there?
- a) Because a list has no way of saying how long it is
- b) Because the search always begins at the last value
- c) Because a search cannot count how many values it has looked at
- d) Because the value it has not looked at yet could be the one
2.This search moves along the list until it meets the value it is looking for. What does the program print?
values = [7, 4, 6, 1, 9] i = 0 while values[i] != 6: i = i + 1 print(i)3.This one does not stop when it finds a match. It carries on and counts them. What does the program print?
values = [4, 9, 4, 2, 4, 7] count = 0 for v in values: if v == 4: count = count + 1 print(count)4.Two searches are run, each stopping as soon as it finds what it wants. The first is on a list of 18 values and finds what it wants at position 4. The second is on a list of 40 values and what it wants is not there at all. How many values are looked at altogether?
5.Every value of one list is compared against every value of another here. What does the program print?
found = 0 for n in [3, 6, 9]: for m in [6, 12]: if n == m: found = found + 1 print(found)6.A search that stops as soon as it finds what it wants is run on the list below. Match each value being looked for to the position the search stops at.
values = [12, 7, 19, 4, 15, 3]
- Looking for 12
- Looking for 7
- Looking for 4
- Looking for 15
- Looking for 3
- 1
- 4
- 5
- 0
- 3
7.A search that stops as soon as it finds what it wants is run on a list of 30 values, and the value it is looking for is not in the list at all. How many values does it look at before it can say so?
- a) 1
- b) 30
- c) 29
- d) 15
8.The value being looked for appears twice in this list. What does the program print?
values = [5, 8, 5, 8] i = 0 while values[i] != 8: i = i + 1 print(i)
Answer key — Year 10 Computing — Searching a list
- 1. d) Because the value it has not looked at yet could be the one
- 2. 2
- 3. 3
- 4. 45
- 5. 1
- 6. Looking for 12 → 0; Looking for 7 → 1; Looking for 4 → 3; Looking for 15 → 4; Looking for 3 → 5
- 7. b) 30
- 8. 1