forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_linkedlist.py
174 lines (156 loc) · 5.27 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
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
)
import unittest
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))
if __name__ == "__main__":
unittest.main()