Recursion and computational thinking · A level · OCR H446 2.1.1, AQA 7517 4.4.1.4 · about 20 min
Information hiding, hiding the values, the method and the representation, and swapping a data type's insides.
[1 mark]What is information hiding?
[1 mark]print(30 * 20) is replaced by def area(width, height): return width * height. Which kind of abstraction is this step?
[1 mark]Two functions both return the smallest reading: one scans the list, one sorts it. A caller uses either without knowing which. What does this show?
[1 mark]A stack is used only through push, pop and is_empty. Inside, it could be an array with a top pointer or a linked list. What is this?
[1 mark]What does this program print?
def new_time(minutes, seconds):
return minutes * 60 + seconds
def add_seconds(t, s):
return t + s
def show(t):
return f"{t // 60}:{t % 60:02d}"
lap = add_seconds(new_time(2, 45), 30)
print(show(lap))[1 mark]What are benefits of hiding a component's implementation behind its interface?
Tick every answer that is true.
The robot stands in the middle of the mat, turns through eight 45 degree steps and logs a distance at each. The starter's log is a list, and the program at the bottom uses it only through five operations: new_log(), add_reading(log, cm), count(log), mean(log) and nearest(log).
Change the representation without changing the program at the bottom:
- a log must now be a dictionary holding only three figures: how many readings there have been, their running total, and the smallest reading so far;
- new_log() returns an empty log; add_reading(log, cm) updates the figures for a reading cm (a number of cm) and returns nothing; count(log) returns the number of readings; mean(log) returns the mean reading; nearest(log) returns the smallest reading;
- do not keep the readings, and do not use append, len or sum anywhere.
The program at the bottom must still print count 8, then mean <m> and nearest <n> with exactly the values the list version prints.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def new_log():
return []
def add_reading(log, cm):
log.append(cm)
def count(log):
return len(log)
def mean(log):
return sum(log) / len(log)
def nearest(log):
return min(log)
# the program that uses the log: do not change anything below this line
log = new_log()
for i in range(8):
add_reading(log, distance())
turn_right(40, angle=45)
print("count", count(log))
print("mean", round(mean(log), 1))
print("nearest", nearest(log))Plan your program here, then type it in and press Run.
farthest(log). What must change in the representation, and what must not change in the program at the bottom?sorted a procedural or a functional abstraction for someone who calls it? Justify your answer.