예제 #1
0
def test_elim():
    # elim 1 from 0, 1 and 0, 2
    assert eliminate([[{1, 2}, {1, 2}, {1, 2}, {1}], [{1}, {1}]], (0, 3), [(0, 1), (0, 2)]) == 2
    # elim 1 from every tuple except 0, 3 == location
    assert eliminate([[{1, 2}, {1, 2}, {1, 2}, {1}], [{1, 2}, {1, 2}]], (0, 3),
                     [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1)]) == 5
    # elim 3 - not in sets
    assert eliminate([[{1, 2}, {1}, {1}, {3}], [{1}, {1}]], (0, 3), [(0, 1), (0, 2)]) == 0
    # elim 2, tuple with 2 not in listOfLocations
    assert eliminate([[{1, 2}, {1}, {1}, {3}], [{1}, {2}]], (1, 1), [(0, 1), (0, 2)]) == 0
    # elim 2 with tuple in listOfLocations
    assert eliminate([[{1, 2}, {1}, {1}, {3}], [{1}, {2}]], (1, 1), [(0, 0), (0, 2)]) == 1
예제 #2
0
def testEliminate():
    sets = [[{1, 2}, {3}, {4}], [{1}, {3, 5, 7}, {2}], [{2, 3}, {2}, {1, 2,
                                                                      3}]]
    location = (1, 2)  # contains {2}
    count = sudoku.eliminate(sets, location, [(0, 0), (1, 0), (2, 2)])
    assert count == 2
    assert sets == [[{1}, {3}, {4}], [{1}, {3, 5, 7}, {2}],
                    [{2, 3}, {2}, {1, 3}]]
예제 #3
0
def testEliminate3():

    sets = [[{1, 2}, {3}, {4}], [{1}, {3, 5, 7}, {2}], [{2, 3}, {2}, {1, 2,
                                                                      3}]]
    location = (1, 2)  #contains 2
    count = sudoku.eliminate(sets, location, [(2, 0), (1, 1)])
    assert count == 1
    assert sets == [[{1, 2}, {3}, {4}], [{1}, {3, 5, 7}, {2}],
                    [{3}, {2}, {1, 2, 3}]]
예제 #4
0
 def test_eliminate(self):
     loc0 = (0,1)
     eliminate = sudoku.read_sudoku("./data/eliminateTest.txt")    #read the eliminate result
     problem = sudoku.read_sudoku("./data/data1.txt")  #read the puzzle
     problemSets = sudoku.convertToSets(problem)
     listOfLocation = sudoku.getRowLocations(loc0[0]) + sudoku.getColumnLocations(loc0[1]) \
                         + sudoku.getBoxLocations(loc0) 
     self.assertEqual(12, sudoku.eliminate(problemSets, loc0, listOfLocation))
     self.assertEqual(eliminate, problemSets)
예제 #5
0
def testEliminate2():
    sets = [[{4}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {3},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {7}, {1, 2, 3, 4, 5, 6, 7, 8, 9}],
            [{1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {9},
             {5}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {8}],
            [{1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {6}, {1, 2, 3, 4, 5, 6, 7, 8, 9},
             {8}, {4}, {1}, {3}],
            [{1, 2, 3, 4, 5, 6, 7, 8, 9}, {1}, {1, 2, 3, 4, 5, 6, 7, 8,
                                                9}, {9},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {3},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}],
            [{1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {5},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}],
            [{1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {4},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {6},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {8}, {1, 2, 3, 4, 5, 6, 7, 8, 9}],
            [{7}, {9}, {2}, {8}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {5},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}],
            [{3}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {5}, {4},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {9},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}],
            [{1, 2, 3, 4, 5, 6, 7, 8, 9}, {4}, {1, 2, 3, 4, 5, 6, 7, 8,
                                                9}, {2},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {8},
             {1, 2, 3, 4, 5, 6, 7, 8, 9}, {5}]]

    location = (0, 0)
    count = sudoku.eliminate(sets, location, [(0, 0), (0, 1), (0, 2), (0, 3),
                                              (0, 4), (0, 5), (0, 6), (0, 7),
                                              (0, 8)])
    assert count == 6
    assert sets[location[0]] == [{4}, {1, 2, 3, 5, 6, 7, 8, 9},
                                 {1, 2, 3, 5, 6, 7, 8, 9},
                                 {1, 2, 3, 5, 6, 7, 8, 9},
                                 {1, 2, 3, 5, 6, 7, 8, 9}, {3},
                                 {1, 2, 3, 5, 6, 7, 8, 9}, {7},
                                 {1, 2, 3, 5, 6, 7, 8, 9}]
예제 #6
0
 def reduce_puzzle(self, values, naked=True):
     """
     Iterate eliminate() and only_choice(). If at some point, there is a box with no available values, return False.
     If the sudoku is solved, return the sudoku.
     If after an iteration of both functions, the sudoku remains the same, return the sudoku.
     Input: A sudoku in dictionary form.
     Output: The resulting sudoku in dictionary form.
     """
     stalled = False
     while not stalled:
         solved_values_before = len(
             [box for box in values.keys() if len(values[box]) == 1])
         values = s.eliminate(values)
         values = s.only_choice(values)
         if naked is True:
             values = s.naked_twins(values)
         solved_values_after = len(
             [box for box in values.keys() if len(values[box]) == 1])
         stalled = solved_values_before == solved_values_after
         if len([box for box in values.keys() if len(values[box]) == 0]):
             return False
     return values