QQuadPollen
Workflows

Coding workflows · Binary-grid quadtree encoding

03
Built around your work

A concrete starting point

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.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Binary-grid quadtree encoding

A concrete starting point
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.

AI coding workspace

Your next change starts here.

Get the Windows app ↗