-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_dfs.py
80 lines (63 loc) · 3.18 KB
/
test_dfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from algorithms.dfs import (
get_factors, get_factors_iterative1, get_factors_iterative2,
num_islands,
pacific_atlantic,
Sudoku,
walls_and_gates,
find_path
)
import unittest
class TestAllFactors(unittest.TestCase):
def test_get_factors(self):
self.assertEqual([[2, 16], [2, 2, 8], [2, 2, 2, 4], [2, 2, 2, 2, 2],
[2, 4, 4], [4, 8]], get_factors(32))
def test_get_factors_iterative1(self):
self.assertEqual([[2, 16], [4, 8], [2, 2, 8], [2, 4, 4], [2, 2, 2, 4],
[2, 2, 2, 2, 2]], get_factors_iterative1(32))
def test_get_factors_iterative2(self):
self.assertEqual([[2, 2, 2, 2, 2], [2, 2, 2, 4], [2, 2, 8], [2, 4, 4],
[2, 16], [4, 8]], get_factors_iterative2(32))
class TestCountIslands(unittest.TestCase):
def test_num_islands(self):
self.assertEqual(1, num_islands([[1, 1, 1, 1, 0], [1, 1, 0, 1, 0],
[1, 1, 0, 0, 0], [0, 0, 0, 0, 0]]))
self.assertEqual(3, num_islands([[1, 1, 0, 0, 0], [1, 1, 0, 0, 0],
[0, 0, 1, 0, 0], [0, 0, 0, 1, 1]]))
class TestPacificAtlantic(unittest.TestCase):
def test_pacific_atlantic(self):
self.assertEqual([[0, 4], [1, 3], [1, 4], [2, 2], [3, 0],
[3, 1], [4, 0]], pacific_atlantic([[1, 2, 2, 3, 5],
[3, 2, 3, 4, 4],
[2, 4, 5, 3, 1],
[6, 7, 1, 4, 5],
[5, 1, 1, 2, 4]]))
class TestSudoku(unittest.TestCase):
def test_sudoku_solver(self):
board = [["5", "3", "."], ["6", ".", "."], [".", "9", "8"]]
test_obj = Sudoku(board, 3, 3)
test_obj.solve()
self.assertEqual([['5', '3', '1'], ['6', '1', '2'],
['1', '9', '8']], test_obj.board)
class TestWallsAndGates(unittest.TestCase):
def test_walls_and_gates(self):
rooms = [[float("inf"), -1, 0, float("inf")],
[float("inf"), float("inf"), float("inf"), -1],
[float("inf"), -1, float("inf"), -1],
[0, -1, float("inf"), float("inf")]]
walls_and_gates(rooms)
self.assertEqual([[3, -1, 0, 1], [2, 2, 1, -1], [1, -1, 2, -1],
[0, -1, 3, 4]], rooms)
class TestMazeSearch(unittest.TestCase):
def test_maze_search(self):
maze_1 = [[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1,
1, 0, 1, 1, 1],
[1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
0, 1, 1, 1, 0, 1]]
self.assertEqual(37, find_path(maze_1))
maze_2 = [[1, 0, 1, 1, 1, 1], [1, 0, 1, 0, 1, 0],
[1, 0, 1, 0, 1, 1], [1, 1, 1, 0, 1, 1]]
self.assertEqual(14, find_path(maze_2))
maze_3 = [[1, 0, 0], [0, 1, 1], [0, 1, 1]]
self.assertEqual(-1, find_path(maze_3))
if __name__ == "__main__":
unittest.main()