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
|
@property
|
||||||
def rows(self):
|
def rows(self):
|
||||||
sz = self.size
|
return self._apply_index_range_list(self.index_rows)
|
||||||
return [range(i * sz, i * sz + sz) for i in range(sz)]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def columns(self):
|
def columns(self):
|
||||||
sz = self.size
|
return self._apply_index_range_list(self.index_columns)
|
||||||
sz2 = sz ** 2
|
|
||||||
return [range(i, sz2, sz) for i in range(sz)]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def squares(self):
|
def squares(self):
|
||||||
dim = self.dimension
|
return self._apply_index_range_list(self.index_squares)
|
||||||
return [self.square(x, y) for y in range(dim) for x in range(dim)]
|
|
||||||
|
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
|
@property
|
||||||
def solved(self):
|
def index_rows(self):
|
||||||
def _check_group(group):
|
'''
|
||||||
values = sorted([self.board[i] for i in group])
|
Return a list of ranges of indexes into the board, each
|
||||||
is_complete = values == list(range(1, self.size+1))
|
defining a row.
|
||||||
return is_complete
|
'''
|
||||||
|
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
|
sz = self.size
|
||||||
sz2 = sz ** 2
|
sz2 = sz ** 2
|
||||||
dim = int(math.sqrt(self.size))
|
return (range(i, sz2, sz) for i in range(sz))
|
||||||
# TODO: WIP
|
|
||||||
|
@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):
|
def square(self, x, y):
|
||||||
dim = self.dimension
|
dim = self.dimension
|
||||||
|
|
9
test.py
9
test.py
|
@ -7,10 +7,11 @@ Unit tests for the Sudoku module.
|
||||||
import unittest
|
import unittest
|
||||||
import sudoku
|
import sudoku
|
||||||
|
|
||||||
class Sudoku4Tests(unittest.TestCase):
|
class Sudoku4TestCase(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.board = sudoku.Sudoku(size=4)
|
self.board = sudoku.Sudoku(size=4)
|
||||||
|
|
||||||
|
class Sudoku4BasicTests(Sudoku4TestCase):
|
||||||
def test_that_board_is_sane(self):
|
def test_that_board_is_sane(self):
|
||||||
self.assertEqual(self.board.size, 4)
|
self.assertEqual(self.board.size, 4)
|
||||||
self.assertEqual(len(self.board.board), 4**2)
|
self.assertEqual(len(self.board.board), 4**2)
|
||||||
|
@ -23,7 +24,7 @@ class Sudoku4Tests(unittest.TestCase):
|
||||||
[ 8, 9, 10, 11],
|
[ 8, 9, 10, 11],
|
||||||
[12, 13, 14, 15]
|
[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)
|
row_list = list(row)
|
||||||
with self.subTest(row=row_list, ex=exrow):
|
with self.subTest(row=row_list, ex=exrow):
|
||||||
self.assertEqual(row_list, exrow)
|
self.assertEqual(row_list, exrow)
|
||||||
|
@ -35,7 +36,7 @@ class Sudoku4Tests(unittest.TestCase):
|
||||||
[2, 6, 10, 14],
|
[2, 6, 10, 14],
|
||||||
[3, 7, 11, 15]
|
[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)
|
col_list = list(col)
|
||||||
with self.subTest(col=col_list, ex=excol):
|
with self.subTest(col=col_list, ex=excol):
|
||||||
self.assertEqual(col_list, excol)
|
self.assertEqual(col_list, excol)
|
||||||
|
@ -47,7 +48,7 @@ class Sudoku4Tests(unittest.TestCase):
|
||||||
[ 8, 9, 12, 13],
|
[ 8, 9, 12, 13],
|
||||||
[10, 11, 14, 15]
|
[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)
|
sq_list = list(sq)
|
||||||
with self.subTest(sq=sq_list, ex=exsq):
|
with self.subTest(sq=sq_list, ex=exsq):
|
||||||
self.assertEqual(sq_list, exsq)
|
self.assertEqual(sq_list, exsq)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue