Programming techniques and object-oriented programming · A level · OCR H446 2.2.1, AQA 7517 4.1.1.1, Eduqas A500QS 1.4 · about 20 min
The A level data types, references and user-defined types, constants, and definite and indefinite iteration.
[1 mark]A variable holds the location in memory where a list of readings is stored, rather than the readings themselves. What is its data type?
[1 mark]What does this program print?
a = [1, 2] b = a b.append(3) c = list(a) c.append(4) print(a, len(c))
[1, 2, 3] 4
b refers to the same list as a, so a becomes [1, 2, 3]; c is a new copy, so appending to it leaves a alone.
[1 mark]A loop has its condition tested at the end. What is always true of it?
[1 mark]Which are advantages of using a named constant such as SAFE_CM instead of typing 25 wherever it is needed?
Tick every answer that is true.
[1 mark]A robot must repeat 'drive 5 cm' until the wall is 25 cm away. The number of repeats is not known in advance. What kind of iteration is this?
[1 mark]Python has no character type. What type does it use to store a single character such as "F"? Give the Python type name.
Define a record type Reading with namedtuple and three fields: heading (an integer, 0, 90, 180 or 270), cm (the real number distance() returns) and clear (a Boolean, True when cm is more than the constant CLEAR_CM). Declare CLEAR_CM = 40 as a constant and use the name, not 40, in the comparison.
The robot starts facing heading 0. Take a reading, store it in an array, turn right 90 degrees, and repeat until there are four readings and the robot faces its starting direction. Then print each record with print(r), which shows Reading(heading=90, cm=51.0, clear=True), and finally print clear directions: <n>, the number of records whose clear field is True.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
from collections import namedtuple
Reading = namedtuple("Reading", ["heading", "cm", "clear"])
survey = []
# take four readings, one every 90 degrees
print("clear directions:", 0)The hint students can ask for: Decide the three fields first and what type each one holds. Then one definite loop does the looking: read, build a record, add it to the array, turn a quarter. A second loop over the array does the counting.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
from collections import namedtuple
CLEAR_CM = 40
Reading = namedtuple("Reading", ["heading", "cm", "clear"])
survey = []
for i in range(4):
cm = distance()
survey.append(Reading(i * 90, cm, cm > CLEAR_CM))
turn_right(30, angle=90)
clear = 0
for r in survey:
print(r)
if r.clear:
clear = clear + 1
print("clear directions:", clear)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.