Edexcel GCSE Computer Science June 2022 Paper 2, Question 6: the two-letter word search
Pearson Edexcel 1CP2/02 June 2022, Question 6: write a Python linear search for a two-letter word in a sorted 2D list that stops when the word is found, when its place has been passed, or at the end, and suggests the next word. A model answer and the fifteen marks.
Question 6 is the last and hardest question of the Pearson Edexcel GCSE Computer Science Paper 2 from the June 2022 series (1CP2/02): 15 marks for a program written from a list of requirements. It is a linear search with a twist. The list is sorted, so the search can give up early.
We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2022 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.
The question in short
A word game has 107 valid two-letter words, each with a points value. They are stored in alphabetical order in a two-dimensional list, wordTable. Each record is a word and its points.
The program asks for a two-letter word, in capitals or lower case. It does a linear search that stops when:
- the word is found: tell the user the word and its points;
- the place where the word would be has been passed: suggest the next word and its points;
- the end of the list is reached: suggest the last word and its points.
It must work for any length of wordTable.
The idea
In a sorted list, as soon as you meet a word that comes after yours in the alphabet, yours cannot be further on. Python compares strings alphabetically with >, so wordTable[index][0] > word means "passed it".
A model answer
word = input("Enter a two-letter word: ")
word = word.upper()
found = False
passed = False
index = 0
while index < len(wordTable) and not found and not passed:
if wordTable[index][0] == word:
found = True
elif wordTable[index][0] > word:
passed = True
else:
index = index + 1
if found:
print(word + " is worth " + str(wordTable[index][1]) + " points.")
else:
if not passed:
index = len(wordTable) - 1
print(word + " is not in the list.")
print("Use " + wordTable[index][0] + " worth " + str(wordTable[index][1]) + " points.")
Where the fifteen marks are
Six single marks: converting the input to upper case; using the length of the list as the loop's limit; a way of knowing the word was found; a way of knowing its place was passed; keeping track of the suggested word; and two-dimensional indexing ([index][0] and [index][1]).
Then up to 3 each for solution design, good programming practice and functionality. A while loop is preferred here, because the search stops early. Comments, white space and meaningful names are worth 3 marks between them.
Where the marks are lost
range(107). It must work for any length:len(wordTable).- A
forloop that always runs to the end. It finds the word, and cannot make a suggestion. - Forgetting
.upper(). The table is in capitals. "no" would never match "NO". - The word after the last one. For ZH, nothing in the list is greater, so the loop runs off the end. The suggestion is then the last word, and
indexhas to be pulled back inside the list first. - No comments. This question marks them.
Run it
A short table stands in for the 107 words. The robot's light is green when your word is valid.
The program
from bugbot import *
connect()
wordTable = [["AA", 2], ["AB", 4], ["MU", 4], ["NO", 2], ["OD", 3], ["ZA", 11]]
word = input("Enter a two-letter word: ")
word = word.upper()
found = False
passed = False
index = 0
# stop when found, when passed, or at the end of the list
while index < len(wordTable) and not found and not passed:
if wordTable[index][0] == word:
found = True
elif wordTable[index][0] > word:
passed = True
else:
index = index + 1
if found:
led("green")
print(word + " is worth " + str(wordTable[index][1]) + " points.")
else:
led("red")
if not passed:
index = len(wordTable) - 1
print(word + " is not in the list.")
print("Use " + wordTable[index][0] + " worth " + str(wordTable[index][1]) + " points.")
Questions
What is the answer to Edexcel GCSE Computer Science 2022 Paper 2 Question 6?
Convert the input to upper case. Loop with a while while the index is inside the list and the word is neither found nor passed. Compare wordTable[index][0] with the word: equal means found, greater means passed, otherwise move on. Then output the word and its points, or suggest the word at the index where the search stopped.
How can a linear search stop early?
If the list is sorted, the search can stop as soon as it meets an item greater than the target, because the target cannot appear after that point.
How do I index a two-dimensional list in Python?
Use two pairs of square brackets: table[row][column]. In a table of records, table[3][0] is the first field of the fourth record.
More from this paper
Every Edexcel 1CP2 question we have worked · Guide: Linear search and binary search explained
Learn it step by step
- F5.5 Linear search Algorithms
- F3.5 Two-dimensional arrays 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 Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.