Lesson 30: Car park map
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
A map in a list
- The robot drives over a small car park and writes down which spaces have a car.
- One row of spaces is a list:
[1, 0, 1, 0]. The whole car park is a list of lists, a 2D list (in the exam: a 2D array). - Then the program uses the map to find the busiest row.
Words for this lesson
| Word | Meaning |
|---|---|
| 2D list | a list of rows; each row is a list |
| row / column | across / down |
| index | the place number; Python starts at 0 |
| nested loop | a loop inside a loop |
English
Read a 2D list
from vexcode_vr import *
def main():
grid = [[1, 0, 1, 0],
[0, 1, 1, 0],
[1, 1, 1, 1]]
brain.print(grid[1])
brain.new_line()
brain.print(grid[2][3])
vr_thread(main)
- The brain prints
[0, 1, 1, 0](the second row), then1(row 2, column 3, counted from 0).
| row ↓ column → | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 0 | 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 1 | 0 |
| 2 | 1 | 1 | 1 | 1 |
English
In the exam
- 0478/22/F/M/23 Q11: a 2D array holds the points of each team in each match. The program must add up the points for each team and output the team with the highest total. It is the same plan as your second program: an outer loop for each row (team), an inner loop along the row that adds up its values (totalling), and a check that keeps the highest total so far (maximum).
- In pseudocode, Cambridge arrays usually start at index 1, so the first row is row 1, not row 0.
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
A small car park has 3 rows of 4 spaces, 200 mm apart. A red tile is a parked car. The program already drives along each row and back. Store what the down eye sees in a 2D list: each row is a list with 1 for a car and 0 for an empty space, and grid is the list of the 3 rows. At the end the program prints each row.
点击“运行”查看此处输出。
难度 5/5 · 今日首次完成可获 400 金币及少量额外奖励
Now use the grid. For each row, add up its 4 values to count the cars (a loop inside a loop). Keep the row with the most cars and print Busiest row: and its number, where the first row is 1, for example Busiest row: 2.
点击“运行”查看此处输出。