Strings, lists and records · GCSE · OCR J277 2.2.3, AQA 8525 3.2.6, Edexcel 1CP2 6.3.1 · about 15 min
Rows and columns: the depth grid as a list of lists.
[1 mark]What does this program print?
board = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(board[1][2])
6
Row 1 is [4, 5, 6], and column 2 of it is 6.
[1 mark]What does this program print?
board = [[1, 2], [3, 4]]
total = 0
for row in board:
for item in row:
total = total + item
print(total)10
The two loops visit every item: 1 + 2 + 3 + 4.
[1 mark]A 2D array has 8 rows and 8 columns. How many items does it hold?
[1 mark]The depth grid comes as one flat list of 64 readings, row after row. Which slice is row 2?
flat[16:24]flat[2:10]flat[8:16]flat[16:32][1 mark]What does this program print?
grid = [[0, 0], [0, 0]] grid[1][0] = 5 print(grid)
[[0, 0], [5, 0]]
grid[1][0] is row 1, column 0.
[1 mark]In OCR's Exam Reference Language, how is row 3, column 4 of grid written?
grid[3, 4]grid[3][4]grid(3, 4)grid.3.4There is a box off to one side. Without moving, find the smallest reading in the level rows (2 and 3) of the depth grid and print nearest: <cm> cm in column <n>.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
flat = tof_grid()
best = min(flat)
print("nearest:", best)The hint students can ask for: The grid is rows of readings. Look along the row you care about, keeping both the smallest reading and the position it was found at.
from bugbot import *
connect()
grid = tof_grid()
best = 999
best_col = 0
for row in (2, 3): # the level rows; the rows below see the mat
for col in range(8):
d = grid[row * 8 + col]
if d < best:
best = d
best_col = col
print(f'nearest: {best} cm in column {best_col}')
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.