From 5ebe1f58ffbecf3f8fecdbf96c518c44c244cb26 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 8 Oct 2017 07:56:23 -0700 Subject: [PATCH] Add index_* properties for returning ranges of indexes --- sudoku.py | 46 ++++++++++++++++++++++++++++++++-------------- test.py | 9 +++++---- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/sudoku.py b/sudoku.py index 8f05749..f664846 100644 --- a/sudoku.py +++ b/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 diff --git a/test.py b/test.py index 38805d6..ab827b4 100644 --- a/test.py +++ b/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)