forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bfs.py
55 lines (38 loc) · 1.8 KB
/
test_bfs.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
from algorithms.bfs import (
count_islands,
maze_search,
shortest_distance_from_all_buildings,
ladder_length
)
import unittest
class TestCountIslands(unittest.TestCase):
def test_count_islands(self):
grid_1 = [[1,1,1,1,0], [1,1,0,1,0], [1,1,0,0,0], [0,0,0,0,0]]
self.assertEqual(1, count_islands(grid_1))
grid_2 = [[1,1,0,0,0], [1,1,0,0,0], [0,0,1,0,0], [0,0,0,1,1]]
self.assertEqual(3, count_islands(grid_2))
grid_3 = [[1,1,1,0,0,0], [1,1,0,0,0,0], [1,0,0,0,0,1], [0,0,1,1,0,1], [0,0,1,1,0,0]]
self.assertEqual(3, count_islands(grid_3))
grid_4 = [[1,1,0,0,1,1], [0,0,1,1,0,0], [0,0,0,0,0,1], [1,1,1,1,0,0]]
self.assertEqual(5, count_islands(grid_4))
class TestMazeSearch(unittest.TestCase):
def test_maze_search(self):
grid_1 = [[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, maze_search(grid_1))
grid_2 = [[1,0,0],[0,1,1],[0,1,1]]
self.assertEqual(-1, maze_search(grid_2))
class TestWordLadder(unittest.TestCase):
def test_ladder_length(self):
# hit -> hot -> dot -> dog -> cog
self.assertEqual(5, ladder_length('hit', 'cog', ["hot", "dot", "dog", "lot", "log"]))
# pick -> sick -> sink -> sank -> tank == 5
self.assertEqual(5, ladder_length('pick', 'tank',
['tock', 'tick', 'sank', 'sink', 'sick']))
# live -> life == 1, no matter what is the word_list.
self.assertEqual(1, ladder_length('live', 'life', ['hoho', 'luck']))
# 0 length from ate -> ate
self.assertEqual(0, ladder_length('ate', 'ate', []))
# not possible to reach !
self.assertEqual(-1, ladder_length('rahul', 'coder', ['blahh', 'blhah']))
if __name__ == "__main__":
unittest.main()