1
+ from algorithms .dfs import (
2
+ get_factors , get_factors_iterative1 , get_factors_iterative2 ,
3
+ num_islands ,
4
+ pacific_atlantic ,
5
+ Sudoku ,
6
+ walls_and_gates
7
+ )
8
+
9
+ import unittest
10
+
11
+
12
+ class TestAllFactors (unittest .TestCase ):
13
+ def test_get_factors (self ):
14
+ self .assertEqual ([[2 , 16 ], [2 , 2 , 8 ], [2 , 2 , 2 , 4 ], [2 , 2 , 2 , 2 , 2 ], [2 , 4 , 4 ], [4 , 8 ]],
15
+ get_factors (32 ))
16
+ def test_get_factors_iterative1 (self ):
17
+ self .assertEqual ([[2 , 16 ], [4 , 8 ], [2 , 2 , 8 ], [2 , 4 , 4 ], [2 , 2 , 2 , 4 ], [2 , 2 , 2 , 2 , 2 ]],
18
+ get_factors_iterative1 (32 ))
19
+ def test_get_factors_iterative2 (self ):
20
+ self .assertEqual ([[2 , 2 , 2 , 2 , 2 ], [2 , 2 , 2 , 4 ], [2 , 2 , 8 ], [2 , 4 , 4 ], [2 , 16 ], [4 , 8 ]],
21
+ get_factors_iterative2 (32 ))
22
+
23
+
24
+ class TestCountIslands (unittest .TestCase ):
25
+ def test_num_islands (self ):
26
+ 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 ]]))
27
+ 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 ]]))
28
+
29
+
30
+ class TestPacificAtlantic (unittest .TestCase ):
31
+ def test_pacific_atlantic (self ):
32
+ self .assertEqual ([[0 , 4 ], [1 , 3 ], [1 , 4 ], [2 , 2 ], [3 , 0 ], [3 , 1 ], [4 , 0 ]],
33
+ 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 ]]))
34
+
35
+
36
+ class TestSudoku (unittest .TestCase ):
37
+ def test_sudoku_solver (self ):
38
+ board = [["5" ,"3" ,"." ], ["6" ,"." , "." ],["." ,"9" ,"8" ]]
39
+ test_obj = Sudoku (board , 3 , 3 )
40
+ test_obj .solve ()
41
+ self .assertEqual ([['5' , '3' , '1' ], ['6' , '1' , '2' ], ['1' , '9' , '8' ]],test_obj .board )
42
+
43
+
44
+ class TestWallsAndGates (unittest .TestCase ):
45
+ def test_walls_and_gates (self ):
46
+ rooms = [[float ("inf" ), - 1 , 0 , float ("inf" )],
47
+ [float ("inf" ), float ("inf" ), float ("inf" ), - 1 ],
48
+ [float ("inf" ), - 1 , float ("inf" ), - 1 ],
49
+ [0 , - 1 , float ("inf" ), float ("inf" )]]
50
+ walls_and_gates (rooms )
51
+ self .assertEqual ([[3 , - 1 , 0 , 1 ], [2 , 2 , 1 , - 1 ], [1 , - 1 , 2 , - 1 ], [0 , - 1 , 3 , 4 ]], rooms )
52
+
53
+
54
+ if __name__ == "__main__" :
55
+ unittest .main ()
0 commit comments