pydsa.grid¶
Grid helpers preloaded by sitecustomize: scan a grid, list a cell's in-bounds neighbours, test for and list the border cells, build a table by shape or by size, read a grid's shape, write one value or a stream of values into many cells, read or set a row or a column, multi-source BFS.
Grid ¶
Bases: list
A list of rows that also takes a (row, col) pair as an index.
Source code in pydsa/grid.mu
145 146 147 148 149 150 151 152 153 154 155 | |
155 156 157 158 159 160 161 162 163 164 165 | |
cells ¶
cells(
grid: Sequence[Sequence[Any]],
start: int = 0,
eq=None,
lt=None,
lte=None,
gt=None,
gte=None,
) -> Iterator[Cell]
Every (i, j) of grid with i >= start and j >= start, in row-major order. With eq, lt, lte, gt or gte, only the cells whose value passes them all (see _holds).
Source code in pydsa/grid.mu
21 22 23 24 25 26 27 28 29 30 | |
25 26 27 28 29 30 31 32 | |
nbrs ¶
nbrs(
grid: Sequence[Sequence[Any]],
r,
c: Optional[int] = None,
dirs: Sequence[Cell] = CARDINALS,
eq=None,
lt=None,
lte=None,
gt=None,
gte=None,
) -> Iterator[Cell]
The cells (r + dr, c + dc) for (dr, dc) in dirs that lie on grid. nbrs(grid, p) takes the cell as one pair. With eq, lt, lte, gt or gte, only the cells whose value passes them all (see _holds).
Source code in pydsa/grid.mu
32 33 34 35 36 37 38 39 40 41 42 43 44 | |
35 36 37 38 39 40 41 42 43 44 | |
in_bounds ¶
in_bounds(
grid: Sequence[Sequence[Any]],
r,
c: Optional[int] = None,
) -> bool
True if (r, c) is a cell of grid. in_bounds(grid, p) takes the cell as one pair.
Source code in pydsa/grid.mu
46 47 48 49 50 51 | |
47 48 49 50 51 52 | |
is_edge ¶
is_edge(
grid: Sequence[Sequence[Any]],
r,
c: Optional[int] = None,
) -> bool
True if (r, c) lies on the border of grid. is_edge(grid, p) takes the cell as one pair.
Source code in pydsa/grid.mu
53 54 55 56 57 58 | |
55 56 57 58 59 60 | |
edges ¶
edges(
grid: Sequence[Sequence[Any]],
eq=None,
lt=None,
lte=None,
gt=None,
gte=None,
) -> Iterator[Cell]
Every (i, j) on the border of grid, each once, in row-major order. With eq, lt, lte, gt or gte, only the cells whose value passes them all (see _holds).
Source code in pydsa/grid.mu
60 61 62 63 64 65 66 67 68 | |
63 64 65 66 67 68 69 | |
table ¶
table(*dims: int, fill=0) -> list
A new table of the given dimensions, every cell set to fill.
table(n) is a list of n cells; table(m, n) is m rows of n cells, the rows distinct lists; table(l, m, n) nests one level deeper.
Source code in pydsa/grid.mu
70 71 72 73 74 75 | |
72 73 74 75 76 77 | |
like ¶
like(grid: Sequence[Sequence[Any]], fill=0) -> list[list]
Source code in pydsa/grid.mu
77 78 | |
80 81 | |
shape ¶
shape(*seqs, last_index: bool = False) -> Tuple[int, ...]
The sizes of a nested list, or of several sequences side by side.
shape(grid) follows grid[0] down while it is a list: (rows, cols, ...). shape(a, b) is (len(a), len(b)). With last_index, every size less one: the index of the last cell.
Source code in pydsa/grid.mu
80 81 82 83 84 85 86 87 88 89 90 91 92 | |
84 85 86 87 88 89 90 91 92 93 94 95 96 | |
put ¶
put(grid: List[List[Any]], at: Iterable[Cell], v) -> None
Set grid[i][j] = v for every (i, j) in at.
v is one value, or an iterable read once per cell in the order of at: put(dp, cells(dp), accumulate(...)).
Source code in pydsa/grid.mu
98 99 100 101 102 103 104 | |
104 105 106 107 108 109 110 | |
row ¶
row(grid: Sequence[Sequence[Any]], i: int) -> list
A copy of row i.
Source code in pydsa/grid.mu
106 107 108 | |
113 114 115 | |
col ¶
col(grid: Sequence[Sequence[Any]], j: int) -> list
A copy of column j, top to bottom.
Source code in pydsa/grid.mu
110 111 112 | |
118 119 120 | |
set_row ¶
set_row(grid: List[List[Any]], i: int, v) -> None
Set every cell of row i to v, one value or an iterable read left to right.
Source code in pydsa/grid.mu
114 115 116 | |
123 124 125 | |
set_col ¶
set_col(grid: List[List[Any]], j: int, v) -> None
Set every cell of column j to v, one value or an iterable read top to bottom.
Source code in pydsa/grid.mu
118 119 120 | |
128 129 130 | |
grid_bfs ¶
grid_bfs(
grid: Sequence[Sequence[Any]],
sources: Sequence[Cell] | Cell,
dirs: Sequence[Cell] = CARDINALS,
eq=None,
lt=None,
lte=None,
gt=None,
gte=None,
) -> list[list[int]]
Steps from the nearest source to every cell; -1 where none reaches.
sources is a list of cells, or one cell. With eq, lt, lte, gt or gte, only the cells whose value passes them all are entered (see _holds). Sources are entered unconditionally at distance 0.
Source code in pydsa/grid.mu
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |