The worksheetDownload the PDF
Answers

F4.4 Decomposition and abstraction

Functions and structured code · GCSE · OCR J277 2.1.1, AQA 8525 3.1.1, Edexcel 1CP2 1.1.1 · about 20 min

BugBotLab

What this lesson is about

Structure diagrams, and hiding the details of a job behind a function.

Questions 5 marks in all

  1. [1 mark]What is decomposition?

    1. ABreaking a problem down into smaller, more manageable sub-problems
    2. BRemoving unnecessary detail
    3. CPutting code in alphabetical order
    4. DDeleting unused code
    Answer: A. Each sub-problem can then be solved, and often written as a subprogram, on its own.
  2. [1 mark]What is abstraction?

    1. ARemoving unnecessary detail to focus on what matters
    2. BBreaking a problem into parts
    3. CWriting code in capitals
    4. DTesting a program
    Answer: A. A map of a bus route is an abstraction; so is a function that hides how it does its job behind a name.
  3. [1 mark]A simulator's mat keeps the walls and distances but not the carpet colour. Which idea is that?

    1. AAbstraction
    2. BDecomposition
    3. CIteration
    4. DCasting
    Answer: A. It leaves out detail that does not matter for the robot's problem.
  4. [1 mark]What does a structure diagram show?

    1. AA problem broken down into smaller sub-problems, top to bottom
    2. BThe order lines run in
    3. CThe values of variables as a program runs
    4. DHow data moves over a network
    Answer: A. The whole problem at the top, the parts beneath it, each part broken down again.
  5. [1 mark]Why keep a delivery route as a list of stops, separate from the code that drives?

    1. AThe route can change without changing the functions
    2. BLists run faster than functions
    3. CPython requires it
    4. DFunctions cannot use numbers
    Answer: A. Data separate from code is abstraction: the functions do not need to know which stops they visit.

The task: three stops

Write go_to(x, y) and signal(), then use them to visit three stops in order, signalling at each: A at (30, 10), B at (30, 60) and C at (0, 60), all in cm from where the robot starts. The robot starts in the bottom left of the mat.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def go_to(x, y):
    """Drive to (x, y), in cm from where the robot started."""
    here_x, here_y = position()

def signal():
    """Light and beep at a stop."""
    led("green")

The hint students can ask for: Work out where you are, then how far you still have to go in each direction, and drive that difference. Sideways and forwards are separate moves. Do the signal at each stop.

A solution

from bugbot import *
connect()
def go_to(x, y):
    """Drive to (x, y), in cm from where the robot started."""
    here_x, here_y = position()
    across = x - here_x
    up = y - here_y
    if across > 0:
        right(60, distance=across)
    elif across < 0:
        left(60, distance=-across)
    if up > 0:
        forward(60, distance=up)
    elif up < 0:
        backward(60, distance=-up)

def signal():
    """Light and beep at a stop."""
    led("green")
    tone(784, 0.3)
    led("off")

for x, y in [(30, 10), (30, 60), (0, 60)]:
    go_to(x, y)
    signal()

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.