def load_grid_181(self): """"Helper function to load a grid filled with values 1 to 81""" test_grid = Grid() expected_string = ','.join((str(x) for x in range(1,82))) test_grid.load_data(expected_string) return test_grid
def test_box_iterator(): assert isinstance(Grid.box_iterator(), Iterable) boxes = [r for r in Grid.box_iterator()] exp_in = [box_a1, box_d3, box_f7, box_j7] exp_out = [row_a1, col_d3, all_a1] for g in exp_in: assert g in boxes for g in exp_out: assert g not in boxes
def test_col_iterator(): assert isinstance(Grid.col_iterator(), Iterable) cols = [r for r in Grid.col_iterator()] exp_in = [col_a1, col_a4, col_d3, col_j7] exp_out = [row_a1, box_d3, all_a1] for g in exp_in: assert g in cols for g in exp_out: assert g not in cols
def test_row_iterator(): assert isinstance(Grid.row_iterator(), Iterable) rows = [r for r in Grid.row_iterator()] exp_in = [row_a1, row_c4, row_d3, row_j7] exp_out = [col_d3, box_d3, all_a1] for g in exp_in: assert g in rows for g in exp_out: assert g not in rows
def test_group_iterator(): assert isinstance(Grid.group_iterator(), Iterable) groups = [c for c in Grid.group_iterator()] for g in [ row_a1, row_c4, row_d3, row_j7, col_a1, col_a4, col_d3, col_j7, box_a1, box_d3, box_f7, box_j7 ]: assert g in groups assert len(groups) == 27
def test_grid_iterator(): assert isinstance(Grid.grid_iterator(), Iterable) act_grid = set([c for c in Grid.grid_iterator()]) exp_grid = { 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8', 'E9', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'J1', 'J2', 'J3', 'J4', 'J5', 'J6', 'J7', 'J8', 'J9' } assert act_grid == exp_grid
def valid_grid() -> Grid: return Grid([ [5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9], ])
def solved_grid() -> Grid: return Grid([ [2, 4, 9, 6, 5, 8, 3, 7, 1], [1, 3, 6, 2, 7, 9, 5, 8, 4], [7, 5, 8, 3, 1, 4, 2, 9, 6], [4, 8, 7, 9, 3, 5, 6, 1, 2], [3, 9, 2, 1, 6, 7, 4, 5, 8], [5, 6, 1, 4, 8, 2, 7, 3, 9], [9, 1, 3, 7, 4, 6, 8, 2, 5], [6, 2, 5, 8, 9, 3, 1, 4, 7], [8, 7, 4, 5, 2, 1, 9, 6, 3], ])
def test_cells_in_box(): assert Grid.cells_in_box(cell='A1') == box_a1 assert Grid.cells_in_box(cell='D3') == box_d3 assert Grid.cells_in_box(cell='J7') == box_j7 assert Grid.cells_in_box(cell='A1', inc=False) == box_a1 - {'A1'} assert Grid.cells_in_box(cell='D3', inc=False) == box_d3 - {'D3'} assert Grid.cells_in_box(cell='J7', inc=False) == box_j7 - {'J7'}
def test_cells_in_col(): assert Grid.cells_in_col(cell='A1') == col_a1 assert Grid.cells_in_col(cell='D3') == col_d3 assert Grid.cells_in_col(cell='J7') == col_j7 assert Grid.cells_in_col(cell='A1', inc=False) == col_a1 - {'A1'} assert Grid.cells_in_col(cell='D3', inc=False) == col_d3 - {'D3'} assert Grid.cells_in_col(cell='J7', inc=False) == col_j7 - {'J7'}
def test_cells_in_row(): assert Grid.cells_in_row(cell='A1') == row_a1 assert Grid.cells_in_row(cell='D3') == row_d3 assert Grid.cells_in_row(cell='J7') == row_j7 assert Grid.cells_in_row(cell='A1', inc=False) == row_a1 - {'A1'} assert Grid.cells_in_row(cell='D3', inc=False) == row_d3 - {'D3'} assert Grid.cells_in_row(cell='J7', inc=False) == row_j7 - {'J7'}
def test_all_neighbours(): assert Grid.all_neighbours(cell='A1') == all_a1 assert Grid.all_neighbours(cell='D3') == all_d3 assert Grid.all_neighbours(cell='J7') == all_j7 assert Grid.all_neighbours(cell='A1', inc=False) == all_a1 - {'A1'} assert Grid.all_neighbours(cell='D3', inc=False) == all_d3 - {'D3'} assert Grid.all_neighbours(cell='J7', inc=False) == all_j7 - {'J7'}
def __enter__(self): g = Grid(grid=self.grid) return g
def test_in_same_box(): assert Grid.in_same_box(group=['A1', 'A2', 'B3']) assert Grid.in_same_box(group=['A1', 'B3', 'C2']) assert Grid.in_same_box(group=['F3', 'F3', 'G6']) is False assert Grid.in_same_box(group=['F1', 'F2', 'F3', 'F4', 'F5']) is False assert Grid.in_same_box(group=['F1', 'F2', 'F3']) assert Grid.in_same_box(group={'G4', 'G5', 'E6'}) is False assert Grid.in_same_box(group={'A1', 'A5'}) is False assert Grid.in_same_box(group=['J8', 'H9', 'B7']) is False assert Grid.in_same_box(group=box_a1) assert Grid.in_same_box(group=row_j7) is False assert Grid.in_same_box(group=col_d3) is False assert Grid.in_same_box(group=all_d3) is False
] expert2 = [ [0, 0, 0, 0, 7, 0, 0, 0, 0], [0, 1, 0, 5, 0, 0, 8, 0, 0], [0, 0, 0, 0, 0, 1, 6, 4, 9], [0, 0, 8, 1, 0, 0, 0, 0, 0], [0, 0, 0, 3, 0, 0, 0, 0, 0], [0, 9, 0, 2, 0, 4, 0, 5, 3], [0, 0, 4, 0, 0, 0, 1, 0, 0], [0, 7, 3, 0, 0, 0, 9, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0, 0], ] def str_to_array(s): out = [] ints = map(int, s) for i in range(0, 9): out.append(ints[i * 9:i * 9 + 9]) return out expert3 = str_to_array( "000000000801000340006400800004210000000030002100600079000008000600000054900002007" ) generated = Grid(generate=True) generated.exclusive_subgroups() generated.manual_solve()