Meta AI Coding -- Searh Grid
Requirements
Your task:
- Level 1: Print path on the grid. Given simple buggy code and fix.
- Level 2: BFS travese the grid. Also given buggy code and need to fix it.
- Level 3: Extend the grid to support two special cell types < and >.
- Level 4: ...
Level 1
You are given a list of coordinates representing a path on a 2D grid. Write a function that renders this path on the grid.
def print_path(coords: list[tuple[int, int]]) -> list[str]:
...
Example
input:
coords = [(0, 0), (0, 1), (1, 1), (2, 1)]
output:
S P .
. P .
. P F
Level 2
Given the same 2D grid and path-rendering rules, write a new function that uses BFS to find the shortest path between the start and end coordinates, then render that shortest path.
def bfs_path(grid: list[list[str]]) -> list[str]:
...
Example
input:
grid = [
["S", ".", ".", "."],
[".", ".", ".", "."],
[".", ".", ".", "E"]
]
output:
S P P .
. . P .
. . P F
Level 3
You are given a 2D grid and asked to find a valid path from a start cell to an end cell. There are two special directional cell types:
'<' : stepping into this cell immediately causes your next move to go one step left
'>' : stepping into this cell immediately causes your next move to go one step right
Write a function that searches for a valid path while correctly handling these directional behaviors and returns the final path as a list of coordinates.
def find_path(grid: list[list[str]]) -> list[tuple[int, int]]:
Example
Input:
grid = [
["S", ".", "." ,"."],
[".", "<", ".", "."],
[".", ".", ">", "E"]
]
Output:
[(0, 0), (0, 1), (1, 1), (1, 0), (2, 0), (2, 1), (2, 2), (2, 3)]
Level 4
...
Checkout our Meta Crash Course.
