-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_linkedlist.py
215 lines (187 loc) · 7.13 KB
/
test_linkedlist.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
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
import unittest
from algorithms.linkedlist import (
reverse_list, reverse_list_recursive,
is_sorted,
remove_range,
swap_pairs,
rotate_right,
is_cyclic,
merge_two_list, merge_two_list_recur,
is_palindrome, is_palindrome_stack, is_palindrome_dict,
RandomListNode, copy_random_pointer_v1, copy_random_pointer_v2
)
class Node(object):
def __init__(self, x):
self.val = x
self.next = None
# Convert from linked list Node to list for testing
def convert(head):
ret = []
if head:
current = head
while current:
ret.append(current.val)
current = current.next
return ret
class TestSuite(unittest.TestCase):
def setUp(self):
# list test for palindrome
self.l = Node('A')
self.l.next = Node('B')
self.l.next.next = Node('C')
self.l.next.next.next = Node('B')
self.l.next.next.next.next = Node('A')
self.l1 = Node('A')
self.l1.next = Node('B')
self.l1.next.next = Node('C')
self.l1.next.next.next = Node('B')
def test_reverse_list(self):
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
self.assertEqual([4, 3, 2, 1], convert(reverse_list(head)))
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
self.assertEqual([4, 3, 2, 1], convert(reverse_list_recursive(head)))
def test_is_sorted(self):
head = Node(-2)
head.next = Node(2)
head.next.next = Node(2)
head.next.next.next = Node(4)
head.next.next.next.next = Node(9)
# head -> -2 -> 2 -> 2 -> 4 -> 9
self.assertTrue(is_sorted(head))
head = Node(1)
head.next = Node(2)
head.next.next = Node(8)
head.next.next.next = Node(4)
head.next.next.next.next = Node(6)
# head -> 1 -> 2 -> 8 -> 4 -> 6
self.assertFalse(is_sorted(head))
def test_remove_range(self):
# Test case: middle case.
head = Node(0)
head.next = Node(1)
head.next.next = Node(2)
head.next.next.next = Node(3)
head.next.next.next.next = Node(4)
# Expect output: 0 4
self.assertEqual([0, 4], convert(remove_range(head, 1, 3)))
# Test case: taking out the front node
head = Node(0)
head.next = Node(1)
head.next.next = Node(2)
head.next.next.next = Node(3)
head.next.next.next.next = Node(4)
# Expect output: 2 3 4
self.assertEqual([2, 3, 4], convert(remove_range(head, 0, 1)))
# Test case: removing all the nodes
head = Node(0)
head.next = Node(1)
head.next.next = Node(2)
head.next.next.next = Node(3)
head.next.next.next.next = Node(4)
self.assertEqual([], convert(remove_range(head, 0, 7)))
def test_swap_in_pairs(self):
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
# Expect output : 2 --> 1 --> 4 --> 3
self.assertEqual([2, 1, 4, 3], convert(swap_pairs(head)))
def test_rotate_right(self):
# Given 1->2->3->4->5->NULL
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
# K = 2. Expect output: 4->5->1->2->3->NULL.
self.assertEqual([4, 5, 1, 2, 3], convert(rotate_right(head, 2)))
def test_is_cyclic(self):
# create linked list => A -> B -> C -> D -> E -> C
head = Node('A')
head.next = Node('B')
curr = head.next
cyclic_node = Node('C')
curr.next = cyclic_node
curr = curr.next
curr.next = Node('D')
curr = curr.next
curr.next = Node('E')
curr = curr.next
curr.next = cyclic_node
self.assertTrue(is_cyclic(head))
# create linked list 1 -> 2 -> 3 -> 4
head = Node(1)
curr = head
for i in range(2, 6):
curr.next = Node(i)
curr = curr.next
self.assertFalse(is_cyclic(head))
def test_merge_two_list(self):
"""
Input: head1:1->2->4, head2: 1->3->4
Output: 1->1->2->3->4->4
"""
head1 = Node(1)
head1.next = Node(2)
head1.next.next = Node(4)
head2 = Node(1)
head2.next = Node(3)
head2.next.next = Node(4)
self.assertEqual([1, 1, 2, 3, 4, 4],
convert(merge_two_list(head1, head2)))
# Test recursive
head1 = Node(1)
head1.next = Node(2)
head1.next.next = Node(4)
head2 = Node(1)
head2.next = Node(3)
head2.next.next = Node(4)
self.assertEqual([1, 1, 2, 3, 4, 4],
convert(merge_two_list_recur(head1, head2)))
def test_is_palindrome(self):
self.assertTrue(is_palindrome(self.l))
self.assertFalse(is_palindrome(self.l1))
def test_is_palindrome_stack(self):
self.assertTrue(is_palindrome_stack(self.l))
self.assertFalse(is_palindrome_stack(self.l1))
def test_is_palindrome_dict(self):
self.assertTrue(is_palindrome_dict(self.l))
self.assertFalse(is_palindrome_dict(self.l1))
def test_solution_0(self):
self._init_random_list_nodes()
result = copy_random_pointer_v1(self.random_list_node1)
self._assert_is_a_copy(result)
def test_solution_1(self):
self._init_random_list_nodes()
result = copy_random_pointer_v2(self.random_list_node1)
self._assert_is_a_copy(result)
def _assert_is_a_copy(self, result):
self.assertEqual(5, result.next.next.next.next.label)
self.assertEqual(4, result.next.next.next.label)
self.assertEqual(3, result.next.next.label)
self.assertEqual(2, result.next.label)
self.assertEqual(1, result.label)
self.assertEqual(3, result.next.next.next.next.random.label)
self.assertIsNone(result.next.next.next.random)
self.assertEqual(2, result.next.next.random.label)
self.assertEqual(5, result.next.random.label)
self.assertEqual(4, result.random.label)
def _init_random_list_nodes(self):
self.random_list_node1 = RandomListNode(1)
random_list_node2 = RandomListNode(2)
random_list_node3 = RandomListNode(3)
random_list_node4 = RandomListNode(4)
random_list_node5 = RandomListNode(5)
self.random_list_node1.next, self.random_list_node1.random = random_list_node2, random_list_node4
random_list_node2.next, random_list_node2.random = random_list_node3, random_list_node5
random_list_node3.next, random_list_node3.random = random_list_node4, random_list_node2
random_list_node4.next = random_list_node5
random_list_node5.random = random_list_node3
if __name__ == "__main__":
unittest.main()