|
2 | 2 | from typing import List |
3 | 3 | from parameterized import parameterized |
4 | 4 | from datastructures.linked_lists.singly_linked_list.node import SingleNode |
5 | | -from datastructures.linked_lists.linked_list_utils import remove_nth_from_end |
| 5 | +from datastructures.linked_lists.linked_list_utils import ( |
| 6 | + remove_nth_from_end, |
| 7 | + sum_of_linked_lists, |
| 8 | +) |
6 | 9 |
|
7 | 10 | REMOVE_NTH_NODE_FROM_END_TEST_CASES = [ |
8 | 11 | ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 4, [0, 1, 2, 3, 4, 5, 7, 8, 9]), |
9 | | - ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 10, [1, 2, 3, 4, 5, 6, 7, 8, 9]) |
| 12 | + ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 10, [1, 2, 3, 4, 5, 6, 7, 8, 9]), |
10 | 13 | ] |
11 | 14 |
|
12 | 15 |
|
@@ -35,5 +38,57 @@ def test_remove_nth_node_from_end( |
35 | 38 | self.assertListEqual(expected, actual_data) |
36 | 39 |
|
37 | 40 |
|
| 41 | +SUM_LINKED_LISTS_TEST_CASES = [ |
| 42 | + ([2, 4, 3], [5, 6, 4], [7, 0, 8]), |
| 43 | + ([0], [0], [0]), |
| 44 | + ([9, 9, 9, 9, 9, 9, 9], [9, 9, 9, 9], [8, 9, 9, 9, 0, 0, 0, 1]), |
| 45 | +] |
| 46 | + |
| 47 | + |
| 48 | +class SumLinkedListsTestCase(unittest.TestCase): |
| 49 | + @staticmethod |
| 50 | + def construct_linked_list(data: List[int]) -> SingleNode: |
| 51 | + head = SingleNode(data[0]) |
| 52 | + current = head |
| 53 | + for d in data[1:]: |
| 54 | + current.next = SingleNode(d) |
| 55 | + current = current.next |
| 56 | + return head |
| 57 | + |
| 58 | + def assert_data(self, actual: SingleNode, expected: List[int]): |
| 59 | + actual_current_head = actual |
| 60 | + actual_data = [] |
| 61 | + while actual_current_head: |
| 62 | + actual_data.append(actual_current_head.data) |
| 63 | + actual_current_head = actual_current_head.next |
| 64 | + |
| 65 | + self.assertListEqual(expected, actual_data) |
| 66 | + |
| 67 | + @parameterized.expand(SUM_LINKED_LISTS_TEST_CASES) |
| 68 | + def test_sum_linked_lists( |
| 69 | + self, data_one: List[int], data_two: List[int], expected: List[int] |
| 70 | + ): |
| 71 | + if not data_one and not data_two: |
| 72 | + actual = sum_of_linked_lists(head_one=None, head_two=None) |
| 73 | + self.assertIsNone(actual) |
| 74 | + |
| 75 | + if not data_one and data_two: |
| 76 | + head = self.construct_linked_list(data_two) |
| 77 | + actual = sum_of_linked_lists(head_one=None, head_two=head) |
| 78 | + self.assertEqual(expected, actual) |
| 79 | + |
| 80 | + if data_one and not data_two: |
| 81 | + head = self.construct_linked_list(data_one) |
| 82 | + actual = sum_of_linked_lists(head_one=None, head_two=head) |
| 83 | + self.assertEqual(expected, actual) |
| 84 | + |
| 85 | + head_one = self.construct_linked_list(data_one) |
| 86 | + head_two = self.construct_linked_list(data_two) |
| 87 | + |
| 88 | + actual = sum_of_linked_lists(head_one=head_one, head_two=head_two) |
| 89 | + self.assertIsNotNone(actual) |
| 90 | + self.assert_data(actual, expected) |
| 91 | + |
| 92 | + |
38 | 93 | if __name__ == "__main__": |
39 | 94 | unittest.main() |
0 commit comments