Pseudocode in the exam languages
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.
Do this lesson in the simulatorExams cannot assume you know Python, so they give code in a pseudocode that every candidate can read, and accept answers in pseudocode or a real language. OCR has its exam reference language; AQA has its pseudo-code. You met both through the course. This lesson puts them side by side, points out where translating goes wrong, and practises the question that turns one into working code.
The two languages side by side
| Construct | OCR exam reference language | AQA pseudo-code | Python |
|---|---|---|---|
| Assignment | x = 3 |
x ← 3 |
x = 3 |
| Equal, not equal | ==, != |
=, ≠ |
==, != |
| Integer division, remainder | DIV, MOD |
DIV, MOD |
//, % |
| Output, input | print(x), x = input("") |
OUTPUT x, x ← USERINPUT |
print(x), x = input() |
| Selection | if … then … elseif … else … endif |
IF … THEN … ELSE … ENDIF |
if … elif … else |
| Count-controlled loop | for i = 0 to 9 … next i |
FOR i ← 0 TO 9 … ENDFOR |
for i in range(0, 10) |
| Condition at the start | while … endwhile |
WHILE … ENDWHILE |
while …: |
| Condition at the end | do … until … |
REPEAT … UNTIL … |
while True: … if …: break |
| Subroutines | function … endfunction, procedure … endprocedure |
SUBROUTINE … ENDSUBROUTINE |
def |
| Array | array route[10], route[0] |
route ← [0, 0, 0], route[0] |
route = [0] * 10, route[0] |
| Length | route.length |
LEN(route) |
len(route) |
| Class | class … endclass, constructor new |
class, constructor __init__ |
The traps in translating
- Inclusive loops.
for i = 0 to 9andFOR i ← 0 TO 9include 9, so they run ten times. The Python isrange(0, 10). This is the commonest translation error. - Post-condition loops.
do … untilandREPEAT … UNTILrun the body at least once and stop when the condition becomes true. A Pythonwhilechecks first and continues while the condition is true, so the condition is reversed and the body must run once regardless. - Assignment and comparison. AQA's
=compares; its←assigns. OCR's=assigns; its==compares. - DIV with negatives. Python's
//rounds down, so-7 // 2is -4. Exam questions avoid relying on this, but do not assume it. - Fixed-size arrays. Pseudocode arrays usually have a declared size and an index you manage yourself, such as a
toppointer. A Python list grows withappend. Either translation is accepted if it behaves the same, but a question that asks you to complete the pseudocode wants the pointer. - Parameters by reference. OCR marks
:byRef. Python cannot pass a number by reference, so return the new value instead (lesson A1.4).
A post-condition loop, both ways:
do
reading = distance()
until reading < 30
readings = iter([55, 42, 31, 29, 18]) # stands in for distance() so this cell runs anywhere
while True:
reading = next(readings)
print("read", reading)
if reading < 30: # until: stop when the condition is true
break
Reading a class in the exam reference language
OCR questions often give a class and ask you to complete a method or use it. This stack keeps its items in a fixed-size array with a pointer to the top:
class Stack
private items
private top
public procedure new()
array items[20]
top = -1
endprocedure
public procedure push(item)
top = top + 1
items[top] = item
endprocedure
public function pop()
if top == -1 then
return "" // nothing to pop
endif
item = items[top]
top = top - 1
return item
endfunction
public function isEmpty()
return top == -1
endfunction
endclass
Translate it one method at a time, keeping its structure. new becomes __init__; private attributes become names with two underscores; isEmpty can keep its name or follow Python's style as is_empty. The array and pointer can stay, or become a list with append and pop: both behave as a stack, last in, first out (lesson A3.2).
Writing pseudocode yourself, examiners do not deduct marks for small differences in syntax, as long as the logic is clear and unambiguous. They do deduct for logic that does not work: a loop that never ends, a variable used before it is given a value, a return missing on one path.
Task: there and back with a stack
Translate 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): putsitemon the top;pop(self): removes and returns the top item;is_empty(self): returnsTrueif there are no items, otherwiseFalse.
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)]
Challenges
- Translate
popexactly as the pseudocode has it, with an array of 20 and atoppointer, and check the robot still comes home. - Write
pushin AQA pseudo-code as a subroutine that takes the array, the pointer and the item, and returns the new pointer. - The pseudocode's
pushdoes not check whether the array is full. Add the check in pseudocode, and say what should happen.