Add peers and index_peers for a given (x,y)
This commit is contained in:
parent
19a9bc9d37
commit
00225aca44
1 changed files with 10 additions and 0 deletions
10
sudoku.py
10
sudoku.py
|
@ -60,6 +60,16 @@ class Sudoku:
|
|||
dim = self.dimension
|
||||
return (self._square(x, y, dim) for y in range(dim) for x in range(dim))
|
||||
|
||||
def peers(self, x, y):
|
||||
return {self._board[i] for i in self.index_peers(x, y) if self._board[i] != 0}
|
||||
|
||||
def index_peers(self, x, y):
|
||||
dim = self.dimension
|
||||
sz = self.size
|
||||
sz2 = sz ** 2
|
||||
sq_x, sq_y = int(x / dim), int(y / dim)
|
||||
return set(self._row(y, sz)) | set(self._column(x, sz, sz2)) | set(self._square(sq_x, sq_y, dim))
|
||||
|
||||
# TODO: Break the above into helper methods that produce a single thing given an index.
|
||||
def _row(self, r, size):
|
||||
return range(r * size, r * size + size)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue