Recursion and computational thinking · A level · OCR H446 2.1.3, AQA 7517 4.4.1.9 · about 20 min
Thinking procedurally, compound procedures and compound data, and putting a model into action.
[1 mark]What is procedural decomposition?
[1 mark]Which are examples of composition?
Tick every answer that is true.
[1 mark]Put the steps of automation in order.
Number the lines 1 to 4 to put them in the right order.
Execute the codeCreate an algorithmImplement the model in data structuresImplement the algorithm in program code[1 mark]What does this program print?
def double(x):
return x * 2
def add_three(x):
return x + 3
def compose(f, g):
def both(x):
return f(g(x))
return both
h = compose(double, add_three)
print(h(4))[1 mark]When thinking procedurally about a problem, what should you identify?
Tick every answer that is true.
A leg is a tuple (direction, cm), where direction is one of "forward", "backward", "left" or "right" and cm is a distance. A shape is a list of legs. A route is a list of shapes. The starter builds this route from two shapes:
step = [("forward", 20), ("right", 20)], square = [("forward", 20), ("right", 20), ("backward", 20), ("left", 20)], route = [step, square, step]
Write three procedures, each built from the one before:
- drive_leg(leg) prints leg <direction> <cm> and drives that leg at speed 60, sliding sideways for left and right (the robot never turns);
- drive_shape(shape) drives every leg of a shape, using drive_leg;
- drive_route(route) drives every shape of a route, using drive_shape.
Drive the route, then print route done: <shapes> shapes, <legs> legs, which for this route is route done: 3 shapes, 8 legs. The robot should finish 40 cm right of and 40 cm up from where it started.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
step = [("forward", 20), ("right", 20)]
square = [("forward", 20), ("right", 20), ("backward", 20), ("left", 20)]
route = [step, square, step]
def drive_leg(leg):
direction, cm = leg
def drive_shape(shape):
pass
def drive_route(route):
passPlan your program here, then type it in and press Run.
zigzag and a new route that uses it twice, without changing any of the three procedures.