AQA GCSE Computer Science June 2023 Paper 1, Question 12.2: writing a linear search
AQA 8525 June 2023 Paper 1, Question 12.2: write your own linear search over a list of fruit and output True or False. Two model answers built step by step, the seven marks explained, and the program to run.
Question 12.2 of the AQA GCSE Computer Science Paper 1 sat on 19 May 2023 (8525/1B, the Python paper) asks you to write a linear search in Python. It is worth 7 marks. Linear search is on the specification by name, so a question like this can come up in any year. Learn the pattern once and the marks are yours.
We do not copy the exam paper here. Open it beside this page: AQA June 2023 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.
The question in short
You are given one line of Python that makes a list of six fruit names, not in alphabetical order. You extend the program so that it:
- asks the user for a word;
- outputs
Trueif the word is in the list; - outputs
Falseif it is not.
The important sentence is this one: you must write your own linear search and not use a built-in search. In Python that rules out if word in fruits and fruits.index(word).
The pattern
Every linear search has the same four parts.
- A flag that says "not found yet".
- A loop that visits every item.
- An
ifthat compares the item with the target, and sets the flag. - After the loop, one output that depends on the flag.
A model answer
fruits = ["banana", "apple", "orange", "pear", "grape", "pineapple"]
word = input("Which word? ")
found = False
for fruit in fruits:
if fruit == word:
found = True
if found:
print("True")
else:
print("False")
You can stop early with break once the word is found. It is tidier, and it is not needed for the marks.
If you prefer index numbers, this is the same search with a while loop. It stops as soon as the word is found.
found = False
i = 0
while i < len(fruits) and not found:
if fruits[i] == word:
found = True
i = i + 1
Where the seven marks are
Three marks are for design, given even if the syntax is wrong:
- getting a word from the user and storing it;
- using iteration;
- trying to check each position in the list.
Four marks are for a program that works:
- a loop that starts at one end of the list and can reach the other;
- comparing the word with all six fruits;
- doing the right thing when there is a match;
- outputting only one of
TrueandFalse.
Two caps apply. Any error in the code limits you to 6. Using a built-in search limits you to 5, however neat it is.
Where the marks are lost
if word in fruits. One line, correct output, and a maximum of 5 out of 7. The question said not to.else: print("False")inside the loop. Then the program printsFalsefor every fruit that does not match: five times for "pear". The output must happen once, after the loop.range(6)off by one.range(1, 6)misses banana.range(0, 5)misses pineapple.range(len(fruits))is always right.- Setting
found = Falseinside the loop. A later fruit that does not match then wipes out the match you found earlier. - Returning a position. The question asks for
TrueorFalse, not where the word is.
Run it
Type a word when it asks. The robot's light shows green for found and red for not found, and the program prints each comparison so you can see the search happen.
The program
from bugbot import *
connect()
fruits = ["banana", "apple", "orange", "pear", "grape", "pineapple"]
word = input("Which word? ")
found = False
for fruit in fruits:
print(" is it", fruit + "?")
if fruit == word:
found = True
break
if found:
led("green")
print("True")
else:
led("red")
print("False")
Want to watch a robot do a linear search along a row of cards? It is in the guide: Linear search and binary search explained.
Part 3 of the same question
The next part asks why a binary search cannot be used on this list (1 mark). The answer is in the list itself: it is not sorted. Binary search only works on an ordered list.
Questions
What is the answer to AQA GCSE Computer Science 2023 Paper 1 Question 12.2?
Read a word with input(), set a flag to False, loop over every item in the list comparing it with the word, set the flag to True on a match, and after the loop print True or False depending on the flag.
Can I use "in" for a linear search in the exam?
Not when the question tells you to write your own search routine. On this question a built-in search was capped at 5 marks out of 7. Write the loop.
How do I write a linear search in Python?
Use a for loop over the list with an if inside it that compares each item with the target. Set a found flag when they match, and print the result once, after the loop has finished.
Why can a binary search not be used on the fruits list?
Because the list is not in order. A binary search relies on the list being sorted so that it can throw away half of it after each comparison.
More from this paper
- Question 7: A ticket price with a group discount 6 marks
- Question 13: Validate a grid reference until it is correct 6 marks
- Question 15: Share a bill: loop until it is paid, then report the tip 8 marks
- Question 16: A dice game to 21 with random numbers 11 marks
Every AQA 8525 question we have worked · Guide: Linear search and binary search explained
Learn it step by step
- F5.5 Linear search Algorithms
- F3.4 Iterating over a list Strings, lists and records
- F13.4 Programming questions Exam preparation
This is our own explanation of a published exam question. It is not written or endorsed by AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.