Is That Square Open?
Intermediate · Write the code · from Grid Robot Algorithms: MOVE_FORWARD, ROTATE and CAN_MOVE
From the lesson: Grid Robot Algorithms: MOVE_FORWARD, ROTATE and CAN_MOVE
Many exam questions show a robot on a grid and ask which code segment gets it to a goal square, using four commands from the reference sheet: MOVE_FORWARD(), ROTATE_LEFT(), ROTATE_RIGHT() and CAN_MOVE(direction). The small Python robot on this page runs any path you write, so you can check a trace instead of guessing.
Read the lessonWrite is_open(grid, row, col). grid is a list of strings, where "#" is a blocked square and anything else is open. Return True only if the square is inside the grid and not blocked. Squares off the edge count as not open.
is_open(["..#", "..."], 0, 2) -> False (blocked)
is_open(["..#", "..."], 1, 2) -> TrueCareful: in Python, grid[-1] does not raise an error. It quietly gives the last row.
What Check runs
Your code runs first, then each of these lines, and 3 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| is_open(["..#", "..."], 0, 2) | False |
| is_open(["..#", "..."], 1, 2) | True |
More Grid Robot exercises
- Which Way Now?Beginner
- Run a Robot ProgramAdvanced