Exam preparation · A level · OCR H446 2.2.1, AQA 7517 4.4.1.2, Eduqas A500QS 1.8 · about 45 min
OCR's exam reference language and AQA's pseudo-code side by side with Python, the traps in translating, and a stack class that brings the robot home.
[1 mark]How many times does the AQA pseudo-code loop FOR i ← 1 TO 10 run?
[1 mark]Which Python matches the OCR loop do ... until reading < 30?
[1 mark]In AQA pseudo-code, what does x ← 3 do?
[1 mark]In OCR's exam reference language, what is the name of a class's constructor?
[1 mark]This is a stack from the exam reference language, translated. What does it print?
items = [None] * 5
top = -1
for move in ["forward", "right", "left"]:
top = top + 1
items[top] = move
while top != -1:
print(items[top])
top = top - 1Translate the Stack class above into Python, then use it to bring the robot home.
Write class Stack with:
- __init__(self): an empty stack;
- push(self, item): puts item on the top;
- pop(self): removes and returns the top item;
- is_empty(self): returns True if there are no items, otherwise False.
route is a list of moves in order. Each move is a tuple (direction, cm): direction is one of "forward", "backward", "left" or "right", and cm is a whole number of centimetres. Drive each move in order at speed 50, pushing it onto a stack after it is driven. The robot then reaches the charger: play a note and print arrived.
To come home, loop while not <stack>.is_empty(): pop a move, print undo <direction> <cm>, and drive the opposite direction (forward and backward are opposites, as are left and right) the same distance at speed 50. After the loop print home. Do not reverse the list yourself: the stack must do it.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
route = [("forward", 30), ("right", 20), ("forward", 15), ("left", 10)]Plan your program here, then type it in and press Run.
pop exactly as the pseudocode has it, with an array of 20 and a top pointer, and check the robot still comes home.push in AQA pseudo-code as a subroutine that takes the array, the pointer and the item, and returns the new pointer.push does not check whether the array is full. Add the check in pseudocode, and say what should happen.