Add index_* properties for returning ranges of indexes
This commit is contained in:
parent
01428e3972
commit
5ebe1f58ff
2 changed files with 37 additions and 18 deletions
46
sudoku.py
46
sudoku.py
|
@ -22,31 +22,49 @@ class Sudoku:
|
|||
|
||||
@property
|
||||
def rows(self):
|
||||
sz = self.size
|
||||
return [range(i * sz, i * sz + sz) for i in range(sz)]
|
||||
return self._apply_index_range_list(self.index_rows)
|
||||
|
||||
@property
|
||||
def columns(self):
|
||||
sz = self.size
|
||||
sz2 = sz ** 2
|
||||
return [range(i, sz2, sz) for i in range(sz)]
|
||||
return self._apply_index_range_list(self.index_columns)
|
||||
|
||||
@property
|
||||
def squares(self):
|
||||
dim = self.dimension
|
||||
return [self.square(x, y) for y in range(dim) for x in range(dim)]
|
||||
return self._apply_index_range_list(self.index_squares)
|
||||
|
||||
def _apply_index_range_list(self, ranges):
|
||||
return (self._apply_index_range(r) for r in ranges)
|
||||
|
||||
def _apply_index_range(self, rng):
|
||||
return (self.board[i] for i in rng)
|
||||
|
||||
@property
|
||||
def solved(self):
|
||||
def _check_group(group):
|
||||
values = sorted([self.board[i] for i in group])
|
||||
is_complete = values == list(range(1, self.size+1))
|
||||
return is_complete
|
||||
def index_rows(self):
|
||||
'''
|
||||
Return a list of ranges of indexes into the board, each
|
||||
defining a row.
|
||||
'''
|
||||
sz = self.size
|
||||
return (range(i * sz, i * sz + sz) for i in range(sz))
|
||||
|
||||
@property
|
||||
def index_columns(self):
|
||||
'''
|
||||
Return a list of ranges of indexes into the board, each
|
||||
defining a column.
|
||||
'''
|
||||
sz = self.size
|
||||
sz2 = sz ** 2
|
||||
dim = int(math.sqrt(self.size))
|
||||
# TODO: WIP
|
||||
return (range(i, sz2, sz) for i in range(sz))
|
||||
|
||||
@property
|
||||
def index_squares(self):
|
||||
'''
|
||||
Return a list of ranges of indexes into the board, each
|
||||
defining a square.
|
||||
'''
|
||||
dim = self.dimension
|
||||
return (self.square(x, y) for y in range(dim) for x in range(dim))
|
||||
|
||||
def square(self, x, y):
|
||||
dim = self.dimension
|
||||
|
|
9
test.py
9
test.py
|
@ -7,10 +7,11 @@ Unit tests for the Sudoku module.
|
|||
import unittest
|
||||
import sudoku
|
||||
|
||||
class Sudoku4Tests(unittest.TestCase):
|
||||
class Sudoku4TestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.board = sudoku.Sudoku(size=4)
|
||||
|
||||
class Sudoku4BasicTests(Sudoku4TestCase):
|
||||
def test_that_board_is_sane(self):
|
||||
self.assertEqual(self.board.size, 4)
|
||||
self.assertEqual(len(self.board.board), 4**2)
|
||||
|
@ -23,7 +24,7 @@ class Sudoku4Tests(unittest.TestCase):
|
|||
[ 8, 9, 10, 11],
|
||||
[12, 13, 14, 15]
|
||||
]
|
||||
for (row, exrow) in zip(self.board.rows, expected_rows):
|
||||
for (row, exrow) in zip(self.board.index_rows, expected_rows):
|
||||
row_list = list(row)
|
||||
with self.subTest(row=row_list, ex=exrow):
|
||||
self.assertEqual(row_list, exrow)
|
||||
|
@ -35,7 +36,7 @@ class Sudoku4Tests(unittest.TestCase):
|
|||
[2, 6, 10, 14],
|
||||
[3, 7, 11, 15]
|
||||
]
|
||||
for (col, excol) in zip(self.board.columns, expected_columns):
|
||||
for (col, excol) in zip(self.board.index_columns, expected_columns):
|
||||
col_list = list(col)
|
||||
with self.subTest(col=col_list, ex=excol):
|
||||
self.assertEqual(col_list, excol)
|
||||
|
@ -47,7 +48,7 @@ class Sudoku4Tests(unittest.TestCase):
|
|||
[ 8, 9, 12, 13],
|
||||
[10, 11, 14, 15]
|
||||
]
|
||||
for (sq, exsq) in zip(self.board.squares, expected_squares):
|
||||
for (sq, exsq) in zip(self.board.index_squares, expected_squares):
|
||||
sq_list = list(sq)
|
||||
with self.subTest(sq=sq_list, ex=exsq):
|
||||
self.assertEqual(sq_list, exsq)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue