AQA GCSE Computer Science June 2022 Paper 1, Question 13.2: finding a run of five cards

AQA 8525 June 2022 Paper 1, Question 13.2: write a Python program that checks a sorted array of 100 cards for five values in sequence. The counting idea, a model answer, the index trap, the six marks explained, and the program to run.

Past paper questionAQA 8525/1BJune 2022 Paper 16 marksWrite a program

Question 13.2 of the AQA GCSE Computer Science June 2022 Paper 1 (8525/1B, the Python paper) is a hard 6 mark question. There is no input and no output. It is pure algorithm: look through an array for a pattern. The answer is eight lines, once you have the idea.

We do not copy the exam paper here. Open it beside this page: AQA June 2022 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.

The question in short

A player holds 100 cards. Their values are in an array called cards, from cards[0] to cards[99], already in numerical order. Values can repeat, because each number appears twice in the full pack.

The player has won if five cards next to each other go up in ones, such as 7, 8, 9, 10, 11. A Boolean variable called gameWon is already False. Your program sets it to True if there is a run.

The idea

You do not need to look at five cards at a time. Look at two: each card and the one before it. Keep a count of how long the current run is.

  • If this card is exactly one more than the one before, the run is one longer.
  • If it is not, the run is broken. A new run starts here, with a length of 1.
  • If the run ever reaches 5, the player has won.

Walk through 3, 4, 5, 5, 6, 7, 8, 9 with that rule. The run goes 1, 2, 3, then the second 5 breaks it (5 is not one more than 5) and the run is back to 1. Then 2, 3, 4, 5: won, on the 9.

A model answer

run = 1
for i in range(1, 100):
    if cards[i] == cards[i - 1] + 1:
        run = run + 1
        if run == 5:
            gameWon = True
    else:
        run = 1

The index trap

The loop compares cards[i] with cards[i - 1], so i must start at 1. If it started at 0, cards[-1] would be read. (In Python that is the last card, so nothing crashes. It just quietly compares the wrong cards.)

If you compare forwards, cards[i] with cards[i + 1], the loop must stop at 98: range(99). Going to 99 reads cards[100], which does not exist.

Either way, there are 99 pairs in 100 cards.

Where the six marks are

Two marks are for design: a loop that tries to visit each card, and selection that tries to compare two cards.

Four marks are for a program that works:

  • a loop that goes through the array without going out of range;
  • a correct comparison of two values in the array;
  • correctly checking for five in sequence;
  • setting gameWon to True in the right place.

Any error in the code caps you at 5.

Where the marks are lost

  • Going out of range. See above. A whole mark is for staying inside the array.
  • Never resetting the count. Then 1, 2, 9, 10, 20, 21 counts as a run. The else is essential.
  • Resetting to 0. A run that has just started already has one card in it. Start and reset at 1 and test for 5. (Starting at 0 and testing for 4 also works. Mixing the two does not.)
  • Setting gameWon = False in the else. A run found early would be wiped out by the cards after it.
  • Checking five cards by hand with cards[i + 4]. It can work, but the loop must then stop at 95, and that is easy to miss.

Run it

An 8 card hand is easier to follow than 100, so this program works on any length of list. It prints the run count at every card. The robot's light goes green when the hand wins.

This hand wins on the last card: the run reaches 5 at the 9. Change the last card to 10 and it does not win.
The program
from bugbot import *
connect()

# change the hand and press Run. It must be in order
cards = [3, 4, 5, 5, 6, 7, 8, 9]

gameWon = False
run = 1
print("card run")
print(cards[0], run)
for i in range(1, len(cards)):
    if cards[i] == cards[i - 1] + 1:
        run = run + 1
        if run == 5:
            gameWon = True
    else:
        run = 1
    print(cards[i], run)

print("gameWon is", gameWon)
if gameWon:
    led("green")
else:
    led("red")
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What is the answer to AQA GCSE Computer Science 2022 Paper 1 Question 13.2?

Keep a run counter starting at 1. Loop i from 1 to 99. If cards[i] equals cards[i - 1] + 1, add 1 to the counter and set gameWon to True if it reaches 5. Otherwise reset the counter to 1.

How do I avoid an index out of range error when comparing neighbouring items?

Count the pairs, not the items. A list of 100 items has 99 neighbouring pairs. If you compare item i with item i + 1, stop at 98. If you compare item i with item i - 1, start at 1.

How do I get marks on a hard algorithm question if I cannot see the whole answer?

Write a loop over the array and an if that compares two cards. Those are the two design marks, and they are given even if the rest does not work.

More from this paper

Every AQA 8525 question we have worked

Learn it step by step

  1. F3.4 Iterating over a list Strings, lists and records
  2. F2.7 Loop patterns Decisions and loops
  3. F13.4 Programming questions Exam preparation
Open the lessons

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.