|
1 | 1 | import unittest |
2 | | - |
| 2 | +from typing import List, Any |
| 3 | +from parameterized import parameterized |
3 | 4 | import pytest |
4 | 5 |
|
5 | | -from . import SinglyLinkedList |
6 | | - |
7 | | - |
8 | | -class LinkedListIsPalindromeTestCase(unittest.TestCase): |
9 | | - def test_1(self): |
10 | | - """should return true for ["r", "a", "c", "e", "c", "a", "r"]""" |
11 | | - linked_list = SinglyLinkedList() |
12 | | - data = ["r", "a", "c", "e", "c", "a", "r"] |
13 | | - for d in data: |
14 | | - linked_list.append(d) |
15 | | - |
16 | | - actual = linked_list.is_palindrome() |
17 | | - self.assertTrue(actual) |
18 | | - |
19 | | - def test_2(self): |
20 | | - """should return true for [2, 4, 6, 4, 2]""" |
21 | | - linked_list = SinglyLinkedList() |
22 | | - data = [2, 4, 6, 4, 2] |
23 | | - for d in data: |
24 | | - linked_list.append(d) |
25 | | - |
26 | | - actual = linked_list.is_palindrome() |
27 | | - self.assertTrue(actual) |
28 | | - |
29 | | - def test_3(self): |
30 | | - """should return true for [0, 3, 5, 5, 0]""" |
31 | | - linked_list = SinglyLinkedList() |
32 | | - data = [0, 3, 5, 5, 0] |
33 | | - for d in data: |
34 | | - linked_list.append(d) |
35 | | - |
36 | | - actual = linked_list.is_palindrome() |
37 | | - self.assertFalse(actual) |
38 | | - |
39 | | - def test_4(self): |
40 | | - """should return true for [9, 7, 4, 4, 7, 9]""" |
41 | | - linked_list = SinglyLinkedList() |
42 | | - data = [9, 7, 4, 4, 7, 9] |
43 | | - for d in data: |
44 | | - linked_list.append(d) |
45 | | - |
46 | | - actual = linked_list.is_palindrome() |
47 | | - self.assertTrue(actual) |
48 | | - |
49 | | - def test_5(self): |
50 | | - """should return true for [1,2,3,2,1]""" |
51 | | - linked_list = SinglyLinkedList() |
52 | | - data = [1, 2, 3, 2, 1] |
53 | | - for d in data: |
54 | | - linked_list.append(d) |
55 | | - |
56 | | - actual = linked_list.is_palindrome() |
57 | | - self.assertTrue(actual) |
58 | | - |
59 | | - def test_6(self): |
60 | | - """should return true for [4,7,9,5,4]""" |
61 | | - linked_list = SinglyLinkedList() |
62 | | - data = [4, 7, 9, 5, 4] |
63 | | - for d in data: |
64 | | - linked_list.append(d) |
| 6 | +from datastructures.linked_lists.singly_linked_list import SinglyLinkedList |
65 | 7 |
|
66 | | - actual = linked_list.is_palindrome() |
67 | | - self.assertFalse(actual) |
| 8 | +IS_LINKED_LIST_PALINDROME_TEST_CASES = [ |
| 9 | + (["r", "a", "c", "e", "c", "a", "r"], True), |
| 10 | + ([2, 4, 6, 4, 2], True), |
| 11 | + ([0, 3, 5, 5, 0], False), |
| 12 | + ([9, 7, 4, 4, 7, 9], True), |
| 13 | + ([1, 2, 3, 2, 1], True), |
| 14 | + ([4, 7, 9, 5, 4], False), |
| 15 | + ([2, 3, 5, 5, 3, 2], True), |
| 16 | + ([6, 1, 0, 5, 1, 6], False), |
| 17 | + ([3, 6, 9, 8, 4, 8, 9, 6, 3], True), |
| 18 | + ([1, 2], False), |
| 19 | + ([1, 2, 3, 4, 5], False), |
| 20 | +] |
68 | 21 |
|
69 | | - def test_7(self): |
70 | | - """should return true for [2,3,5,5,3,2]""" |
71 | | - linked_list = SinglyLinkedList() |
72 | | - data = [2, 3, 5, 5, 3, 2] |
73 | | - for d in data: |
74 | | - linked_list.append(d) |
75 | 22 |
|
76 | | - actual = linked_list.is_palindrome() |
77 | | - self.assertTrue(actual) |
78 | | - |
79 | | - def test_8(self): |
80 | | - """should return true for [6,1,0,5,1,6]""" |
81 | | - linked_list = SinglyLinkedList() |
82 | | - data = [6, 1, 0, 5, 1, 6] |
83 | | - for d in data: |
84 | | - linked_list.append(d) |
85 | | - |
86 | | - actual = linked_list.is_palindrome() |
87 | | - self.assertFalse(actual) |
88 | | - |
89 | | - def test_9(self): |
90 | | - """should return true for [3,6,9,8,4,8,9,6,3]""" |
| 23 | +class LinkedListIsPalindromeTestCase(unittest.TestCase): |
| 24 | + @parameterized.expand(IS_LINKED_LIST_PALINDROME_TEST_CASES) |
| 25 | + def test_is_palindrome_using_stack(self, data: List[Any], expected: bool): |
| 26 | + """Tests to check if a linked list is a palindrome using a stack approach""" |
91 | 27 | linked_list = SinglyLinkedList() |
92 | | - data = [3, 6, 9, 8, 4, 8, 9, 6, 3] |
93 | 28 | for d in data: |
94 | 29 | linked_list.append(d) |
95 | 30 |
|
96 | 31 | actual = linked_list.is_palindrome() |
97 | | - self.assertTrue(actual) |
98 | | - |
99 | | - |
100 | | -class LinkedListIsPalindromeV2TestCase(unittest.TestCase): |
101 | | - """ |
102 | | - Tests to check if a linked list is a palindrome using fast and slow pointers approach |
103 | | - """ |
104 | | - |
105 | | - @pytest.mark.linked_list_is_palindrome_fast_and_slow_pointer |
106 | | - def test_1(self): |
107 | | - """should return true for ["r", "a", "c", "e", "c", "a", "r"]""" |
108 | | - linked_list = SinglyLinkedList() |
109 | | - data = ["r", "a", "c", "e", "c", "a", "r"] |
110 | | - for d in data: |
111 | | - linked_list.append(d) |
112 | | - |
113 | | - actual = linked_list.is_palindrome_2() |
114 | | - self.assertTrue(actual) |
115 | | - |
116 | | - @pytest.mark.linked_list_is_palindrome_fast_and_slow_pointer |
117 | | - def test_2(self): |
118 | | - """should return true for [2, 4, 6, 4, 2]""" |
119 | | - linked_list = SinglyLinkedList() |
120 | | - data = [2, 4, 6, 4, 2] |
121 | | - for d in data: |
122 | | - linked_list.append(d) |
123 | | - |
124 | | - actual = linked_list.is_palindrome_2() |
125 | | - self.assertTrue(actual) |
126 | | - |
127 | | - @pytest.mark.linked_list_is_palindrome_fast_and_slow_pointer |
128 | | - def test_3(self): |
129 | | - """should return true for [0, 3, 5, 5, 0]""" |
130 | | - linked_list = SinglyLinkedList() |
131 | | - data = [0, 3, 5, 5, 0] |
132 | | - for d in data: |
133 | | - linked_list.append(d) |
134 | | - |
135 | | - actual = linked_list.is_palindrome_2() |
136 | | - self.assertFalse(actual) |
137 | | - |
138 | | - @pytest.mark.linked_list_is_palindrome_fast_and_slow_pointer |
139 | | - def test_4(self): |
140 | | - """should return true for [9, 7, 4, 4, 7, 9]""" |
141 | | - linked_list = SinglyLinkedList() |
142 | | - data = [9, 7, 4, 4, 7, 9] |
143 | | - for d in data: |
144 | | - linked_list.append(d) |
145 | | - |
146 | | - actual = linked_list.is_palindrome_2() |
147 | | - self.assertTrue(actual) |
148 | | - |
149 | | - @pytest.mark.linked_list_is_palindrome_fast_and_slow_pointer |
150 | | - def test_5(self): |
151 | | - """should return true for [1,2,3,2,1]""" |
152 | | - linked_list = SinglyLinkedList() |
153 | | - data = [1, 2, 3, 2, 1] |
154 | | - for d in data: |
155 | | - linked_list.append(d) |
156 | | - |
157 | | - actual = linked_list.is_palindrome_2() |
158 | | - self.assertTrue(actual) |
159 | | - |
160 | | - @pytest.mark.linked_list_is_palindrome_fast_and_slow_pointer |
161 | | - def test_6(self): |
162 | | - """should return true for [4,7,9,5,4]""" |
163 | | - linked_list = SinglyLinkedList() |
164 | | - data = [4, 7, 9, 5, 4] |
165 | | - for d in data: |
166 | | - linked_list.append(d) |
167 | | - |
168 | | - actual = linked_list.is_palindrome_2() |
169 | | - self.assertFalse(actual) |
170 | | - |
171 | | - @pytest.mark.linked_list_is_palindrome_fast_and_slow_pointer |
172 | | - def test_7(self): |
173 | | - """should return true for [2,3,5,5,3,2]""" |
174 | | - linked_list = SinglyLinkedList() |
175 | | - data = [2, 3, 5, 5, 3, 2] |
176 | | - for d in data: |
177 | | - linked_list.append(d) |
178 | | - |
179 | | - actual = linked_list.is_palindrome_2() |
180 | | - self.assertTrue(actual) |
181 | | - |
182 | | - @pytest.mark.linked_list_is_palindrome_fast_and_slow_pointer |
183 | | - def test_8(self): |
184 | | - """should return false for [6,1,0,5,1,6]""" |
185 | | - linked_list = SinglyLinkedList() |
186 | | - data = [6, 1, 0, 5, 1, 6] |
187 | | - for d in data: |
188 | | - linked_list.append(d) |
189 | | - |
190 | | - actual = linked_list.is_palindrome_2() |
191 | | - self.assertFalse(actual) |
| 32 | + self.assertEqual(expected, actual) |
192 | 33 |
|
193 | 34 | @pytest.mark.linked_list_is_palindrome_fast_and_slow_pointer |
194 | | - def test_9(self): |
195 | | - """should return true for [3,6,9,8,4,8,9,6,3]""" |
| 35 | + @parameterized.expand(IS_LINKED_LIST_PALINDROME_TEST_CASES) |
| 36 | + def test_is_palindrome_using_two_pointers(self, data: List[Any], expected: bool): |
| 37 | + """Tests to check if a linked list is a palindrome using fast and slow pointers approach""" |
196 | 38 | linked_list = SinglyLinkedList() |
197 | | - data = [3, 6, 9, 8, 4, 8, 9, 6, 3] |
198 | 39 | for d in data: |
199 | 40 | linked_list.append(d) |
200 | 41 |
|
201 | 42 | actual = linked_list.is_palindrome_2() |
202 | | - self.assertTrue(actual) |
| 43 | + self.assertEqual(expected, actual) |
203 | 44 |
|
204 | 45 |
|
205 | 46 | if __name__ == "__main__": |
|
0 commit comments