Night 23: A map for your pet
Bạn đã hoàn thành đêm này bằng Python hoặc mã giả. Kết quả được tính cho cả hai. Bạn có thể luyện tập lại nếu muốn.
Tonight: a map for your pet
- Your pet's world needs a map: rows and columns, like a chess board.
- You will learn one thing: 2D arrays.
Words for tonight
| Word | Meaning |
|---|---|
| grid | rows and columns of boxes |
| row | a line from left to right |
| column | a line from top to bottom |
Two indexes
- A 2D array has a range for the rows and a range for the columns.
World[Row, Column]: first the row, then the column. Both start at 1.
DECLARE World : ARRAY[1:2, 1:3] OF STRING
World[1, 1] ← "tree"
World[1, 2] ← "grass"
World[1, 3] ← "pond"
World[2, 1] ← "grass"
World[2, 2] ← "home"
World[2, 3] ← "grass"
OUTPUT World[2, 2]
OUTPUT World[1, 3]

Fill and print the map
- Two loops can fill every place, and two more can print it.
DECLARE Grid : ARRAY[1:2, 1:3] OF STRING
DECLARE RowNumber, Col : INTEGER
DECLARE Row : STRING
FOR RowNumber ← 1 TO 2
FOR Col ← 1 TO 3
Grid[RowNumber, Col] ← "."
NEXT Col
NEXT RowNumber
Grid[2, 3] ← "P"
FOR RowNumber ← 1 TO 2
Row ← ""
FOR Col ← 1 TO 3
Row ← Row & Grid[RowNumber, Col]
NEXT Col
OUTPUT Row
NEXT RowNumber
Rows first
| Code | Means |
|---|---|
Grid[2, 3] |
row 2, column 3 |
Grid[3, 2] |
row 3, column 2: a different place! |
- In exams, the question always says which index is the row. Read it carefully.
- Now draw your pet's map!
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Here is your pet's world as a 2D array. Output the place at row 2, column 2, then the place at row 1, column 3. The output is home, then pond.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Put your pet P at row 3, column 1. Then output each row as one line, built with &. The last line is P...
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Count the trees T in the garden with a loop inside a loop. Output the count: 3.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 4/5 · 280 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Find where H (home) is. Use two loops and output its row and column: 2 3.
Nhấn Chạy để xem kết quả ở đây.