예제 #1
0
    def test_world_hardest_sudoku(self):

        # given See http://www.telegraph.co.uk/news/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html
        """
        8      1246   24569  |  2347   12357    1234  | 13569    4579  1345679
        12459    124      3    |   6     12578    1248  |  1589   45789   14579
        1456     7      456   |  348      9      1348  |   2      458    13456
        ------------------------+------------------------+------------------------
        123469    5      2469  |  2389    2368     7    |  1689    2489   12469
        12369   12368    269   |  2389     4       5    |   7      289     1269
        24679    2468   24679  |   1      268     2689  |  5689     3     24569
        ------------------------+------------------------+------------------------
        23457    234      1    | 23479    237     2349  |  359      6       8
        23467    2346     8    |   5      2367   23469  |   39      1      2379
        23567     9      2567  |  2378   123678  12368  |   4      257     2357
        """
        grid = '8..........36......7..9.2...5...7.......457.....1...3...1....68..85...1..9....4..'

        # when
        values = solution.solve(grid)

        # then
        self.assertTrue(solution.is_solved_correctly(values))

        print()
        solution.display(values)
예제 #2
0
 def test_naked_twins(self):
     # solution.display(self.before_naked_twins_1)
     solution.display(self.possible_solutions_1[1])
     self.assertTrue(
         solution.naked_twins(self.before_naked_twins_1)
         in self.possible_solutions_1,
         "Your naked_twins function produced an unexpected board.")
예제 #3
0
 def test_naked_twins(self):
     print("start testing","first nt")
     solved=solution.naked_twins(before_naked_twins)
     print("solved nt")
     solution.display(solved)
     print("ideal nt")
     solution.display(after_naked_twins)
     self.assertEqual(solved, after_naked_twins)
예제 #4
0
 def test_solve(self):
     print("anotehr test")
     print("solved sudoku")
     solved=solution.solve(diagonal_grid)
     solution.display(solved)
     print("ideal sudoku")
     solution.display(solved_diag_sudoku)
     self.assertEqual(solved, solved_diag_sudoku)
예제 #5
0
 def valid_answer(self, values):
     if values is False:
         return
     try:
         for unit in solution.units:
             self.assertEqual(''.join(sorted(values[box] for box in unit)), '123456789', "unit " + str(unit))
     except:
         solution.display(values)
         raise
예제 #6
0
    def test_solve_positive(self):
        # given
        grid = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'

        # when
        values = solution.solve(grid)

        # then
        self.assertTrue(solution.is_solved_correctly(values))

        solution.display(values)
예제 #7
0
    def time_solve(grid):
        start = time.clock()
        values = solution.solve(grid)
        t = time.clock() - start

        # Display puzzles that take long enough
        if showif is not None and t > showif:
            solution.display(solution.grid_values(grid))
            if values: solution.display(values)
            print('(%.2f seconds)\n' % t)

        return (t, solved(values))
예제 #8
0
    def test_reduce_puzzle(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
        values = solution.reduce_puzzle(t_values)

        # then
        self.assertTrue(solution.is_solved_correctly(values))

        print()
        solution.display(values)
예제 #9
0
    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)
예제 #10
0
 def test_solve_diagonal_4(self):
     diag_sudoku_grid_4 = '..7..5..2.......13.........9...8.7......7...5..2.......1..3.......54.......7....4'
     expected_solution_4 = '367415892295867413841329657953284761684173925172956348418632579739541286526798134'
     resp = solve(diag_sudoku_grid_4)
     if resp is not False:
         solution_4 = values_grid(resp)
         print('Expected: {}'.format(expected_solution_4))
         print('Solution: {}'.format(solution_4))
         print(display(grid_values(solution_4)))
         self.assertEquals(expected_solution_4, solution_4)
         self.assertTrue(is_valid(grid_values(solution_4)))
     else:
         print('Could not find a valid solution.')
예제 #11
0
    def test_solve(self):
        # Harder sudoko
        # given
        grid = '.....97..4..7...2...18...39.3....4...769.531...4....9.84...39...1...8..3..26.....'

        # when
        values = solution.solve(grid)

        # then
        #self.assertTrue(solution.is_solved_correctly(values))

        print()
        lines = solution.display(values)
예제 #12
0
    def test_search(self):
        # given
        t_values = solution.grid_values(
            '.....97..4..7...2...18...39.3....4...769.531...4....9.84...39...1...8..3..26.....'
        )

        # when
        values = solution.search(t_values)

        # then
        self.assertTrue(solution.is_solved_correctly(values))

        print()
        lines = solution.display(values)
예제 #13
0
 def test_solve(self):
     print("input:")
     solution.display(solution.grid_values(self.diagonal_grid))
     print("\nexpected:")
     solved = solution.solve(self.diagonal_grid)
     solution.display(self.solved_diag_sudoku)
     print("\nsolution:")
     solution.display(solved)
     self.assertEqual(solved, self.solved_diag_sudoku)
예제 #14
0
def display_naked_twins():
    solution.display(before_naked_twins_1)

    print()

    solution.display(possible_solutions_1[0])
    print()

    values = solution.naked_twins(before_naked_twins_1)
    solution.display(values)
예제 #15
0
    def test_display(self):
        # given
        t_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
        lines = solution.display(solution.grid_values(t_grid_values))

        # then
        self.assertEquals(
            '123456789 123456789     3     |123456789     2     123456789 |    6     123456789 123456789 ',
            lines[0])
        self.assertEquals(
            '123456789 123456789     8     |    1     123456789     2     |    9     123456789 123456789 ',
            lines[4])
        self.assertEquals(
            '123456789 123456789     5     |123456789     1     123456789 |    3     123456789 123456789 ',
            lines[10])
예제 #16
0
    def test_naked_twins(self):
        solution.display(self.before_naked_twins_1)

        for sol in self.possible_solutions_1:
            print(" \n \n \n ")
            solution.display(sol)

        twins = solution.naked_twins(self.before_naked_twins_1)
        print(" \n \n \n ")
        solution.display(twins)

        self.assertTrue(
            twins in self.possible_solutions_1,
            "Your naked_twins function produced an unexpected board.")
예제 #17
0
    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])
예제 #18
0
 def test_solve(self):
     solution.display(solution.grid_values(self.diagonal_grid2))
     solution.display(solution.solve(self.diagonal_grid2))
     self.assertEqual(solution.solve(self.diagonal_grid),
                      self.solved_diag_sudoku)
예제 #19
0
 def test_display_001(self):
     grid = "..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3.."
     display(grid_values(grid))
예제 #20
0
 def test_grid_values(self):
     grid = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
     solution.display(solution.grid_values(grid))
예제 #21
0
 def test_super_naked_twins(self):
     solution.display(self.before_super_naked_twins)
예제 #22
0
 def test_naked_twins3(self):
     solution.display(self.before_naked_twins_3)
     solution_3 = solution.naked_twins(self.before_naked_twins_3)
     print('')
     solution.display(solution_3)
예제 #23
0
 def test_naked_twins_submit(self):
     puzzle = {
         "G7": "2345678",
         "G6": "1236789",
         "G5": "23456789",
         "G4": "345678",
         "G3": "1234569",
         "G2": "12345678",
         "G1": "23456789",
         "G9": "24578",
         "G8": "345678",
         "C9": "124578",
         "C8": "3456789",
         "C3": "1234569",
         "C2": "1234568",
         "C1": "2345689",
         "C7": "2345678",
         "C6": "236789",
         "C5": "23456789",
         "C4": "345678",
         "E5": "678",
         "E4": "2",
         "F1": "1",
         "F2": "24",
         "F3": "24",
         "F4": "9",
         "F5": "37",
         "F6": "37",
         "F7": "58",
         "F8": "58",
         "F9": "6",
         "B4": "345678",
         "B5": "23456789",
         "B6": "236789",
         "B7": "2345678",
         "B1": "2345689",
         "B2": "1234568",
         "B3": "1234569",
         "B8": "3456789",
         "B9": "124578",
         "I9": "9",
         "I8": "345678",
         "I1": "2345678",
         "I3": "23456",
         "I2": "2345678",
         "I5": "2345678",
         "I4": "345678",
         "I7": "1",
         "I6": "23678",
         "A1": "2345689",
         "A3": "7",
         "A2": "234568",
         "E9": "3",
         "A4": "34568",
         "A7": "234568",
         "A6": "23689",
         "A9": "2458",
         "A8": "345689",
         "E7": "9",
         "E6": "4",
         "E1": "567",
         "E3": "56",
         "E2": "567",
         "E8": "1",
         "A5": "1",
         "H8": "345678",
         "H9": "24578",
         "H2": "12345678",
         "H3": "1234569",
         "H1": "23456789",
         "H6": "1236789",
         "H7": "2345678",
         "H4": "345678",
         "H5": "23456789",
         "D8": "2",
         "D9": "47",
         "D6": "5",
         "D7": "47",
         "D4": "1",
         "D5": "36",
         "D2": "9",
         "D3": "8",
         "D1": "36"
     }
     solution.display(puzzle)
     expected = {
         'G7': '2345678',
         'G6': '1236789',
         'G5': '23456789',
         'G4': '345678',
         'G3': '1234569',
         'G2': '12345678',
         'G1': '23456789',
         'G9': '24578',
         'G8': '345678',
         'C9': '124578',
         'C8': '3456789',
         'C3': '1234569',
         'C2': '1234568',
         'C1': '2345689',
         'C7': '2345678',
         'C6': '236789',
         'C5': '23456789',
         'C4': '345678',
         'E5': '68',
         'E4': '2',
         'F1': '1',
         'F2': '24',
         'F3': '24',
         'F4': '9',
         'F5': '37',
         'F6': '37',
         'F7': '58',
         'F8': '58',
         'F9': '6',
         'B4': '345678',
         'B5': '23456789',
         'B6': '236789',
         'B7': '2345678',
         'B1': '2345689',
         'B2': '1234568',
         'B3': '1234569',
         'B8': '3456789',
         'B9': '124578',
         'I9': '9',
         'I8': '345678',
         'I1': '2345678',
         'I3': '23456',
         'I2': '2345678',
         'I5': '2345678',
         'I4': '345678',
         'I7': '1',
         'I6': '23678',
         'A1': '2345689',
         'A3': '7',
         'A2': '234568',
         'E9': '3',
         'A4': '34568',
         'A7': '234568',
         'A6': '23689',
         'A9': '2458',
         'A8': '345689',
         'E7': '9',
         'E6': '4',
         'E1': '567',
         'E3': '56',
         'E2': '567',
         'E8': '1',
         'A5': '1',
         'H8': '345678',
         'H9': '24578',
         'H2': '12345678',
         'H3': '1234569',
         'H1': '23456789',
         'H6': '1236789',
         'H7': '2345678',
         'H4': '345678',
         'H5': '23456789',
         'D8': '2',
         'D9': '47',
         'D6': '5',
         'D7': '47',
         'D4': '1',
         'D5': '6',
         'D2': '9',
         'D3': '8',
         'D1': '36'
     }
     result = solution.naked_twins(puzzle)
     print("______________________\n")
     solution.display(result)
     self.assertEqual(expected, result)
예제 #24
0
 def test_try(self):
     values = solution.solve(self.grid)
     self.assertTrue(solution.is_solved(values))
     solution.display(values)
예제 #25
0
from solution import display
from solution_test import TestNakedTwins

if __name__ == '__main__':
    tnt = TestNakedTwins()
    display(tnt.before_naked_twins_1)
예제 #26
0
 def test_naked_twins3(self):
     print()
     solution.display(self.before_naked_twins_3)
     print()
     solution.display(solution.naked_twins(self.before_naked_twins_3))