예제 #1
0
    def test_grid_with_missing_lines_exception_raised(self):
        invalid_input = """
+-------+-------+-------+
| 1     |       |       |
|   5   |     9 |       |
|       |       | 2     |
+-------+-------+-------+
|   1   |       |       |
|       |       |   1   |
| 3     |   7   |       |
+-------+-------+-------+
"""
        with self.assertRaises(InvalidInputError):
            PuzzleParser.read_from_string(invalid_input)
예제 #2
0
    def test_valid_input_with_leading_and_trailing_whitespace_proper_cell_values_returned(
            self):
        valid_input = """
            +-------+-------+-------+
            | 3     |     4 |   9   |   
            | 1     |   6 5 |       |
            |     7 |   2   |       |
            +-------+-------+-------+   
            |   8   |   1 3 |       |
            |     6 |   8   | 5     |
            |       | 5   6 |   4   |
            +-------+-------+-------+
            |       |   1   | 4 6   |
            |       | 6 9   |   2   |
            | 2 7   | 4     |     9 |
            +-------+-------+-------+
"""
        expected_cell_values = [
            [3, _, _, _, _, 4, _, 9, _],
            [1, _, _, _, 6, 5, _, _, _],
            [_, _, 7, _, 2, _, _, _, _],
            [_, 8, _, _, 1, 3, _, _, _],
            [_, _, 6, _, 8, _, 5, _, _],
            [_, _, _, 5, _, 6, _, 4, _],
            [_, _, _, _, 1, _, 4, 6, _],
            [_, _, _, 6, 9, _, _, 2, _],
            [2, 7, _, 4, _, _, _, _, 9],
        ]
        actual_cell_values = PuzzleParser.read_from_string(valid_input)
        self.assertEqual(expected_cell_values, actual_cell_values)
예제 #3
0
    def test_valid_input_with_proper_cell_values_returned(self):
        valid_input = """
+-------+-------+-------+
| 6     |     4 |   8 5 |
| 9 7   |   6 5 |       |
|   4 8 | 7 3   |       |
+-------+-------+-------+
|   8   | 2 4 7 |       |
|     6 |   8   | 5     |
|       | 1 5 6 |   4   |
+-------+-------+-------+
|       |   1 3 | 2 6   |
|       | 6 9   |   3 4 |
| 2 6   | 4     |     9 |
+-------+-------+-------+
"""
        expected_cell_values = [
            [6, _, _, _, _, 4, _, 8, 5],
            [9, 7, _, _, 6, 5, _, _, _],
            [_, 4, 8, 7, 3, _, _, _, _],
            [_, 8, _, 2, 4, 7, _, _, _],
            [_, _, 6, _, 8, _, 5, _, _],
            [_, _, _, 1, 5, 6, _, 4, _],
            [_, _, _, _, 1, 3, 2, 6, _],
            [_, _, _, 6, 9, _, _, 3, 4],
            [2, 6, _, 4, _, _, _, _, 9],
        ]
        actual_cell_values = PuzzleParser.read_from_string(valid_input)
        self.assertEqual(expected_cell_values, actual_cell_values)
예제 #4
0
    def test_letter_as_cell_value_exception_raised(self):
        invalid_input = """
+-------+-------+-------+
| 6     |     4 |   8 5 |
| 9 7   |   6 5 |       |
|   4 8 | 7 x   |       |
+-------+-------+-------+
|   8   | 2 4 7 |       |
|     6 |   8   | 5     |
|       | 1 5 6 |   4   |
+-------+-------+-------+
|       |   1 3 | 2 6   |
|       | 6 9   |   3 4 |
| 2 6   | 4     |     9 |
+-------+-------+-------+ 
"""
        with self.assertRaises(InvalidInputError):
            PuzzleParser.read_from_string(invalid_input)
예제 #5
0
    def test_invalid_border_exception_raised(self):
        invalid_input = """
+-------+-------+-------+
| 1     |       |       |
|   5   |     9 |       |
|       |       | 2     |
+-------+--
|   1   |       |       |
|       |       |   1   |
| 3     |   7   |       |
+-------+-------+-------+
|       |       | 6   4 |
|       |       | 8     |
|     4 |       |     3 |
+-------+-------+-------+ 
"""
        with self.assertRaises(InvalidInputError):
            PuzzleParser.read_from_string(invalid_input)
예제 #6
0
    def test_special_character_as_cell_value_exception_raised(self):
        invalid_input = """
+-------+-------+-------+
| 3     |       |   8 5 |
|   9   |   6 1 |       |
|   8 4 |   $   |       |
+-------+-------+-------+
|   4   | 2     |       |
|     6 |       | 5     |
|       | 1     |   4   |
+-------+-------+-------+
|       |   1   | 6     |
|       | 6     |   3   |
| 2 6   | 4     |     1 |
+-------+-------+-------+ 
"""
        with self.assertRaises(InvalidInputError):
            PuzzleParser.read_from_string(invalid_input)
예제 #7
0
 def test_empty_input_exception_raised(self):
     invalid_input = ""
     with self.assertRaises(InvalidInputError):
         PuzzleParser.read_from_string(invalid_input)
예제 #8
0
 def find_solution(puzzle, algorithm, timeout_sec = 10):
     cell_values = PuzzleParser.read_from_string(puzzle)
     search_summary = SearchEngine.find_solution(cell_values, algorithm, timeout_sec)
     formatted_final_grid = GridFormatter.write_to_string(search_summary.final_grid)
     return (search_summary, formatted_final_grid)