def test_naked_twins(self): # given """ 5 367 9 | 478 2 47 | 367 1 468 4 1237 23 | 178 5 6 | 2379 237 89 8 1267 26 | 9 14 3 | 267 2467 5 ---------------+---------------+--------------- 39 8 7 | 134 1349 2 | 5 36 16 6 5 4 | 137 139 179 | 13 8 2 23 23 1 | 5 6 8 | 4 9 7 ---------------+---------------+--------------- 1 369 8 | 2 349 5 | 679 467 469 7 29 25 | 6 8 149 | 129 245 3 239 4 2356| 13 7 19 | 8 256 169 """ t_values = solution.eliminate( solution.grid_values( '5.9.2..1.4...56...8..9.3..5.87..25..654....82..15684971.82.5...7..68...3.4..7.8..' )) # when result = solution.naked_twins(t_values) self.assertEqual('9', result['D1'])
def test_eliminate1(self): before = generate_empty_board() before['A1'] = '4' expected = generate_empty_board() expected['A1'] = '4' for b in solution.peers.get('A1'): expected[b] = '12356789' display(before) display(expected) actual = solution.eliminate(before) self.assertEqual(actual,expected)
def test_eliminate(self): # given t_values = solution.grid_values( '..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..' ) # when result = solution.eliminate(t_values) # then self.assertEquals('45', result['A1']) self.assertEquals('9', result['B1']) self.assertEquals('9', result['G6']) self.assertEquals('1', result['I5']) self.assertEquals('34569', result['E5']) self.assertEquals('24678', result['I8']) # print print() solution.display(result)
def test_only_choice(self): # given t_values = solution.eliminate( solution.grid_values( '..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..' )) # when print() lines = solution.display(solution.only_choice(t_values)) # then self.assertEquals( ' 345 345 8 | 1 3456 2 | 9 34567 34567 ', lines[4]) self.assertEquals( ' 7 2 9 | 5 34569 4 | 1 13456 8 ', lines[5]) self.assertEquals( ' 1345 13459 6 | 7 3459 8 | 2 1345 345 ', lines[6])
class TestXWing(unittest.TestCase): before_x_wing_1 = solution.eliminate( solution.grid_values( '.....7..6...2..5....613.79.63194..5729.76..8374.325169..261...8..3.796.59.78..3.4' )) possible_solutions_1 = [{ 'A1': '1358', 'A2': '1258', 'A3': '4589', 'A4': '45', 'A5': '89', 'A6': '7', 'A7': '248', 'A8': '1234', 'A9': '6', 'B1': '138', 'B2': '178', 'B3': '489', 'B4': '2', 'B5': '89', 'B6': '68', 'B7': '5', 'B8': '134', 'B9': '1', 'C1': '458', 'C2': '258', 'C3': '6', 'C4': '1', 'C5': '3', 'C6': '48', 'C7': '7', 'C8': '9', 'C9': '2', 'D1': '6', 'D2': '3', 'D3': '1', 'D4': '9', 'D5': '4', 'D6': '8', 'D7': '2', 'D8': '5', 'D9': '7', 'E1': '2', 'E2': '9', 'E3': '5', 'E4': '7', 'E5': '6', 'E6': '1', 'E7': '4', 'E8': '8', 'E9': '3', 'F1': '7', 'F2': '4', 'F3': '8', 'F4': '3', 'F5': '2', 'F6': '5', 'F7': '1', 'F8': '6', 'F9': '9', 'G1': '45', 'G2': '5', 'G3': '2', 'G4': '6', 'G5': '1', 'G6': '34', 'G7': '9', 'G8': '7', 'G9': '8', 'H1': '18', 'H2': '18', 'H3': '3', 'H4': '4', 'H5': '7', 'H6': '9', 'H7': '6', 'H8': '12', 'H9': '5', 'I1': '9', 'I2': '16', 'I3': '7', 'I4': '8', 'I5': '5', 'I6': '2', 'I7': '3', 'I8': '12', 'I9': '4' }] def test_x_wing(self): discarded_val = '4' discarded_boxes = solution.cross('ABDEFHI', '16') sol = solution.x_wing(self.before_x_wing_1) for box in discarded_boxes: self.assertTrue(discarded_val not in sol[box])
def test_eliminate(self): puzzle = { 'A1': '2', 'A2': '123456789', 'A3': '123456789', 'A4': '123456789', 'A5': '123456789', 'A6': '123456789', 'A7': '123456789', 'A8': '123456789', 'A9': '123456789', 'B1': '123456789', 'B2': '123456789', 'B3': '123456789', 'B4': '123456789', 'B5': '123456789', 'B6': '6', 'B7': '2', 'B8': '123456789', 'B9': '123456789', 'C1': '123456789', 'C2': '123456789', 'C3': '1', 'C4': '123456789', 'C5': '123456789', 'C6': '123456789', 'C7': '123456789', 'C8': '7', 'C9': '123456789', 'D1': '123456789', 'D2': '123456789', 'D3': '6', 'D4': '123456789', 'D5': '123456789', 'D6': '8', 'D7': '123456789', 'D8': '123456789', 'D9': '123456789', 'E1': '3', 'E2': '123456789', 'E3': '123456789', 'E4': '123456789', 'E5': '9', 'E6': '123456789', 'E7': '123456789', 'E8': '123456789', 'E9': '7', 'F1': '123456789', 'F2': '123456789', 'F3': '123456789', 'F4': '6', 'F5': '123456789', 'F6': '123456789', 'F7': '4', 'F8': '123456789', 'F9': '123456789', 'G1': '123456789', 'G2': '4', 'G3': '123456789', 'G4': '123456789', 'G5': '123456789', 'G6': '123456789', 'G7': '8', 'G8': '123456789', 'G9': '123456789', 'H1': '123456789', 'H2': '123456789', 'H3': '5', 'H4': '2', 'H5': '123456789', 'H6': '123456789', 'H7': '123456789', 'H8': '123456789', 'H9': '123456789', 'I1': '123456789', 'I2': '123456789', 'I3': '123456789', 'I4': '123456789', 'I5': '123456789', 'I6': '123456789', 'I7': '123456789', 'I8': '123456789', 'I9': '3' } expected = { 'A1': '2', 'A2': '356789', 'A3': '34789', 'A4': '1345789', 'A5': '134578', 'A6': '134579', 'A7': '13569', 'A8': '1345689', 'A9': '145', 'B1': '45789', 'B2': '57', 'B3': '34789', 'B4': '1345789', 'B5': '134578', 'B6': '6', 'B7': '2', 'B8': '1345', 'B9': '14589', 'C1': '45689', 'C2': '35689', 'C3': '1', 'C4': '34589', 'C5': '23458', 'C6': '23459', 'C7': '35', 'C8': '7', 'C9': '45689', 'D1': '14579', 'D2': '12579', 'D3': '6', 'D4': '457', 'D5': '123457', 'D6': '8', 'D7': '1359', 'D8': '12359', 'D9': '1259', 'E1': '3', 'E2': '1258', 'E3': '248', 'E4': '145', 'E5': '9', 'E6': '1245', 'E7': '156', 'E8': '12568', 'E9': '7', 'F1': '15789', 'F2': '125789', 'F3': '2789', 'F4': '6', 'F5': '12357', 'F6': '57', 'F7': '4', 'F8': '123589', 'F9': '12589', 'G1': '1679', 'G2': '4', 'G3': '237', 'G4': '13579', 'G5': '13567', 'G6': '13579', 'G7': '8', 'G8': '12569', 'G9': '12569', 'H1': '16789', 'H2': '137', 'H3': '5', 'H4': '2', 'H5': '134678', 'H6': '13479', 'H7': '1679', 'H8': '46', 'H9': '1469', 'I1': '17', 'I2': '126789', 'I3': '2789', 'I4': '145789', 'I5': '145678', 'I6': '14579', 'I7': '15679', 'I8': '124569', 'I9': '3' } actual = solution.eliminate(puzzle) self.assertEqual(expected, actual)
def test_eliminate_1(self): self.assertTrue(solution.eliminate(self.before_eliminate_1), self.after_eliminate_1)
def test_eliminate(self): self.grid.update({"A1": "1", "A2": "123", "D1": "416"}) reduced_grid = solution.eliminate(self.grid) self.assertEqual(reduced_grid["A2"], "23") self.assertEqual(reduced_grid["D1"], "46")
def test_only_choice_001(self): grid = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3' values = grid_values(grid) values = eliminate(values) values = only_choice(values) # with the diagonal constraint self.assertEqual( { 'G7': '8', 'G6': '13579', 'G5': '13567', 'G4': '13579', 'G3': '2', 'G2': '4', 'G1': '1679', 'G9': '12569', 'G8': '12569', 'C9': '45689', 'C8': '7', 'C3': '1', 'C2': '35689', 'C1': '45689', 'C7': '35', 'C6': '23459', 'C5': '23458', 'C4': '34589', 'E5': '9', 'E4': '145', 'F1': '15789', 'F2': '125789', 'F3': '2789', 'F4': '6', 'F5': '12357', 'F6': '57', 'F7': '4', 'F8': '123589', 'F9': '12589', 'B4': '1345789', 'B5': '134578', 'B6': '6', 'B7': '2', 'B1': '45789', 'B2': '57', 'B3': '34789', 'B8': '1345', 'B9': '14589', 'I9': '3', 'I8': '124569', 'I1': '17', 'I3': '2789', 'I2': '126789', 'I5': '145678', 'I4': '145789', 'I7': '15679', 'I6': '14579', 'A1': '2', 'A3': '34789', 'A2': '356789', 'E9': '7', 'A4': '1345789', 'A7': '13569', 'A6': '134579', 'A9': '145', 'A8': '1345689', 'E7': '156', 'E6': '1245', 'E1': '3', 'E3': '248', 'E2': '1258', 'E8': '12568', 'A5': '134578', 'H8': '6', 'H9': '1469', 'H2': '137', 'H3': '5', 'H1': '16789', 'H6': '13479', 'H7': '1679', 'H4': '2', 'H5': '134678', 'D8': '12359', 'D9': '1259', 'D6': '8', 'D7': '1359', 'D4': '457', 'D5': '123457', 'D2': '12579', 'D3': '6', 'D1': '14579' }, values)
def test_eliminate_preserves_solved_values(self): eliminated_values = solution.eliminate(self.values.copy()) self.assertEqual(eliminated_values['A3'], '3') self.assertEqual(eliminated_values['D7'], '9') self.assertEqual(eliminated_values['H1'], '8')
def test_eliminate_square_peers(self): eliminated_values = solution.eliminate(self.values.copy()) self.assertTrue('1' not in eliminated_values['A1']) self.assertTrue('9' not in eliminated_values['A2']) self.assertTrue('3' not in eliminated_values['B2'])
def test_eliminates_vertical_peers(self): eliminated_values = solution.eliminate(self.values.copy()) self.assertTrue('9' not in eliminated_values['A1']) self.assertTrue('7' not in eliminated_values['A1']) self.assertTrue('8' not in eliminated_values['A1'])
def test_eliminates_horizontal_peers(self): eliminated_values = solution.eliminate(self.values.copy()) self.assertTrue('3' not in eliminated_values['A1']) self.assertTrue('2' not in eliminated_values['A1']) self.assertTrue('6' not in eliminated_values['A1'])