From 7ca20b7c65ac9367db624f1a747dbe5c59d67d90 Mon Sep 17 00:00:00 2001 From: Chiemezie Jacob Date: Wed, 18 Oct 2023 15:05:17 +0100 Subject: [PATCH 1/6] Added doctests to the swap_nodes file under linkedlist data structure --- data_structures/linked_list/swap_nodes.py | 89 ++++++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 31dcb02bfa9a..822be4440e13 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -24,18 +24,56 @@ def __init__(self) -> None: def print_list(self): """ Print the elements of the Linked List in order. + + Examples: + >>> new_list = LinkedList() + >>> new_list.push(0) + >>> new_list.push(2) + >>> new_list.push(2) + >>> new_list.push(3) + >>> new_list.push(4) + >>> new_list.print_list() + 4 3 2 2 0 + + Args: + No arguments. + + Returns: + None + + Raises: + None """ temp = self.head while temp is not None: - print(temp.data, end=" ") + if not temp.next: + print(temp.data) + else: + print(temp.data, end=" ") temp = temp.next - print() def push(self, new_data: Any) -> None: """ Add a new node with the given data to the beginning of the Linked List. + Args: new_data (Any): The data to be added to the new node. + + Examples: + >>> new_list = LinkedList() + >>> new_list.push(5) + >>> new_list.push(4) + >>> new_list.push(3) + >>> new_list.push(2) + >>> new_list.push(1) + >>> new_list.print_list() + 1 2 3 4 5 + + Returns: + None + + Raises: + None """ new_node = Node(new_data) new_node.next = self.head @@ -44,6 +82,7 @@ def push(self, new_data: Any) -> None: def swap_nodes(self, node_data_1, node_data_2) -> None: """ Swap the positions of two nodes in the Linked List based on their data values. + Args: node_data_1: Data value of the first node to be swapped. node_data_2: Data value of the second node to be swapped. @@ -51,6 +90,49 @@ def swap_nodes(self, node_data_1, node_data_2) -> None: Note: If either of the specified data values isn't found then, no swapping occurs. + + Examples: + When both values are present in a linkedlist. + >>> new_list = LinkedList() + >>> new_list.push(5) + >>> new_list.push(4) + >>> new_list.push(3) + >>> new_list.push(2) + >>> new_list.push(1) + >>> new_list.print_list() + 1 2 3 4 5 + >>> new_list.swap_nodes(1, 5) + >>> new_list.print_list() + 5 2 3 4 1 + + When just a single value is present in the linkedlist. + >>> second_list = LinkedList() + >>> second_list.push(6) + >>> second_list.push(7) + >>> second_list.push(8) + >>> second_list.push(9) + >>> second_list.swap_nodes(1, 6) is None + True + + When both values are absent in the linkedlist. + >>> second_list = LinkedList() + >>> second_list.push(10) + >>> second_list.push(9) + >>> second_list.push(8) + >>> second_list.push(7) + >>> second_list.swap_nodes(1, 3) is None + True + + When linkedlist is empty. + >>> second_list = LinkedList() + >>> second_list.swap_nodes(1, 3) is None + True + + Returns: + None + + Raises: + None """ if node_data_1 == node_data_2: return @@ -71,6 +153,9 @@ def swap_nodes(self, node_data_1, node_data_2) -> None: if __name__ == "__main__": + """ + Python script that outputs the swap of nodes in a linkedlist. + """ ll = LinkedList() for i in range(5, 0, -1): ll.push(i) From 7ca30052219b1af8d26ccd3f8fde7bf0724a3fff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 14:20:03 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/swap_nodes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 822be4440e13..48aeae75c3db 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -40,7 +40,7 @@ def print_list(self): Returns: None - + Raises: None """ @@ -71,7 +71,7 @@ def push(self, new_data: Any) -> None: Returns: None - + Raises: None """ @@ -130,7 +130,7 @@ def swap_nodes(self, node_data_1, node_data_2) -> None: Returns: None - + Raises: None """ From 46de3fd9fc7281b1db1f556c03838129be330816 Mon Sep 17 00:00:00 2001 From: Chiemezie Jacob Date: Sat, 21 Oct 2023 10:50:16 +0100 Subject: [PATCH 3/6] Added doctests to the swap_nodes file under linkedlist data structure --- data_structures/linked_list/swap_nodes.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 822be4440e13..b4b562ec88d5 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -40,9 +40,6 @@ def print_list(self): Returns: None - - Raises: - None """ temp = self.head while temp is not None: @@ -130,9 +127,6 @@ def swap_nodes(self, node_data_1, node_data_2) -> None: Returns: None - - Raises: - None """ if node_data_1 == node_data_2: return From cadec9a9584296d753792cb9d8c53256d451bc67 Mon Sep 17 00:00:00 2001 From: Chiemezie Jacob Date: Sat, 21 Oct 2023 11:01:12 +0100 Subject: [PATCH 4/6] Added doctests to the swap_nodes file under linkedlist data structure --- data_structures/linked_list/swap_nodes.py | 24 +++++++++-------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 11504d944744..755db788ec07 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -25,6 +25,12 @@ def print_list(self): """ Print the elements of the Linked List in order. + Args: + No arguments + + Returns: + None + Examples: >>> new_list = LinkedList() >>> new_list.push(0) @@ -34,12 +40,6 @@ def print_list(self): >>> new_list.push(4) >>> new_list.print_list() 4 3 2 2 0 - - Args: - No arguments. - - Returns: - None """ temp = self.head while temp is not None: @@ -56,6 +56,9 @@ def push(self, new_data: Any) -> None: Args: new_data (Any): The data to be added to the new node. + Returns: + None + Examples: >>> new_list = LinkedList() >>> new_list.push(5) @@ -65,12 +68,6 @@ def push(self, new_data: Any) -> None: >>> new_list.push(1) >>> new_list.print_list() 1 2 3 4 5 - - Returns: - None - - Raises: - None """ new_node = Node(new_data) new_node.next = self.head @@ -127,9 +124,6 @@ def swap_nodes(self, node_data_1, node_data_2) -> None: Returns: None - - Raises: - None """ if node_data_1 == node_data_2: return From 0f39846b3ac407fb7ef0c899f42b575f85bf7ed2 Mon Sep 17 00:00:00 2001 From: Chiemezie Jacob Date: Sat, 21 Oct 2023 11:06:47 +0100 Subject: [PATCH 5/6] Added doctests to the swap_nodes file under linkedlist data structure --- data_structures/linked_list/swap_nodes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 755db788ec07..8ad9406b015e 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -99,7 +99,7 @@ def swap_nodes(self, node_data_1, node_data_2) -> None: >>> new_list.print_list() 5 2 3 4 1 - When just a single value is present in the linkedlist. + When one value is present and the other isn't in the linkedlist. >>> second_list = LinkedList() >>> second_list.push(6) >>> second_list.push(7) From 824b0e92f79648000ce00975eb36782df44ffe1e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 22 Oct 2023 01:29:12 +0200 Subject: [PATCH 6/6] Update swap_nodes.py --- data_structures/linked_list/swap_nodes.py | 160 ++++++++++------------ 1 file changed, 74 insertions(+), 86 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 8ad9406b015e..d66512087d2d 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -1,53 +1,44 @@ +from __future__ import annotations + +from collections.abc import Iterator +from dataclasses import dataclass from typing import Any +@dataclass class Node: - def __init__(self, data: Any) -> None: - """ - Initialize a new Node with the given data. - - Args: - data: The data to be stored in the node. - - """ - self.data = data - self.next: Node | None = None # Reference to the next node + data: Any + next_node: Node | None = None +@dataclass class LinkedList: - def __init__(self) -> None: + head: Node | None = None + + def __iter__(self) -> Iterator: """ - Initialize an empty Linked List. + >>> linked_list = LinkedList() + >>> list(linked_list) + [] + >>> linked_list.push(0) + >>> tuple(linked_list) + (0,) """ - self.head: Node | None = None # Reference to the head (first node) + node = self.head + while node: + yield node.data + node = node.next_node - def print_list(self): + def __len__(self) -> int: """ - Print the elements of the Linked List in order. - - Args: - No arguments - - Returns: - None - - Examples: - >>> new_list = LinkedList() - >>> new_list.push(0) - >>> new_list.push(2) - >>> new_list.push(2) - >>> new_list.push(3) - >>> new_list.push(4) - >>> new_list.print_list() - 4 3 2 2 0 + >>> linked_list = LinkedList() + >>> len(linked_list) + 0 + >>> linked_list.push(0) + >>> len(linked_list) + 1 """ - temp = self.head - while temp is not None: - if not temp.next: - print(temp.data) - else: - print(temp.data, end=" ") - temp = temp.next + return sum(1 for _ in self) def push(self, new_data: Any) -> None: """ @@ -60,20 +51,20 @@ def push(self, new_data: Any) -> None: None Examples: - >>> new_list = LinkedList() - >>> new_list.push(5) - >>> new_list.push(4) - >>> new_list.push(3) - >>> new_list.push(2) - >>> new_list.push(1) - >>> new_list.print_list() - 1 2 3 4 5 + >>> linked_list = LinkedList() + >>> linked_list.push(5) + >>> linked_list.push(4) + >>> linked_list.push(3) + >>> linked_list.push(2) + >>> linked_list.push(1) + >>> list(linked_list) + [1, 2, 3, 4, 5] """ new_node = Node(new_data) - new_node.next = self.head + new_node.next_node = self.head self.head = new_node - def swap_nodes(self, node_data_1, node_data_2) -> None: + def swap_nodes(self, node_data_1: Any, node_data_2: Any) -> None: """ Swap the positions of two nodes in the Linked List based on their data values. @@ -86,20 +77,20 @@ def swap_nodes(self, node_data_1, node_data_2) -> None: If either of the specified data values isn't found then, no swapping occurs. Examples: - When both values are present in a linkedlist. - >>> new_list = LinkedList() - >>> new_list.push(5) - >>> new_list.push(4) - >>> new_list.push(3) - >>> new_list.push(2) - >>> new_list.push(1) - >>> new_list.print_list() - 1 2 3 4 5 - >>> new_list.swap_nodes(1, 5) - >>> new_list.print_list() - 5 2 3 4 1 - - When one value is present and the other isn't in the linkedlist. + When both values are present in a linked list. + >>> linked_list = LinkedList() + >>> linked_list.push(5) + >>> linked_list.push(4) + >>> linked_list.push(3) + >>> linked_list.push(2) + >>> linked_list.push(1) + >>> list(linked_list) + [1, 2, 3, 4, 5] + >>> linked_list.swap_nodes(1, 5) + >>> tuple(linked_list) + (5, 2, 3, 4, 1) + + When one value is present and the other isn't in the linked list. >>> second_list = LinkedList() >>> second_list.push(6) >>> second_list.push(7) @@ -108,7 +99,7 @@ def swap_nodes(self, node_data_1, node_data_2) -> None: >>> second_list.swap_nodes(1, 6) is None True - When both values are absent in the linkedlist. + When both values are absent in the linked list. >>> second_list = LinkedList() >>> second_list.push(10) >>> second_list.push(9) @@ -127,34 +118,31 @@ def swap_nodes(self, node_data_1, node_data_2) -> None: """ if node_data_1 == node_data_2: return - else: - node_1 = self.head - while node_1 is not None and node_1.data != node_data_1: - node_1 = node_1.next - - node_2 = self.head - while node_2 is not None and node_2.data != node_data_2: - node_2 = node_2.next - if node_1 is None or node_2 is None: - return - - # Swap the data values of the two nodes - node_1.data, node_2.data = node_2.data, node_1.data + node_1 = self.head + while node_1 and node_1.data != node_data_1: + node_1 = node_1.next_node + node_2 = self.head + while node_2 and node_2.data != node_data_2: + node_2 = node_2.next_node + if node_1 is None or node_2 is None: + return + # Swap the data values of the two nodes + node_1.data, node_2.data = node_2.data, node_1.data if __name__ == "__main__": """ - Python script that outputs the swap of nodes in a linkedlist. + Python script that outputs the swap of nodes in a linked list. """ - ll = LinkedList() - for i in range(5, 0, -1): - ll.push(i) + from doctest import testmod - print("Original Linked List:") - ll.print_list() - - ll.swap_nodes(1, 4) - print("After swapping the nodes whose data is 1 and 4:") + testmod() + linked_list = LinkedList() + for i in range(5, 0, -1): + linked_list.push(i) - ll.print_list() + print(f"Original Linked List: {list(linked_list)}") + linked_list.swap_nodes(1, 4) + print(f"Modified Linked List: {list(linked_list)}") + print("After swapping the nodes whose data is 1 and 4.")