AQA GCSE Computer Science sample Paper 1, Question 13: the block columns

AQA 8525 sample assessment material, Paper 1 Question 13: trace two block-moving programs, then design an algorithm that moves every block from column 0 to column 1 keeping their order, using MOVE and HEIGHT. Worked through, with a simulation to run.

Past paper questionAQA 8525/1BSample Paper 110 marksWrite a program

Question 13 of AQA's sample assessment material for GCSE Computer Science Paper 1 (8525/1B, the Python paper) is 10 marks about stacks of blocks in three columns. Two parts trace given programs (3 marks each). The third asks for an algorithm of your own (4 marks), and it has a twist.

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

The subroutines

  • MOVE(source, destination) takes the top block off one column and puts it on top of another.
  • HEIGHT(column) returns how many blocks are in a column.
  • BLOCK_ON_TOP(column) returns the label of the top block.

Parts 1 and 2: tracing

Part 1. Column 0 starts with C on top of B on top of A. MOVE(0, 1) puts C in column 1. MOVE(0, 2) puts B in column 2. MOVE(0, 2) puts A on top of B in column 2. Result: column 0 empty, C in column 1, A on B in column 2.

Part 2. The loop WHILE HEIGHT(0) > 1: MOVE(0, 1) moves blocks until one is left in column 0. C then B go to column 1, leaving A. Then MOVE(1, 2) moves the top of column 1, which is B, to column 2. Result: A in column 0, C in column 1, B in column 2.

Part 3: move every block from column 0 to column 1, keeping the order

The twist: moving blocks one at a time reverses them. Take C, B, A off column 0 one by one onto column 1 and you get A on top. Move them a second time, through a spare column, and they come out the right way up.

WHILE HEIGHT(0) > 0
    MOVE(0, 2)
ENDWHILE
WHILE HEIGHT(2) > 0
    MOVE(2, 1)
ENDWHILE

It works however many blocks there are, because each loop runs until its column is empty.

The four marks: a loop that moves from column 0 to column 2; a condition that stops when column 0 is empty; a second loop that moves from column 2 to column 1, with its condition; and the subroutines used correctly throughout. A FOR loop that reads HEIGHT(0) once to get its limit is accepted as well.

Where the marks are lost

  • One loop straight to column 1. The order comes out reversed. Try it with the demo.
  • WHILE HEIGHT(0) > 1. That leaves one block behind. The check is > 0.
  • Guessing the number of blocks. "However many blocks start in column 0" means no fixed count.
  • MOVE(0) with one argument, or HEIGHT used without a column. The last mark is for using them correctly.

Run it

A small simulation of the columns. PLAN chooses between the one-loop answer (reversed) and the two-loop answer (order kept).

PLAN = 1 leaves column 1 as A on top: reversed. PLAN = 2 leaves it C, B, A from the top: the order preserved.
The program
from bugbot import *
connect()

# 1 moves straight to column 1. 2 goes through column 2 first
PLAN = 2

columns = [["A", "B", "C"], [], []]      # bottom first, so C is on top

def HEIGHT(column):
    return len(columns[column])

def MOVE(source, destination):
    block = columns[source].pop()
    columns[destination].append(block)
    print("moved", block, ":", columns)

if PLAN == 1:
    while HEIGHT(0) > 0:
        MOVE(0, 1)
else:
    while HEIGHT(0) > 0:
        MOVE(0, 2)
    while HEIGHT(2) > 0:
        MOVE(2, 1)

print("column 1 from the top:", list(reversed(columns[1])))
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 sample Paper 1 Question 13.3?

Two loops. While HEIGHT(0) > 0, MOVE(0, 2). Then while HEIGHT(2) > 0, MOVE(2, 1). Moving through the spare column reverses the blocks twice, which puts them back in the original order.

Why does moving blocks one at a time reverse them?

The block on top moves first and ends up at the bottom of the new column. Each later block goes on top of it, so the last one moved, the original bottom block, finishes on top.

What is a stack?

A data structure where items are added to and removed from the top only, like the columns of blocks here. The last item put in is the first one taken out.

More from this paper

Every AQA 8525 question we have worked · Guide: Big O notation explained

Learn it step by step

  1. F4.2 Parameters and return values Functions and structured code
  2. F2.6 Condition-controlled loops: while Decisions and loops
  3. F5.1 What an algorithm is Algorithms
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.