Open the context
Choose the project folder and name the files or behavior involved.
Encode a square binary grid of power-of-two width up to 16 as a quadtree with uniform leaves. Check the top-left, top-right, bottom-left and bottom-right child order, rejecting empty, ragged or non-binary grids without changing them.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
def encode_quadtree(grid):
grid = tuple(tuple(row) for row in grid)
n = len(grid)
if not 1 <= n <= 16 or n & (n - 1) or any(len(row) != n for row in grid):
raise ValueError("grid.shape")
if any(type(v) is not int or v not in (0, 1) for row in grid for v in row):
raise ValueError("cell.binary")
def visit(x, y, size):
value = grid[y][x]
if all(grid[r][c] == value for r in range(y, y + size) for c in range(x, x + size)):
return value
half = size // 2
return (visit(x, y, half), visit(x + half, y, half),
visit(x, y + half, half), visit(x + half, y + half, half))
return visit(0, 0, n)The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.