コード例 #1
0
 def candidates_test(self):
     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]]
     candidates = [[
         set(),
         set(), {1, 2, 4}, {2, 6},
         set(), {8, 2, 4, 6}, {8, 1, 4, 9}, {1, 2, 4, 9}, {8, 2, 4}
     ],
                   [
                       set(), {2, 4, 7}, {2, 4, 7},
                       set(),
                       set(),
                       set(), {8, 3, 4, 7}, {2, 3, 4}, {8, 2, 4, 7}
                   ],
                   [{1, 2},
                    set(),
                    set(), {2, 3}, {3, 4}, {2, 4}, {1, 3, 4, 5, 7},
                    set(), {2, 4, 7}],
                   [
                       set(), {1, 2, 5}, {1, 2, 5, 9}, {9, 5, 7},
                       set(), {1, 4, 7}, {9, 4, 5, 7}, {9, 2, 4, 5},
                       set()
                   ],
                   [
                       set(), {2, 5}, {9, 2, 5, 6},
                       set(), {5},
                       set(), {9, 5, 7}, {9, 2, 5},
                       set()
                   ],
                   [
                       set(), {1, 5}, {1, 3, 5, 9}, {9, 5},
                       set(), {1, 4}, {8, 9, 4, 5}, {9, 4, 5},
                       set()
                   ],
                   [{1, 3, 9},
                    set(), {1, 3, 4, 5, 7, 9}, {3, 5, 7}, {3, 5}, {7},
                    set(),
                    set(), {4}],
                   [{2, 3}, {8, 2, 7}, {2, 3, 7},
                    set(),
                    set(),
                    set(), {3, 6}, {3},
                    set()],
                   [{1, 2, 3}, {1, 2, 4, 5}, {1, 2, 3, 4, 5}, {2, 3, 5, 6},
                    set(), {2, 6}, {1, 3, 4, 6},
                    set(),
                    set()]]
     s = Sudoku(grid)
     self.assertTrue(grid_equal(candidates, s.candidates))
     # test uniques
     inds = [(0, j) for j in range(9)]  # no unique candidates
     #self.assertEqual(s.get_unique(inds), [])
     self.assertEqual(s.get_unique(inds, type=[1]), [])
     inds = [(7, j)
             for j in range(9)]  # uniques: 8 at (7, 1)  and 6 at (7, 6)
     #self.assertEqual(s.get_unique(inds), [([(7, 6)], [6]), ([(7, 1)], [8])] ) #[(6, (7, 6)), (8, (7, 1))])
     self.assertEqual(s.get_unique(inds, type=[1]), [([(7, 6)], [6]),
                                                     ([(7, 1)], [8])])
     inds = s.get_box_inds(5, 6)  # middle right box. unique 8 at (5,6)
     #self.assertEqual(s.get_unique(inds), [([(5, 6)], [8])])
     self.assertEqual(s.get_unique(inds, type=[1]), [([(5, 6)], [8])])
     # test erase function
     s.place_and_erase(0, 2, 1, constraint_prop=False)
     self.assertEqual(s.candidates[0], [
         set(),
         set(),
         set(), {2, 6},
         set(), {8, 2, 4, 6}, {8, 4, 9}, {2, 4, 9}, {8, 2, 4}
     ])
     col = [s.candidates[i][2] for i in range(9)]
     self.assertEqual(col, [
         set(), {2, 4, 7},
         set(), {2, 5, 9}, {2, 5, 6, 9}, {3, 5, 9}, {3, 4, 5, 7, 9},
         {2, 3, 7}, {2, 3, 4, 5}
     ])
コード例 #2
0
 def candidates_advanced(self):
     grid = [[0, 8, 0, 0, 0, 1, 2, 0, 6], [0, 0, 0, 0, 2, 0, 0, 0, 0],
             [0, 2, 0, 3, 0, 5, 0, 4, 0], [0, 6, 0, 0, 1, 0, 9, 0, 0],
             [0, 0, 2, 0, 5, 0, 4, 0, 0], [0, 0, 8, 0, 0, 0, 0, 1, 0],
             [0, 3, 0, 7, 0, 4, 0, 5, 0], [0, 0, 0, 0, 3, 0, 0, 0, 0],
             [4, 0, 6, 1, 0, 0, 0, 8,
              0]]  # June 7 Extreme 'https://www.sudokuwiki.org
     s = Sudoku(grid)
     candidates = [[{9, 3, 5, 7},
                    set(), {3, 4, 5, 7, 9}, {9, 4}, {9, 4, 7},
                    set(),
                    set(), {9, 3, 7},
                    set()],
                   [{1, 3, 5, 6, 7, 9}, {1, 4, 5, 7, 9}, {1, 3, 4, 5, 7, 9},
                    {8, 9, 4, 6},
                    set(), {8, 9, 6, 7}, {1, 3, 5, 7, 8}, {9, 3, 7},
                    {1, 3, 5, 7, 8, 9}],
                   [{1, 9, 6, 7},
                    set(), {1, 9, 7},
                    set(), {8, 9, 6, 7},
                    set(), {8, 1, 7},
                    set(), {8, 1, 9, 7}],
                   [{3, 5, 7},
                    set(), {3, 4, 5, 7}, {8, 2, 4},
                    set(), {8, 2, 3, 7},
                    set(), {2, 3, 7}, {2, 3, 5, 7, 8}],
                   [{1, 3, 9, 7}, {1, 9, 7},
                    set(), {8, 9, 6},
                    set(), {3, 6, 7, 8, 9},
                    set(), {3, 6, 7}, {8, 3, 7}],
                   [{9, 3, 5, 7}, {9, 4, 5, 7},
                    set(), {9, 2, 4, 6}, {9, 4, 6, 7}, {2, 3, 6, 7, 9},
                    {3, 5, 6, 7},
                    set(), {2, 3, 5, 7}],
                   [{8, 1, 2, 9},
                    set(), {1, 9},
                    set(), {8, 9, 6},
                    set(), {1, 6},
                    set(), {1, 2, 9}],
                   [{1, 2, 5, 7, 8, 9}, {1, 5, 9, 7}, {1, 5, 9, 7},
                    {2, 5, 6, 8, 9},
                    set(), {8, 9, 2, 6}, {1, 6, 7}, {9, 2, 6, 7},
                    {1, 2, 4, 7, 9}],
                   [
                       set(), {9, 5, 7},
                       set(),
                       set(), {9}, {9, 2}, {3, 7},
                       set(), {9, 2, 3, 7}
                   ]]
     self.assertTrue(grid_equal(candidates, s.candidates))
     inds = [(i, 0) for i in range(9)]
     #self.assertEqual(s.get_pairs(inds), [([(6, 0), (7, 0)], (2, 8))])
     uniques = s.get_unique(inds, type=[2])[0]
     uniques[0].sort()
     self.assertEqual(uniques, ([(6, 0), (7, 0)], (2, 8)))
     grid = [[0, 0, 0, 0, 0, 1, 0, 3, 0], [2, 3, 1, 0, 9, 0, 0, 0, 0],
             [0, 6, 5, 0, 0, 3, 1, 0, 0], [6, 7, 8, 9, 2, 4, 3, 0, 0],
             [1, 0, 3, 0, 5, 0, 0, 0, 6], [0, 0, 0, 1, 3, 6, 7, 0, 0],
             [0, 0, 9, 3, 6, 0, 5, 7, 0], [0, 0, 6, 0, 1, 9, 8, 4, 3],
             [3, 0, 0, 0, 0, 0, 0, 0,
              0]]  # https://www.sudokuwiki.org/Hidden_Candidates#HP
     s = Sudoku(grid)
     inds = [(0, j) for j in range(9)]
     uniques = s.get_unique(inds, type=[3])[0]
     uniques[0].sort()
     self.assertEqual(uniques, ([(0, 3), (0, 6), (0, 8)], (2, 5, 6)))
     s.candidates[0][8] = {2, 5}
     inds = [(i, 8) for i in range(9)]
     uniques = s.get_unique(inds, type=[3])[0]
     uniques[0].sort()
     self.assertEqual(uniques, ([(1, 8), (2, 8), (5, 8)], (4, 8, 7)))