Skip to content

Commit 52fa49c

Browse files
committed
Added more tests for singly linked list
1 parent 81906d5 commit 52fa49c

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

data_structures/linked_lists/singly_linked_list.py

+3-31
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,12 @@ def is_palindrome(self):
276276
This method will determine whether the linked list is palindrome or
277277
not.
278278
"""
279-
s = ""
279+
s = list()
280280
cur = self.head
281281
while cur:
282-
s += str(cur.data)
282+
s.append(cur.data)
283283
cur = cur.next
284+
print(s)
284285
return s == s[::-1]
285286

286287
def move_tail_to_head(self):
@@ -297,32 +298,3 @@ def move_tail_to_head(self):
297298
cur.next = self.head
298299
self.head = cur
299300
prev.next = None
300-
301-
def sum(self, llist):
302-
"""
303-
This method will add the values of another linked list to the values of
304-
the linked list
305-
:param llist: The linked list that should add to the linked list
306-
"""
307-
prev = None
308-
p = self.head
309-
q = llist.head
310-
carry = 0
311-
while p or q:
312-
i = p.data if p else 0
313-
j = q.data if q else 0
314-
_sum = i + j + carry
315-
if _sum >= 10:
316-
carry = 1
317-
_sum %= 10
318-
else:
319-
carry = 0
320-
if p:
321-
p.data = _sum
322-
prev = p
323-
p = p.next
324-
else:
325-
prev.next = Node(_sum)
326-
prev = prev.next
327-
if q:
328-
q = q.next

tests/test_singly_linked_list.py

+43-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ def setUp(self) -> None:
99
self.linked_list = LinkedList()
1010
self.sample_data = [1, 10, 3, 8, 12, 9, 4, 15, 24]
1111

12-
@mock.patch('builtins.print')
13-
def assert_valid_print(self, items, mock_print):
14-
self.linked_list.print_list()
15-
mock_print.assert_called_with(
16-
" ".join(map(str, items))
17-
)
18-
1912
def append_items(self, items=None):
2013
if items is None:
2114
items = self.sample_data
@@ -26,6 +19,13 @@ def prepend_items(self):
2619
for number in self.sample_data:
2720
self.linked_list.prepend(number)
2821

22+
@mock.patch('builtins.print')
23+
def assert_valid_print(self, items, mock_print):
24+
self.linked_list.print_list()
25+
mock_print.assert_called_with(
26+
" ".join(map(str, items))
27+
)
28+
2929
def test_append_empty_linked_list(self):
3030
self.assertIsNone(self.linked_list.head)
3131
self.linked_list.append(15)
@@ -123,3 +123,39 @@ def test_merge_sort(self):
123123
list2.append(item)
124124
self.linked_list.merge_sorted(list2)
125125
self.assert_valid_print(sorted(data1 + data2))
126+
127+
def test_remove_duplicates(self):
128+
data = copy.copy(self.sample_data)
129+
data.append(data[0])
130+
self.append_items(data)
131+
self.linked_list.remove_duplicates()
132+
self.assert_valid_print(self.sample_data)
133+
134+
def test_get_nth_from_last(self):
135+
self.append_items()
136+
n = 5
137+
nth_item = self.linked_list.get_nth_from_last(n)
138+
self.assertEqual(nth_item, self.sample_data[-n])
139+
140+
def test_count_occurrences(self):
141+
self.append_items()
142+
num = 5
143+
counted = self.linked_list.count_occurrences(num)
144+
self.assertEqual(counted, self.sample_data.count(num))
145+
146+
def test_is_palindrome_false(self):
147+
self.append_items()
148+
self.assertFalse(self.linked_list.is_palindrome())
149+
150+
def test_is_palindrome_true(self):
151+
data = copy.copy(self.sample_data)
152+
data.extend(self.sample_data[::-1])
153+
self.append_items(data)
154+
self.assertTrue(self.linked_list.is_palindrome())
155+
156+
def test_move_tail_to_head(self):
157+
self.append_items()
158+
self.linked_list.move_tail_to_head()
159+
self.linked_list.print_list()
160+
data = [self.sample_data[-1]] + self.sample_data[:-1]
161+
self.assert_valid_print(data)

0 commit comments

Comments
 (0)