-
Notifications
You must be signed in to change notification settings - Fork 505
Expand file tree
/
Copy pathtest_backtracking.py
More file actions
193 lines (149 loc) · 6.39 KB
/
Copy pathtest_backtracking.py
File metadata and controls
193 lines (149 loc) · 6.39 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""
Test cases for backtracking algorithms
"""
import unittest
from pygorithm.backtracking import n_queens, sudoku_solver, maze_solver, permutations
class TestBacktracking(unittest.TestCase):
def test_n_queens(self):
"""Test N-Queens algorithm"""
# Test 4-Queens (should have 2 solutions)
solutions = n_queens.solve_n_queens(4)
self.assertEqual(len(solutions), 2)
# Test first solution for 4-Queens
first_solution = n_queens.solve_n_queens_first_solution(4)
self.assertIsNotNone(first_solution)
self.assertEqual(len(first_solution), 4)
# Test invalid input
self.assertEqual(n_queens.solve_n_queens(0), [])
self.assertEqual(n_queens.solve_n_queens(-1), [])
def test_sudoku_solver(self):
"""Test Sudoku solver"""
# Create a simple sudoku puzzle
board = [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9]
]
# Test if solver can solve it
result = sudoku_solver.solve_sudoku(board)
self.assertTrue(result)
# Test validation
self.assertTrue(sudoku_solver.is_valid_sudoku(board))
def test_maze_solver(self):
"""Test maze solver"""
# Create a simple maze
maze = [
[0, 1, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 1, 0],
[1, 1, 0, 0, 0],
[0, 0, 0, 1, 0]
]
# Test if solver can find a path
path = maze_solver.solve_maze(maze)
self.assertIsNotNone(path)
self.assertGreater(len(path), 0)
# Test validation
self.assertTrue(maze_solver.is_valid_maze(maze))
def test_permutations(self):
"""Test permutations generator"""
# Test basic permutations
arr = [1, 2, 3]
perms = permutations.generate_permutations(arr)
self.assertEqual(len(perms), 6) # 3! = 6
# Test unique permutations with duplicates
arr_with_dups = [1, 1, 2]
unique_perms = permutations.generate_unique_permutations(arr_with_dups)
self.assertEqual(len(unique_perms), 3) # Only 3 unique permutations
# Test k-permutations
k_perms = permutations.generate_k_permutations([1, 2, 3, 4], 2)
self.assertEqual(len(k_perms), 12) # P(4,2) = 12
class TestNewStringAlgorithms(unittest.TestCase):
def test_kmp_search(self):
"""Test KMP string search"""
from pygorithm.strings import kmp_search
text = "ABABDABACDABABCABCABCABCABC"
pattern = "ABABCABCABCABC"
matches = kmp_search.kmp_search(text, pattern)
self.assertGreater(len(matches), 0)
# Test first occurrence - "ABAD" appears at index 2 in "ABABDABACDABABCABCABCABCABC"
first_match = kmp_search.kmp_search_first(text, "ABAD")
# Let's check what the actual result is and fix the test
expected_index = text.find("ABAD") # Use Python's built-in to verify
self.assertEqual(first_match, expected_index)
def test_edit_distance(self):
"""Test edit distance algorithm"""
from pygorithm.strings import edit_distance
# Test basic edit distance
dist = edit_distance.edit_distance("kitten", "sitting")
self.assertEqual(dist, 3)
# Test similarity ratio
ratio = edit_distance.similarity_ratio("hello", "hello")
self.assertEqual(ratio, 1.0)
# Test one edit away
self.assertTrue(edit_distance.is_one_edit_away("cat", "bat"))
self.assertFalse(edit_distance.is_one_edit_away("cat", "dog"))
class TestNewSortingAlgorithms(unittest.TestCase):
def test_bingo_sort(self):
"""Test bingo sort algorithm"""
from pygorithm.sorting import bingo_sort
# Test with duplicates (ideal for bingo sort)
arr = [5, 2, 8, 2, 9, 1, 5, 5, 2]
sorted_arr = bingo_sort.sort(arr)
expected = [1, 2, 2, 2, 5, 5, 5, 8, 9]
self.assertEqual(sorted_arr, expected)
# Test suitability check with array that has more duplicates
arr_with_many_dups = [1, 1, 1, 2, 2, 2, 3, 3, 3]
self.assertTrue(bingo_sort.is_suitable_for_bingo_sort(arr_with_many_dups))
class TestNewPathfindingAlgorithms(unittest.TestCase):
def test_bellman_ford(self):
"""Test Bellman-Ford algorithm"""
from pygorithm.pathfinding import bellman_ford
# Create a sample graph
graph = {
'A': [('B', -1), ('C', 4)],
'B': [('C', 3), ('D', 2), ('E', 2)],
'C': [],
'D': [('B', 1), ('C', 5)],
'E': [('D', -3)]
}
result = bellman_ford.bellman_ford(graph, 'A')
self.assertIsNotNone(result)
distances, predecessors = result
self.assertIn('A', distances)
self.assertEqual(distances['A'], 0)
def test_floyd_warshall(self):
"""Test Floyd-Warshall algorithm"""
from pygorithm.pathfinding import floyd_warshall
graph = {
'A': [('B', 3), ('D', 7)],
'B': [('A', 8), ('C', 2)],
'C': [('A', 5), ('D', 1)],
'D': [('A', 2)]
}
result = floyd_warshall.floyd_warshall(graph)
self.assertIsNotNone(result)
distance_matrix, next_matrix = result
self.assertIn('A', distance_matrix)
self.assertEqual(distance_matrix['A']['A'], 0)
def test_prims_algorithm(self):
"""Test Prim's algorithm"""
from pygorithm.pathfinding import prims_algorithm
graph = {
'A': [('B', 2), ('C', 3)],
'B': [('A', 2), ('C', 1), ('D', 1), ('E', 4)],
'C': [('A', 3), ('B', 1), ('E', 5)],
'D': [('B', 1), ('E', 1)],
'E': [('B', 4), ('C', 5), ('D', 1)]
}
mst_edges, total_weight = prims_algorithm.prims_mst(graph)
self.assertGreater(len(mst_edges), 0)
self.assertGreater(total_weight, 0)
if __name__ == '__main__':
unittest.main()