From 2e9a2ff6ed4ef3e1f14f9dec1de4441f6664be4d Mon Sep 17 00:00:00 2001 From: Maliha Date: Tue, 10 Nov 2020 14:02:07 -0800 Subject: [PATCH 1/8] Create merge_two_lists.py that implements merging of two sorted linked lists --- .../linked_list/merge_two_lists.py | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 data_structures/linked_list/merge_two_lists.py diff --git a/data_structures/linked_list/merge_two_lists.py b/data_structures/linked_list/merge_two_lists.py new file mode 100644 index 000000000000..985b115c5c53 --- /dev/null +++ b/data_structures/linked_list/merge_two_lists.py @@ -0,0 +1,102 @@ +""" +Algorithm that merges two sorted linked lists into one sorted linked list. +""" + + +class Node: + def __init__(self, data): + self.data = data + self.next = None + + +def create_linked_list(num_list: list) -> Node: + """ + >>> node = create_linked_list([3,2,1]) + >>> node.data + 1 + >>> node = create_linked_list([9,6,-3,5,8]) + >>> node.data + -3 + >>> create_linked_list([]) + + """ + if not num_list: + return None + + num_list.sort() + current = head = Node(num_list[0]) + for i in range(1, len(num_list)): + current.next = Node(num_list[i]) + current = current.next + return head + + +def str_linked_list(head: Node) -> str: + """ + >>> n1 = Node(1) + >>> n2 = Node(2) + >>> n3 = Node(3) + >>> n1.next = n2 + >>> n2.next = n3 + >>> str_linked_list(n1) + '1->2->3' + """ + int_list = [] + current = head + while current is not None: + int_list.append(f"{current.data}") + current = current.next + return '->'.join(int_list) + + +def merge_lists(list_one: list, list_two: list) -> Node: + """ + >>> n1 = Node(1) + >>> n2 = Node(2) + >>> n3 = Node(3) + >>> n4 = Node(4) + >>> n5 = Node(5) + >>> n6 = Node(6) + >>> n1.next = n3 + >>> n2.next = n4 + >>> n4.next = n5 + >>> n5.next = n6 + >>> ll = merge_lists(n1, n2) + >>> str_linked_list(ll) + '1->2->3->4->5->6' + """ + node_one = list_one + node_two = list_two + merged_list = current_node = Node(0) + + while node_one is not None and node_two is not None: + if node_one.data <= node_two.data: + current_node.next = node_one + node_one = node_one.next + else: + current_node.next = node_two + node_two = node_two.next + current_node = current_node.next + + if node_one is not None: + current_node.next = node_one + + if node_two is not None: + current_node.next = node_two + + return merged_list.next + + +def main() -> None: + list1 = [3, 9, 7, 5, 1] + list2 = [4, 6, 5, 2, 8, 7, 10, 3, 0] + ll1 = create_linked_list(list1) + ll2 = create_linked_list(list2) + ll3 = merge_lists(ll1, ll2) + print(str_linked_list(ll3)) + + +if __name__ == "__main__": + main() + import doctest + doctest.testmod() From 2836d721878861f959189de625a726f6d120de8b Mon Sep 17 00:00:00 2001 From: Maliha Date: Tue, 10 Nov 2020 14:14:41 -0800 Subject: [PATCH 2/8] Update merge_two_lists.py Fixed formatting errors --- data_structures/linked_list/merge_two_lists.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/merge_two_lists.py b/data_structures/linked_list/merge_two_lists.py index 985b115c5c53..3520996eb742 100644 --- a/data_structures/linked_list/merge_two_lists.py +++ b/data_structures/linked_list/merge_two_lists.py @@ -46,7 +46,7 @@ def str_linked_list(head: Node) -> str: while current is not None: int_list.append(f"{current.data}") current = current.next - return '->'.join(int_list) + return "->".join(int_list) def merge_lists(list_one: list, list_two: list) -> Node: @@ -99,4 +99,5 @@ def main() -> None: if __name__ == "__main__": main() import doctest + doctest.testmod() From febed1416d39ff5a7742ff067a7403f4f1432f1a Mon Sep 17 00:00:00 2001 From: Maliha Date: Tue, 10 Nov 2020 14:16:56 -0800 Subject: [PATCH 3/8] Fixed trailing whitespace --- data_structures/linked_list/merge_two_lists.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/merge_two_lists.py b/data_structures/linked_list/merge_two_lists.py index 3520996eb742..f57e4b86cb62 100644 --- a/data_structures/linked_list/merge_two_lists.py +++ b/data_structures/linked_list/merge_two_lists.py @@ -99,5 +99,5 @@ def main() -> None: if __name__ == "__main__": main() import doctest - + doctest.testmod() From f335a5c81516a28b3f567602f12af4bcb5c91f84 Mon Sep 17 00:00:00 2001 From: Maliha Date: Mon, 30 Nov 2020 22:59:43 -0800 Subject: [PATCH 4/8] Change name of function to def __str__() --- data_structures/linked_list/merge_two_lists.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/merge_two_lists.py b/data_structures/linked_list/merge_two_lists.py index f57e4b86cb62..f4a6cca6ccd7 100644 --- a/data_structures/linked_list/merge_two_lists.py +++ b/data_structures/linked_list/merge_two_lists.py @@ -31,7 +31,7 @@ def create_linked_list(num_list: list) -> Node: return head -def str_linked_list(head: Node) -> str: +def __str__(head: Node) -> str: """ >>> n1 = Node(1) >>> n2 = Node(2) @@ -93,7 +93,7 @@ def main() -> None: ll1 = create_linked_list(list1) ll2 = create_linked_list(list2) ll3 = merge_lists(ll1, ll2) - print(str_linked_list(ll3)) + print(__str__(ll3)) if __name__ == "__main__": From ffb2e6bdb025b168b1687a0ebdd59a312e53a9f6 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 1 Dec 2020 07:01:09 +0000 Subject: [PATCH 5/8] updating DIRECTORY.md --- DIRECTORY.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index fd45eacaad9b..c3c1a4f9fe7d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -148,6 +148,7 @@ * [From Sequence](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/from_sequence.py) * [Has Loop](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/has_loop.py) * [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/is_palindrome.py) + * [Merge Two Lists](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/merge_two_lists.py) * [Middle Element Of Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/middle_element_of_linked_list.py) * [Print Reverse](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/print_reverse.py) * [Singly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/singly_linked_list.py) @@ -206,6 +207,7 @@ * [Heaps Algorithm](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/heaps_algorithm.py) * [Heaps Algorithm Iterative](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/heaps_algorithm_iterative.py) * [Inversions](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/inversions.py) + * [Kth Order Statistic](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/kth_order_statistic.py) * [Max Subarray Sum](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/max_subarray_sum.py) * [Mergesort](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/mergesort.py) * [Power](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/power.py) @@ -390,6 +392,7 @@ * [Chudnovsky Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/chudnovsky_algorithm.py) * [Collatz Sequence](https://github.com/TheAlgorithms/Python/blob/master/maths/collatz_sequence.py) * [Combinations](https://github.com/TheAlgorithms/Python/blob/master/maths/combinations.py) + * [Decimal Isolate](https://github.com/TheAlgorithms/Python/blob/master/maths/decimal_isolate.py) * [Entropy](https://github.com/TheAlgorithms/Python/blob/master/maths/entropy.py) * [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py) * [Explicit Euler](https://github.com/TheAlgorithms/Python/blob/master/maths/explicit_euler.py) @@ -493,6 +496,7 @@ * [Autocomplete Using Trie](https://github.com/TheAlgorithms/Python/blob/master/other/autocomplete_using_trie.py) * [Binary Exponentiation](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation.py) * [Binary Exponentiation 2](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation_2.py) + * [Davis–Putnam–Logemann–Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davis–putnam–logemann–loveland.py) * [Detecting English Programmatically](https://github.com/TheAlgorithms/Python/blob/master/other/detecting_english_programmatically.py) * [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py) * [Doomsday](https://github.com/TheAlgorithms/Python/blob/master/other/doomsday.py) @@ -660,6 +664,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_048/sol1.py) * Problem 049 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_049/sol1.py) + * Problem 050 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_050/sol1.py) * Problem 051 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_051/sol1.py) * Problem 052 @@ -681,6 +687,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_062/sol1.py) * Problem 063 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_063/sol1.py) + * Problem 064 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_064/sol1.py) * Problem 065 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_065/sol1.py) * Problem 067 @@ -694,6 +702,7 @@ * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_072/sol2.py) * Problem 074 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_074/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_074/sol2.py) * Problem 075 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_075/sol1.py) * Problem 076 @@ -718,6 +727,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_119/sol1.py) * Problem 120 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_120/sol1.py) + * Problem 123 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_123/sol1.py) * Problem 125 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_125/sol1.py) * Problem 173 @@ -726,12 +737,16 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_174/sol1.py) * Problem 191 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_191/sol1.py) + * Problem 203 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_203/sol1.py) * Problem 206 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_206/sol1.py) * Problem 207 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_207/sol1.py) * Problem 234 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_234/sol1.py) + * Problem 301 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_301/sol1.py) * Problem 551 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py) From 697665a6fefab9d3b3df291790659548be426d51 Mon Sep 17 00:00:00 2001 From: Maliha Date: Wed, 2 Dec 2020 17:40:58 -0800 Subject: [PATCH 6/8] Imported classes from singly_linked_list.py --- .../linked_list/merge_two_lists.py | 38 ++++++------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/data_structures/linked_list/merge_two_lists.py b/data_structures/linked_list/merge_two_lists.py index f4a6cca6ccd7..008f8d389305 100644 --- a/data_structures/linked_list/merge_two_lists.py +++ b/data_structures/linked_list/merge_two_lists.py @@ -1,12 +1,10 @@ """ Algorithm that merges two sorted linked lists into one sorted linked list. +Class implementations imported from: +https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/singly_linked_list.py """ - -class Node: - def __init__(self, data): - self.data = data - self.next = None +from data_structures.linked_list.singly_linked_list import Node, LinkedList def create_linked_list(num_list: list) -> Node: @@ -31,24 +29,6 @@ def create_linked_list(num_list: list) -> Node: return head -def __str__(head: Node) -> str: - """ - >>> n1 = Node(1) - >>> n2 = Node(2) - >>> n3 = Node(3) - >>> n1.next = n2 - >>> n2.next = n3 - >>> str_linked_list(n1) - '1->2->3' - """ - int_list = [] - current = head - while current is not None: - int_list.append(f"{current.data}") - current = current.next - return "->".join(int_list) - - def merge_lists(list_one: list, list_two: list) -> Node: """ >>> n1 = Node(1) @@ -61,8 +41,10 @@ def merge_lists(list_one: list, list_two: list) -> Node: >>> n2.next = n4 >>> n4.next = n5 >>> n5.next = n6 - >>> ll = merge_lists(n1, n2) - >>> str_linked_list(ll) + >>> head = merge_lists(n1, n2) + >>> ll = LinkedList() + >>> ll.head = head + >>> repr(ll) '1->2->3->4->5->6' """ node_one = list_one @@ -92,8 +74,10 @@ def main() -> None: list2 = [4, 6, 5, 2, 8, 7, 10, 3, 0] ll1 = create_linked_list(list1) ll2 = create_linked_list(list2) - ll3 = merge_lists(ll1, ll2) - print(__str__(ll3)) + new_head = merge_lists(ll1, ll2) + ll3 = LinkedList() + ll3.head = new_head + print(repr(ll3)) if __name__ == "__main__": From 295ec36e2fb60db4ae9c3e06fd6a5632fbd931a9 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 3 Dec 2020 15:28:05 +0100 Subject: [PATCH 7/8] Update merge_two_lists.py --- .../linked_list/merge_two_lists.py | 149 +++++++++--------- 1 file changed, 76 insertions(+), 73 deletions(-) diff --git a/data_structures/linked_list/merge_two_lists.py b/data_structures/linked_list/merge_two_lists.py index 008f8d389305..7aa84b21099d 100644 --- a/data_structures/linked_list/merge_two_lists.py +++ b/data_structures/linked_list/merge_two_lists.py @@ -1,87 +1,90 @@ """ Algorithm that merges two sorted linked lists into one sorted linked list. -Class implementations imported from: -https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/singly_linked_list.py -""" - -from data_structures.linked_list.singly_linked_list import Node, LinkedList - - -def create_linked_list(num_list: list) -> Node: - """ - >>> node = create_linked_list([3,2,1]) - >>> node.data - 1 - >>> node = create_linked_list([9,6,-3,5,8]) - >>> node.data - -3 - >>> create_linked_list([]) - - """ - if not num_list: - return None - - num_list.sort() - current = head = Node(num_list[0]) - for i in range(1, len(num_list)): - current.next = Node(num_list[i]) - current = current.next - return head - -def merge_lists(list_one: list, list_two: list) -> Node: +https://en.wikipedia.org/wiki/Linked_list +""" +from __future__ import annotations + +from dataclasses import dataclass +from typing import List, Optional + +test_data_odd = (3, 9, -11, 0, 7, 5, 1, -1) +test_data_even = (4, 6, 2, 0, 8, 10, 3, -2) + + +@dataclass +class Node: + data: int + next: Optional[Node] + + +class SortedLinkedList: + def __init__(self, ints: List[int]) -> None: + self.head: Optional[Node] = None + for i in reversed(sorted(ints)): + self.head = Node(i, self.head) + + def __iter__(self): + """ + >>> tuple(SortedLinkedList(test_data_odd)) == tuple(sorted(test_data_odd)) + True + >>> tuple(SortedLinkedList(test_data_even)) == tuple(sorted(test_data_even)) + True + """ + node = self.head + while node: + yield node.data + node = node.next + + def __len__(self): + """ + >>> for i in range(3): + ... len(SortedLinkedList(range(i))) == i + True + True + True + >>> len(SortedLinkedList(test_data_odd)) + 8 + """ + return len(tuple(iter(self))) + + def __str__(self): + """ + >>> str(SortedLinkedList([])) + '' + >>> str(SortedLinkedList(test_data_odd)) + '-11 -> -1 -> 0 -> 1 -> 3 -> 5 -> 7 -> 9' + >>> str(SortedLinkedList(test_data_even)) + '-2 -> 0 -> 2 -> 3 -> 4 -> 6 -> 8 -> 10' + """ + return " -> ".join([str(node) for node in self]) + + +def merge_lists( + sll_one: SortedLinkedList, sll_two: SortedLinkedList +) -> SortedLinkedList: """ - >>> n1 = Node(1) - >>> n2 = Node(2) - >>> n3 = Node(3) - >>> n4 = Node(4) - >>> n5 = Node(5) - >>> n6 = Node(6) - >>> n1.next = n3 - >>> n2.next = n4 - >>> n4.next = n5 - >>> n5.next = n6 - >>> head = merge_lists(n1, n2) - >>> ll = LinkedList() - >>> ll.head = head - >>> repr(ll) - '1->2->3->4->5->6' + >>> odd = SortedLinkedList(test_data_odd) + >>> even = SortedLinkedList(test_data_even) + >>> merged = merge_lists(odd, even) + >>> len(merged) + 16 + >>> str(merged) + '-11 -> -2 -> -1 -> 0 -> 0 -> 1 -> 2 -> 3 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10' + >>> list(merged) == list(sorted(test_data_odd + test_data_even)) + True """ - node_one = list_one - node_two = list_two - merged_list = current_node = Node(0) - - while node_one is not None and node_two is not None: - if node_one.data <= node_two.data: - current_node.next = node_one - node_one = node_one.next - else: - current_node.next = node_two - node_two = node_two.next - current_node = current_node.next - - if node_one is not None: - current_node.next = node_one - - if node_two is not None: - current_node.next = node_two - - return merged_list.next + return SortedLinkedList(list(sll_one) + list(sll_two)) def main() -> None: - list1 = [3, 9, 7, 5, 1] - list2 = [4, 6, 5, 2, 8, 7, 10, 3, 0] - ll1 = create_linked_list(list1) - ll2 = create_linked_list(list2) - new_head = merge_lists(ll1, ll2) - ll3 = LinkedList() - ll3.head = new_head - print(repr(ll3)) + sll_one = SortedLinkedList(list(test_data_odd)) + sll_two = SortedLinkedList(list(test_data_even)) + print(merge_lists(sll_one, sll_two)) if __name__ == "__main__": - main() import doctest doctest.testmod() + main() From 37de983f5b84aa2963e859c666bce0d488af7c27 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 3 Dec 2020 15:56:16 +0100 Subject: [PATCH 8/8] Update merge_two_lists.py --- .../linked_list/merge_two_lists.py | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/data_structures/linked_list/merge_two_lists.py b/data_structures/linked_list/merge_two_lists.py index 7aa84b21099d..96ec6b8abc85 100644 --- a/data_structures/linked_list/merge_two_lists.py +++ b/data_structures/linked_list/merge_two_lists.py @@ -1,12 +1,11 @@ """ Algorithm that merges two sorted linked lists into one sorted linked list. - -https://en.wikipedia.org/wiki/Linked_list """ from __future__ import annotations +from collections.abc import Iterable, Iterator from dataclasses import dataclass -from typing import List, Optional +from typing import Optional test_data_odd = (3, 9, -11, 0, 7, 5, 1, -1) test_data_even = (4, 6, 2, 0, 8, 10, 3, -2) @@ -19,12 +18,12 @@ class Node: class SortedLinkedList: - def __init__(self, ints: List[int]) -> None: + def __init__(self, ints: Iterable[int]) -> None: self.head: Optional[Node] = None for i in reversed(sorted(ints)): self.head = Node(i, self.head) - def __iter__(self): + def __iter__(self) -> Iterator[int]: """ >>> tuple(SortedLinkedList(test_data_odd)) == tuple(sorted(test_data_odd)) True @@ -36,7 +35,7 @@ def __iter__(self): yield node.data node = node.next - def __len__(self): + def __len__(self) -> int: """ >>> for i in range(3): ... len(SortedLinkedList(range(i))) == i @@ -48,7 +47,7 @@ def __len__(self): """ return len(tuple(iter(self))) - def __str__(self): + def __str__(self) -> str: """ >>> str(SortedLinkedList([])) '' @@ -64,9 +63,8 @@ def merge_lists( sll_one: SortedLinkedList, sll_two: SortedLinkedList ) -> SortedLinkedList: """ - >>> odd = SortedLinkedList(test_data_odd) - >>> even = SortedLinkedList(test_data_even) - >>> merged = merge_lists(odd, even) + >>> SSL = SortedLinkedList + >>> merged = merge_lists(SSL(test_data_odd), SSL(test_data_even)) >>> len(merged) 16 >>> str(merged) @@ -77,14 +75,9 @@ def merge_lists( return SortedLinkedList(list(sll_one) + list(sll_two)) -def main() -> None: - sll_one = SortedLinkedList(list(test_data_odd)) - sll_two = SortedLinkedList(list(test_data_even)) - print(merge_lists(sll_one, sll_two)) - - if __name__ == "__main__": import doctest doctest.testmod() - main() + SSL = SortedLinkedList + print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))