-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
261 lines (210 loc) · 5.89 KB
/
Copy pathtests.py
File metadata and controls
261 lines (210 loc) · 5.89 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from doubly_linked_list import DoublyLinkedList
from helpers import print_title, check, check_list
# ------------------------------
# Example usage and tests
# ------------------------------
print_title("INITIAL DOUBLY LINKED LIST TEST")
dll = DoublyLinkedList(4)
dll.print_list()
check(4, dll.head.value, "Head value")
check(4, dll.tail.value, "Tail value")
check(1, dll.length, "List length")
# --------------------------------------------------
print_title("APPEND TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.print_list()
check_list(dll, [1, 2, 3], "List after append")
# --------------------------------------------------
print_title("MAKE EMPTY & APPEND TEST")
dll = DoublyLinkedList(1)
dll.make_empty()
dll.print_list()
check(0, dll.length, "List length after make_empty")
dll.append(10)
dll.append(20)
dll.print_list()
check_list(dll, [10, 20], "List after appending to empty")
# --------------------------------------------------
print_title("POP TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.print_list()
popped = dll.pop()
print(f"Popped value: {popped.value if popped else None}")
dll.print_list()
check_list(dll, [1, 2], "List after pop")
# --------------------------------------------------
print_title("POP ALL TEST")
dll = DoublyLinkedList(5)
dll.append(6)
dll.print_list()
dll.pop()
dll.pop()
dll.print_list()
check_list(dll, [], "List after popping all nodes")
# --------------------------------------------------
print_title("PREPEND TEST")
dll = DoublyLinkedList(2)
dll.append(3)
dll.print_list()
dll.prepend(1)
dll.print_list()
check_list(dll, [1, 2, 3], "List after prepend")
# --------------------------------------------------
print_title("POP FIRST TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.print_list()
popped_first = dll.pop_first()
print(f"Popped first value: {popped_first.value if popped_first else None}")
dll.print_list()
check_list(dll, [2, 3], "List after pop_first")
# --------------------------------------------------
print_title("GET TEST")
dll = DoublyLinkedList(0)
dll.append(1)
dll.append(2)
dll.append(3)
dll.print_list()
check(1, dll.get(1).value, "Get node at index 1")
check(3, dll.get(3).value, "Get node at index 3")
# --------------------------------------------------
print_title("SET VALUE TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.print_list()
dll.set_value(1, 99)
dll.print_list()
check_list(dll, [1, 99, 3], "List after set_value")
# --------------------------------------------------
print_title("INSERT TEST")
dll = DoublyLinkedList(1)
dll.append(3)
dll.insert(1, 2) # Insert in middle
dll.insert(0, 0) # Insert at head
dll.insert(dll.length, 4) # Insert at tail
dll.print_list()
check_list(dll, [0, 1, 2, 3, 4], "List after inserts")
# --------------------------------------------------
print_title("REMOVE TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.append(4)
dll.print_list()
removed = dll.remove(2)
print(f"Removed value: {removed.value if removed else None}")
dll.print_list()
check_list(dll, [1, 2, 4], "List after remove in middle")
# --------------------------------------------------
print_title("IS PALINDROME TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.append(2)
dll.append(1)
dll.print_list()
check(True, dll.is_palindrome(), "Is palindrome")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.print_list()
check(False, dll.is_palindrome(), "Is not palindrome")
# --------------------------------------------------
print_title("REVERSE TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.append(4)
dll.print_list()
dll.reverse()
dll.print_list()
check_list(dll, [4, 3, 2, 1], "List after reverse")
# --------------------------------------------------
print_title("PARTITION LIST TEST")
dll = DoublyLinkedList(3)
dll.append(8)
dll.append(5)
dll.append(10)
dll.append(2)
dll.append(1)
dll.print_list()
dll.partition_list(5)
dll.print_list()
# Expected partitioned order may vary depending on implementation
# --------------------------------------------------
print_title("REVERSE BETWEEN TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.append(4)
dll.append(5)
dll.print_list()
dll.reverse_between(1, 4)
dll.print_list()
# Expected: [1, 4, 3, 2, 5] if reversed between index 1 and 4
# --------------------------------------------------
print_title("SWAP PAIRS TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.append(4)
dll.print_list()
dll.swap_pairs()
dll.print_list()
check_list(dll, [2, 1, 4, 3], "List after swap_pairs")
# --------------------------------------------------
print_title("FIND MIDDLE NODE TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.append(4)
dll.append(5)
dll.print_list()
middle = dll.find_middle_node()
print(f"Middle node value: {middle.value if middle else None}")
# --------------------------------------------------
print_title("HAS LOOP TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.tail.next = dll.head # Manually create loop
print(f"Has loop: {dll.has_loop()}")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
print(f"Has loop: {dll.has_loop()}")
# --------------------------------------------------
print_title("FIND KTH FROM END TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(3)
dll.append(4)
dll.append(5)
dll.print_list()
kth = dll.find_kth_from_end(2)
print(f"2nd node from end: {kth.value if kth else None}")
# --------------------------------------------------
print_title("REMOVE DUPLICATES TEST")
dll = DoublyLinkedList(1)
dll.append(2)
dll.append(1)
dll.append(3)
dll.append(2)
dll.print_list()
dll.remove_duplicates()
dll.print_list()
check_list(dll, [1, 2, 3], "List after remove_duplicates")
# --------------------------------------------------
print_title("BINARY TO DECIMAL TEST")
dll = DoublyLinkedList(1)
dll.append(1)
dll.append(0)
dll.print_list()
decimal = dll.binary_to_decimal()
check(6, decimal, "Binary 110 to Decimal")