From 44a47c49e88af58f3e673338d8cce4964a76be75 Mon Sep 17 00:00:00 2001 From: SiddhantTotade Date: Wed, 4 Oct 2023 00:48:10 +0530 Subject: [PATCH 01/21] docs : add comment in circular_linked_list.py and swap_nodes.py --- .../linked_list/circular_linked_list.py | 107 ++++++++++++++++-- data_structures/linked_list/swap_nodes.py | 42 ++++++- 2 files changed, 133 insertions(+), 16 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index d9544f4263a6..8c56c36ba389 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,4 +1,4 @@ -from __future__ import annotations +from __future__ import annotations # Enables forward annotations for type hinting from collections.abc import Iterator from typing import Any @@ -6,16 +6,31 @@ class Node: def __init__(self, data: Any): + """ + Initialize a new Node with the given data. + + Args: + data (Any): The data to be stored in the node. + """ self.data: Any = data - self.next: Node | None = None + self.next: Node | None = None # Reference to the next node class CircularLinkedList: def __init__(self): - self.head = None - self.tail = None + """ + Initialize an empty Circular Linked List. + """ + self.head = None # Reference to the head (first node) + self.tail = None # Reference to the tail (last node) def __iter__(self) -> Iterator[Any]: + """ + Iterator method to allow iterating through the Circular Linked List. + + Yields: + Iterator[Any]: The data of each node in the list. + """ node = self.head while self.head: yield node.data @@ -24,25 +39,59 @@ def __iter__(self) -> Iterator[Any]: break def __len__(self) -> int: + """ + Get the length (number of nodes) in the Circular Linked List. + + Returns: + int: The number of nodes in the list. + """ return sum(1 for _ in self) def __repr__(self): + """ + Generate a string representation of the Circular Linked List. + + Returns: + str: A string representation of the list in the format "data1->data2->...->dataN". + """ return "->".join(str(item) for item in iter(self)) def insert_tail(self, data: Any) -> None: + """ + Insert a node with the given data at the end of the Circular Linked List. + + Args: + data (Any): The data to be inserted. + """ self.insert_nth(len(self), data) def insert_head(self, data: Any) -> None: + """ + Insert a node with the given data at the beginning of the Circular Linked List. + + Args: + data (Any): The data to be inserted. + """ self.insert_nth(0, data) def insert_nth(self, index: int, data: Any) -> None: + """ + Insert a node with the given data at the specified index in the Circular Linked List. + + Args: + index (int): The index at which to insert the data. + data (Any): The data to be inserted. + + Raises: + IndexError: If the index is out of range. + """ if index < 0 or index > len(self): raise IndexError("list index out of range.") new_node = Node(data) if self.head is None: - new_node.next = new_node # first node points itself + new_node.next = new_node # First node points to itself self.tail = self.head = new_node - elif index == 0: # insert at head + elif index == 0: # Insert at the head new_node.next = self.head self.head = self.tail.next = new_node else: @@ -51,22 +100,52 @@ def insert_nth(self, index: int, data: Any) -> None: temp = temp.next new_node.next = temp.next temp.next = new_node - if index == len(self) - 1: # insert at tail + if index == len(self) - 1: # Insert at the tail self.tail = new_node def delete_front(self): + """ + Delete and return the data of the node at the front of the Circular Linked List. + + Returns: + Any: The data of the deleted node. + + Raises: + IndexError: If the list is empty. + """ return self.delete_nth(0) def delete_tail(self) -> Any: + """ + Delete and return the data of the node at the end of the Circular Linked List. + + Returns: + Any: The data of the deleted node. + + Raises: + IndexError: If the list is empty. + """ return self.delete_nth(len(self) - 1) def delete_nth(self, index: int = 0) -> Any: + """ + Delete and return the data of the node at the specified index in the Circular Linked List. + + Args: + index (int): The index of the node to be deleted. Defaults to 0. + + Returns: + Any: The data of the deleted node. + + Raises: + IndexError: If the index is out of range. + """ if not 0 <= index < len(self): raise IndexError("list index out of range.") delete_node = self.head - if self.head == self.tail: # just one node + if self.head == self.tail: # Just one node self.head = self.tail = None - elif index == 0: # delete head node + elif index == 0: # Delete head node self.tail.next = self.tail.next.next self.head = self.head.next else: @@ -75,16 +154,24 @@ def delete_nth(self, index: int = 0) -> Any: temp = temp.next delete_node = temp.next temp.next = temp.next.next - if index == len(self) - 1: # delete at tail + if index == len(self) - 1: # Delete at tail self.tail = temp return delete_node.data def is_empty(self) -> bool: + """ + Check if the Circular Linked List is empty. + + Returns: + bool: True if the list is empty, False otherwise. + """ return len(self) == 0 def test_circular_linked_list() -> None: """ + Test cases for the CircularLinkedList class. + >>> test_circular_linked_list() """ circular_linked_list = CircularLinkedList() diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 3f825756b3d2..072fd66f99b2 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -3,29 +3,57 @@ class Node: def __init__(self, data: Any): + """ + Initialize a new Node with the given data. + + Args: + data (Any): The data to be stored in the node. + """ self.data = data - self.next = None + self.next = None # Reference to the next node class LinkedList: def __init__(self): - self.head = None + """ + Initialize an empty Linked List. + """ + self.head = None # Reference to the head (first node) def print_list(self): + """ + Print the elements of the Linked List in order. + + This method iterates through the list and prints each element, separated by a space. + """ temp = self.head while temp is not None: print(temp.data, end=" ") temp = temp.next - print() + print() # Move to the next line after printing all elements - # adding nodes def push(self, new_data: Any): + """ + 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. + """ new_node = Node(new_data) new_node.next = self.head self.head = new_node - # swapping nodes def swap_nodes(self, node_data_1, node_data_2): + """ + Swap the positions of two nodes in the Linked List based on their data values. + + Args: + node_data_1 (Any): Data value of the first node to be swapped. + node_data_2 (Any): Data value of the second node to be swapped. + + Note: + If either of the specified data values is not found in the Linked List, no swapping occurs. + """ if node_data_1 == node_data_2: return else: @@ -40,6 +68,7 @@ def swap_nodes(self, node_data_1, node_data_2): 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 @@ -48,8 +77,9 @@ def swap_nodes(self, node_data_1, node_data_2): for i in range(5, 0, -1): ll.push(i) + print("Original Linked List:") ll.print_list() ll.swap_nodes(1, 4) - print("After swapping") + print("After swapping nodes 1 and 4:") ll.print_list() From b89baf80727007a4813fb842572b0fadcbbd6487 Mon Sep 17 00:00:00 2001 From: SiddhantTotade Date: Wed, 4 Oct 2023 01:05:01 +0530 Subject: [PATCH 02/21] docs : improve comments --- .../linked_list/circular_linked_list.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 8c56c36ba389..5f4041e7a37d 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -8,7 +8,6 @@ class Node: def __init__(self, data: Any): """ Initialize a new Node with the given data. - Args: data (Any): The data to be stored in the node. """ @@ -27,7 +26,6 @@ def __init__(self): def __iter__(self) -> Iterator[Any]: """ Iterator method to allow iterating through the Circular Linked List. - Yields: Iterator[Any]: The data of each node in the list. """ @@ -41,7 +39,6 @@ def __iter__(self) -> Iterator[Any]: def __len__(self) -> int: """ Get the length (number of nodes) in the Circular Linked List. - Returns: int: The number of nodes in the list. """ @@ -50,7 +47,6 @@ def __len__(self) -> int: def __repr__(self): """ Generate a string representation of the Circular Linked List. - Returns: str: A string representation of the list in the format "data1->data2->...->dataN". """ @@ -59,7 +55,6 @@ def __repr__(self): def insert_tail(self, data: Any) -> None: """ Insert a node with the given data at the end of the Circular Linked List. - Args: data (Any): The data to be inserted. """ @@ -68,7 +63,6 @@ def insert_tail(self, data: Any) -> None: def insert_head(self, data: Any) -> None: """ Insert a node with the given data at the beginning of the Circular Linked List. - Args: data (Any): The data to be inserted. """ @@ -77,7 +71,6 @@ def insert_head(self, data: Any) -> None: def insert_nth(self, index: int, data: Any) -> None: """ Insert a node with the given data at the specified index in the Circular Linked List. - Args: index (int): The index at which to insert the data. data (Any): The data to be inserted. @@ -106,10 +99,8 @@ def insert_nth(self, index: int, data: Any) -> None: def delete_front(self): """ Delete and return the data of the node at the front of the Circular Linked List. - Returns: Any: The data of the deleted node. - Raises: IndexError: If the list is empty. """ @@ -118,10 +109,8 @@ def delete_front(self): def delete_tail(self) -> Any: """ Delete and return the data of the node at the end of the Circular Linked List. - Returns: Any: The data of the deleted node. - Raises: IndexError: If the list is empty. """ @@ -130,13 +119,10 @@ def delete_tail(self) -> Any: def delete_nth(self, index: int = 0) -> Any: """ Delete and return the data of the node at the specified index in the Circular Linked List. - Args: index (int): The index of the node to be deleted. Defaults to 0. - Returns: Any: The data of the deleted node. - Raises: IndexError: If the index is out of range. """ @@ -161,7 +147,6 @@ def delete_nth(self, index: int = 0) -> Any: def is_empty(self) -> bool: """ Check if the Circular Linked List is empty. - Returns: bool: True if the list is empty, False otherwise. """ @@ -171,7 +156,6 @@ def is_empty(self) -> bool: def test_circular_linked_list() -> None: """ Test cases for the CircularLinkedList class. - >>> test_circular_linked_list() """ circular_linked_list = CircularLinkedList() From 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 Mon Sep 17 00:00:00 2001 From: SiddhantTotade Date: Wed, 4 Oct 2023 02:06:44 +0530 Subject: [PATCH 03/21] docs : improved docs and tested on pre-commit --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 25dba6f5a250..419b8e32d770 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ numpy opencv-python pandas pillow +pre-commit projectq qiskit ; python_version < '3.12' qiskit-aer ; python_version < '3.12' From 753da5a45d10115bf2f1e45a8aab49d6dfbe4ef3 Mon Sep 17 00:00:00 2001 From: SiddhantTotade Date: Wed, 4 Oct 2023 00:48:10 +0530 Subject: [PATCH 04/21] docs : add comment in circular_linked_list.py and swap_nodes.py --- .../linked_list/circular_linked_list.py | 107 ++++++++++++++++-- data_structures/linked_list/swap_nodes.py | 42 ++++++- 2 files changed, 133 insertions(+), 16 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index d9544f4263a6..8c56c36ba389 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,4 +1,4 @@ -from __future__ import annotations +from __future__ import annotations # Enables forward annotations for type hinting from collections.abc import Iterator from typing import Any @@ -6,16 +6,31 @@ class Node: def __init__(self, data: Any): + """ + Initialize a new Node with the given data. + + Args: + data (Any): The data to be stored in the node. + """ self.data: Any = data - self.next: Node | None = None + self.next: Node | None = None # Reference to the next node class CircularLinkedList: def __init__(self): - self.head = None - self.tail = None + """ + Initialize an empty Circular Linked List. + """ + self.head = None # Reference to the head (first node) + self.tail = None # Reference to the tail (last node) def __iter__(self) -> Iterator[Any]: + """ + Iterator method to allow iterating through the Circular Linked List. + + Yields: + Iterator[Any]: The data of each node in the list. + """ node = self.head while self.head: yield node.data @@ -24,25 +39,59 @@ def __iter__(self) -> Iterator[Any]: break def __len__(self) -> int: + """ + Get the length (number of nodes) in the Circular Linked List. + + Returns: + int: The number of nodes in the list. + """ return sum(1 for _ in self) def __repr__(self): + """ + Generate a string representation of the Circular Linked List. + + Returns: + str: A string representation of the list in the format "data1->data2->...->dataN". + """ return "->".join(str(item) for item in iter(self)) def insert_tail(self, data: Any) -> None: + """ + Insert a node with the given data at the end of the Circular Linked List. + + Args: + data (Any): The data to be inserted. + """ self.insert_nth(len(self), data) def insert_head(self, data: Any) -> None: + """ + Insert a node with the given data at the beginning of the Circular Linked List. + + Args: + data (Any): The data to be inserted. + """ self.insert_nth(0, data) def insert_nth(self, index: int, data: Any) -> None: + """ + Insert a node with the given data at the specified index in the Circular Linked List. + + Args: + index (int): The index at which to insert the data. + data (Any): The data to be inserted. + + Raises: + IndexError: If the index is out of range. + """ if index < 0 or index > len(self): raise IndexError("list index out of range.") new_node = Node(data) if self.head is None: - new_node.next = new_node # first node points itself + new_node.next = new_node # First node points to itself self.tail = self.head = new_node - elif index == 0: # insert at head + elif index == 0: # Insert at the head new_node.next = self.head self.head = self.tail.next = new_node else: @@ -51,22 +100,52 @@ def insert_nth(self, index: int, data: Any) -> None: temp = temp.next new_node.next = temp.next temp.next = new_node - if index == len(self) - 1: # insert at tail + if index == len(self) - 1: # Insert at the tail self.tail = new_node def delete_front(self): + """ + Delete and return the data of the node at the front of the Circular Linked List. + + Returns: + Any: The data of the deleted node. + + Raises: + IndexError: If the list is empty. + """ return self.delete_nth(0) def delete_tail(self) -> Any: + """ + Delete and return the data of the node at the end of the Circular Linked List. + + Returns: + Any: The data of the deleted node. + + Raises: + IndexError: If the list is empty. + """ return self.delete_nth(len(self) - 1) def delete_nth(self, index: int = 0) -> Any: + """ + Delete and return the data of the node at the specified index in the Circular Linked List. + + Args: + index (int): The index of the node to be deleted. Defaults to 0. + + Returns: + Any: The data of the deleted node. + + Raises: + IndexError: If the index is out of range. + """ if not 0 <= index < len(self): raise IndexError("list index out of range.") delete_node = self.head - if self.head == self.tail: # just one node + if self.head == self.tail: # Just one node self.head = self.tail = None - elif index == 0: # delete head node + elif index == 0: # Delete head node self.tail.next = self.tail.next.next self.head = self.head.next else: @@ -75,16 +154,24 @@ def delete_nth(self, index: int = 0) -> Any: temp = temp.next delete_node = temp.next temp.next = temp.next.next - if index == len(self) - 1: # delete at tail + if index == len(self) - 1: # Delete at tail self.tail = temp return delete_node.data def is_empty(self) -> bool: + """ + Check if the Circular Linked List is empty. + + Returns: + bool: True if the list is empty, False otherwise. + """ return len(self) == 0 def test_circular_linked_list() -> None: """ + Test cases for the CircularLinkedList class. + >>> test_circular_linked_list() """ circular_linked_list = CircularLinkedList() diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 3f825756b3d2..072fd66f99b2 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -3,29 +3,57 @@ class Node: def __init__(self, data: Any): + """ + Initialize a new Node with the given data. + + Args: + data (Any): The data to be stored in the node. + """ self.data = data - self.next = None + self.next = None # Reference to the next node class LinkedList: def __init__(self): - self.head = None + """ + Initialize an empty Linked List. + """ + self.head = None # Reference to the head (first node) def print_list(self): + """ + Print the elements of the Linked List in order. + + This method iterates through the list and prints each element, separated by a space. + """ temp = self.head while temp is not None: print(temp.data, end=" ") temp = temp.next - print() + print() # Move to the next line after printing all elements - # adding nodes def push(self, new_data: Any): + """ + 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. + """ new_node = Node(new_data) new_node.next = self.head self.head = new_node - # swapping nodes def swap_nodes(self, node_data_1, node_data_2): + """ + Swap the positions of two nodes in the Linked List based on their data values. + + Args: + node_data_1 (Any): Data value of the first node to be swapped. + node_data_2 (Any): Data value of the second node to be swapped. + + Note: + If either of the specified data values is not found in the Linked List, no swapping occurs. + """ if node_data_1 == node_data_2: return else: @@ -40,6 +68,7 @@ def swap_nodes(self, node_data_1, node_data_2): 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 @@ -48,8 +77,9 @@ def swap_nodes(self, node_data_1, node_data_2): for i in range(5, 0, -1): ll.push(i) + print("Original Linked List:") ll.print_list() ll.swap_nodes(1, 4) - print("After swapping") + print("After swapping nodes 1 and 4:") ll.print_list() From c19d8c6948d4ead3217fdd8768e810ae3c5f7b36 Mon Sep 17 00:00:00 2001 From: SiddhantTotade Date: Wed, 4 Oct 2023 01:05:01 +0530 Subject: [PATCH 05/21] docs : improve comments --- .../linked_list/circular_linked_list.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 8c56c36ba389..5f4041e7a37d 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -8,7 +8,6 @@ class Node: def __init__(self, data: Any): """ Initialize a new Node with the given data. - Args: data (Any): The data to be stored in the node. """ @@ -27,7 +26,6 @@ def __init__(self): def __iter__(self) -> Iterator[Any]: """ Iterator method to allow iterating through the Circular Linked List. - Yields: Iterator[Any]: The data of each node in the list. """ @@ -41,7 +39,6 @@ def __iter__(self) -> Iterator[Any]: def __len__(self) -> int: """ Get the length (number of nodes) in the Circular Linked List. - Returns: int: The number of nodes in the list. """ @@ -50,7 +47,6 @@ def __len__(self) -> int: def __repr__(self): """ Generate a string representation of the Circular Linked List. - Returns: str: A string representation of the list in the format "data1->data2->...->dataN". """ @@ -59,7 +55,6 @@ def __repr__(self): def insert_tail(self, data: Any) -> None: """ Insert a node with the given data at the end of the Circular Linked List. - Args: data (Any): The data to be inserted. """ @@ -68,7 +63,6 @@ def insert_tail(self, data: Any) -> None: def insert_head(self, data: Any) -> None: """ Insert a node with the given data at the beginning of the Circular Linked List. - Args: data (Any): The data to be inserted. """ @@ -77,7 +71,6 @@ def insert_head(self, data: Any) -> None: def insert_nth(self, index: int, data: Any) -> None: """ Insert a node with the given data at the specified index in the Circular Linked List. - Args: index (int): The index at which to insert the data. data (Any): The data to be inserted. @@ -106,10 +99,8 @@ def insert_nth(self, index: int, data: Any) -> None: def delete_front(self): """ Delete and return the data of the node at the front of the Circular Linked List. - Returns: Any: The data of the deleted node. - Raises: IndexError: If the list is empty. """ @@ -118,10 +109,8 @@ def delete_front(self): def delete_tail(self) -> Any: """ Delete and return the data of the node at the end of the Circular Linked List. - Returns: Any: The data of the deleted node. - Raises: IndexError: If the list is empty. """ @@ -130,13 +119,10 @@ def delete_tail(self) -> Any: def delete_nth(self, index: int = 0) -> Any: """ Delete and return the data of the node at the specified index in the Circular Linked List. - Args: index (int): The index of the node to be deleted. Defaults to 0. - Returns: Any: The data of the deleted node. - Raises: IndexError: If the index is out of range. """ @@ -161,7 +147,6 @@ def delete_nth(self, index: int = 0) -> Any: def is_empty(self) -> bool: """ Check if the Circular Linked List is empty. - Returns: bool: True if the list is empty, False otherwise. """ @@ -171,7 +156,6 @@ def is_empty(self) -> bool: def test_circular_linked_list() -> None: """ Test cases for the CircularLinkedList class. - >>> test_circular_linked_list() """ circular_linked_list = CircularLinkedList() From 3129fb311e1e645403f18365b71bc679972e0041 Mon Sep 17 00:00:00 2001 From: SiddhantTotade Date: Wed, 4 Oct 2023 02:06:44 +0530 Subject: [PATCH 06/21] docs : improved docs and tested on pre-commit --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 25dba6f5a250..419b8e32d770 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ numpy opencv-python pandas pillow +pre-commit projectq qiskit ; python_version < '3.12' qiskit-aer ; python_version < '3.12' From 6711e46e4bf9f4235387c060f6abcd3f96ca5e2a Mon Sep 17 00:00:00 2001 From: SiddhantTotade Date: Wed, 4 Oct 2023 09:12:18 +0530 Subject: [PATCH 07/21] docs : modified comments --- data_structures/linked_list/circular_linked_list.py | 12 ++++++------ data_structures/linked_list/swap_nodes.py | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 5f4041e7a37d..dbf05f20c39a 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,5 +1,7 @@ -from __future__ import annotations # Enables forward annotations for type hinting +# Enables forward annotations for type hinting +from __future__ import annotations +import doctest from collections.abc import Iterator from typing import Any @@ -48,7 +50,7 @@ def __repr__(self): """ Generate a string representation of the Circular Linked List. Returns: - str: A string representation of the list in the format "data1->data2->...->dataN". + str: A string representation of the list in the format "1->2->....->N". """ return "->".join(str(item) for item in iter(self)) @@ -70,7 +72,7 @@ def insert_head(self, data: Any) -> None: def insert_nth(self, index: int, data: Any) -> None: """ - Insert a node with the given data at the specified index in the Circular Linked List. + Insert and return the data of the node at the nth pos in Circular Linked List. Args: index (int): The index at which to insert the data. data (Any): The data to be inserted. @@ -118,7 +120,7 @@ def delete_tail(self) -> Any: def delete_nth(self, index: int = 0) -> Any: """ - Delete and return the data of the node at the specified index in the Circular Linked List. + Delete and return the data of the node at the nth pos in Circular Linked List. Args: index (int): The index of the node to be deleted. Defaults to 0. Returns: @@ -210,6 +212,4 @@ def test_circular_linked_list() -> None: if __name__ == "__main__": - import doctest - doctest.testmod() diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 072fd66f99b2..14c5fb8160dc 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -24,7 +24,7 @@ def print_list(self): """ Print the elements of the Linked List in order. - This method iterates through the list and prints each element, separated by a space. + This method iterates through the list and prints each element. """ temp = self.head while temp is not None: @@ -52,7 +52,7 @@ def swap_nodes(self, node_data_1, node_data_2): node_data_2 (Any): Data value of the second node to be swapped. Note: - If either of the specified data values is not found in the Linked List, no swapping occurs. + If either of the specified data values isn't found then, no swapping occurs. """ if node_data_1 == node_data_2: return From 64aff93c58782cfbaba38adfb53566ff3a74a6b5 Mon Sep 17 00:00:00 2001 From: Siddhant Totade Date: Wed, 4 Oct 2023 09:45:16 +0530 Subject: [PATCH 08/21] Update circular_linked_list.py --- .../linked_list/circular_linked_list.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index de219444b394..dbf05f20c39a 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,9 +1,5 @@ -<<<<<<< HEAD # Enables forward annotations for type hinting from __future__ import annotations -======= -from __future__ import annotations # Enables forward annotations for type hinting ->>>>>>> 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 import doctest from collections.abc import Iterator @@ -54,11 +50,7 @@ def __repr__(self): """ Generate a string representation of the Circular Linked List. Returns: -<<<<<<< HEAD str: A string representation of the list in the format "1->2->....->N". -======= - str: A string representation of the list in the format "data1->data2->...->dataN". ->>>>>>> 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 """ return "->".join(str(item) for item in iter(self)) @@ -80,11 +72,7 @@ def insert_head(self, data: Any) -> None: def insert_nth(self, index: int, data: Any) -> None: """ -<<<<<<< HEAD Insert and return the data of the node at the nth pos in Circular Linked List. -======= - Insert a node with the given data at the specified index in the Circular Linked List. ->>>>>>> 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 Args: index (int): The index at which to insert the data. data (Any): The data to be inserted. @@ -132,11 +120,7 @@ def delete_tail(self) -> Any: def delete_nth(self, index: int = 0) -> Any: """ -<<<<<<< HEAD Delete and return the data of the node at the nth pos in Circular Linked List. -======= - Delete and return the data of the node at the specified index in the Circular Linked List. ->>>>>>> 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 Args: index (int): The index of the node to be deleted. Defaults to 0. Returns: From f5ca8810e8768d378b59446b91a4e68ea4c61370 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Oct 2023 04:25:32 +0000 Subject: [PATCH 09/21] [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 | 36 +++++++++++------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 2601f1169959..59381d5b812f 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -22,13 +22,13 @@ def __init__(self): def print_list(self): """ - Print the elements of the Linked List in order. + Print the elements of the Linked List in order. -<<<<<<< HEAD - This method iterates through the list and prints each element. -======= - This method iterates through the list and prints each element, separated by a space. ->>>>>>> 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 + <<<<<<< HEAD + This method iterates through the list and prints each element. + ======= + This method iterates through the list and prints each element, separated by a space. + >>>>>>> 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 """ temp = self.head while temp is not None: @@ -49,18 +49,18 @@ def push(self, new_data: Any): def swap_nodes(self, node_data_1, node_data_2): """ - Swap the positions of two nodes in the Linked List based on their data values. - - Args: - node_data_1 (Any): Data value of the first node to be swapped. - node_data_2 (Any): Data value of the second node to be swapped. - - Note: -<<<<<<< HEAD - If either of the specified data values isn't found then, no swapping occurs. -======= - If either of the specified data values is not found in the Linked List, no swapping occurs. ->>>>>>> 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 + Swap the positions of two nodes in the Linked List based on their data values. + + Args: + node_data_1 (Any): Data value of the first node to be swapped. + node_data_2 (Any): Data value of the second node to be swapped. + + Note: + <<<<<<< HEAD + If either of the specified data values isn't found then, no swapping occurs. + ======= + If either of the specified data values is not found in the Linked List, no swapping occurs. + >>>>>>> 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 """ if node_data_1 == node_data_2: return From 926dc0430b6700f164a44e0f5adab0ecc6bcfbfa Mon Sep 17 00:00:00 2001 From: SiddhantTotade Date: Wed, 4 Oct 2023 10:07:39 +0530 Subject: [PATCH 10/21] docs : improved --- data_structures/linked_list/swap_nodes.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 2601f1169959..37bb1d3f8ae7 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -23,12 +23,7 @@ def __init__(self): def print_list(self): """ Print the elements of the Linked List in order. - -<<<<<<< HEAD This method iterates through the list and prints each element. -======= - This method iterates through the list and prints each element, separated by a space. ->>>>>>> 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 """ temp = self.head while temp is not None: @@ -39,7 +34,6 @@ def print_list(self): def push(self, new_data: Any): """ 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. """ @@ -50,17 +44,12 @@ def push(self, new_data: Any): def swap_nodes(self, node_data_1, node_data_2): """ Swap the positions of two nodes in the Linked List based on their data values. - Args: node_data_1 (Any): Data value of the first node to be swapped. node_data_2 (Any): Data value of the second node to be swapped. Note: -<<<<<<< HEAD If either of the specified data values isn't found then, no swapping occurs. -======= - If either of the specified data values is not found in the Linked List, no swapping occurs. ->>>>>>> 8a04d2e519f7a5c0efc182e2010cc324f5f0dee5 """ if node_data_1 == node_data_2: return From 228870810819bb3aa3d9efea703fe7b53bca3ebc Mon Sep 17 00:00:00 2001 From: Siddhant Totade Date: Wed, 4 Oct 2023 14:42:49 +0530 Subject: [PATCH 11/21] Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss --- data_structures/linked_list/circular_linked_list.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index dbf05f20c39a..95ecc9da828d 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,7 +1,6 @@ # Enables forward annotations for type hinting from __future__ import annotations -import doctest from collections.abc import Iterator from typing import Any From fa3b858f8b542b2ecfb707cc8d304c1beddda70f Mon Sep 17 00:00:00 2001 From: Siddhant Totade Date: Wed, 4 Oct 2023 14:43:02 +0530 Subject: [PATCH 12/21] Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss --- data_structures/linked_list/circular_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 95ecc9da828d..6234fad4d079 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -10,7 +10,7 @@ def __init__(self, data: Any): """ Initialize a new Node with the given data. Args: - data (Any): The data to be stored in the node. + data: The data to be stored in the node. """ self.data: Any = data self.next: Node | None = None # Reference to the next node From a918ca53c00db11394df21360f1199b4286c671d Mon Sep 17 00:00:00 2001 From: Siddhant Totade Date: Wed, 4 Oct 2023 14:43:13 +0530 Subject: [PATCH 13/21] Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss --- 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 65925c3a650e..c0988b963653 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -30,7 +30,7 @@ def print_list(self): temp = temp.next print() # Move to the next line after printing all elements - def push(self, new_data: Any): + def push(self, new_data: Any) -> None: """ Add a new node with the given data to the beginning of the Linked List. Args: From 6366b17523b1a28be56b5c465b28e72ef345bc21 Mon Sep 17 00:00:00 2001 From: Siddhant Totade Date: Wed, 4 Oct 2023 14:43:21 +0530 Subject: [PATCH 14/21] Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss --- 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 c0988b963653..89655a83c164 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -40,7 +40,7 @@ def push(self, new_data: Any) -> None: new_node.next = self.head self.head = new_node - def swap_nodes(self, node_data_1, node_data_2): + 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: From 01352c848719a6c166d3724d440e1954405141ce Mon Sep 17 00:00:00 2001 From: Siddhant Totade Date: Wed, 4 Oct 2023 14:43:29 +0530 Subject: [PATCH 15/21] Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss --- data_structures/linked_list/swap_nodes.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 89655a83c164..1b39d559991e 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -44,8 +44,9 @@ 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 (Any): Data value of the first node to be swapped. - node_data_2 (Any): Data value of the second node to be swapped. + node_data_1: Data value of the first node to be swapped. + node_data_2: Data value of the second node to be swapped. + Note: If either of the specified data values isn't found then, no swapping occurs. From dea9a9f24ceff221f935ad9e0f2e7b4771cc38a7 Mon Sep 17 00:00:00 2001 From: Siddhant Totade Date: Wed, 4 Oct 2023 14:43:36 +0530 Subject: [PATCH 16/21] Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss --- data_structures/linked_list/swap_nodes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 1b39d559991e..c5e94ea5c62b 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -78,5 +78,6 @@ def swap_nodes(self, node_data_1, node_data_2) -> None: ll.print_list() ll.swap_nodes(1, 4) - print("After swapping nodes 1 and 4:") + print("After swapping the nodes whose data is 1 and 4:") + ll.print_list() From 74a5ddc359fb2b945a6831142db219b78d5f5f8a Mon Sep 17 00:00:00 2001 From: Siddhant Totade Date: Wed, 4 Oct 2023 14:43:53 +0530 Subject: [PATCH 17/21] Update requirements.txt Co-authored-by: Christian Clauss --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 419b8e32d770..25dba6f5a250 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,6 @@ numpy opencv-python pandas pillow -pre-commit projectq qiskit ; python_version < '3.12' qiskit-aer ; python_version < '3.12' From 61a9e055c9b091c8b34831a0f9b92fce0a06367f Mon Sep 17 00:00:00 2001 From: Siddhant Totade Date: Wed, 4 Oct 2023 14:44:24 +0530 Subject: [PATCH 18/21] Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss --- data_structures/linked_list/circular_linked_list.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 6234fad4d079..57c76d8e5b06 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,4 +1,3 @@ -# Enables forward annotations for type hinting from __future__ import annotations from collections.abc import Iterator From bf265991d35caefb84aa54e36c84aebfc3169c58 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 4 Oct 2023 11:23:10 +0200 Subject: [PATCH 19/21] Apply suggestions from code review --- .../linked_list/circular_linked_list.py | 28 +++++++------------ data_structures/linked_list/swap_nodes.py | 10 ++++--- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 57c76d8e5b06..8b47895576ba 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -16,7 +16,7 @@ def __init__(self, data: Any): class CircularLinkedList: - def __init__(self): + def __init__(self) -> None: """ Initialize an empty Circular Linked List. """ @@ -25,9 +25,9 @@ def __init__(self): def __iter__(self) -> Iterator[Any]: """ - Iterator method to allow iterating through the Circular Linked List. + Iterate through all nodes in the Circular Linked List yielding their data. Yields: - Iterator[Any]: The data of each node in the list. + The data of each node in the linked list. """ node = self.head while self.head: @@ -39,41 +39,35 @@ def __iter__(self) -> Iterator[Any]: def __len__(self) -> int: """ Get the length (number of nodes) in the Circular Linked List. - Returns: - int: The number of nodes in the list. """ return sum(1 for _ in self) - def __repr__(self): + def __repr__(self) -> str: """ Generate a string representation of the Circular Linked List. Returns: - str: A string representation of the list in the format "1->2->....->N". + A string of the format "1->2->....->N". """ return "->".join(str(item) for item in iter(self)) def insert_tail(self, data: Any) -> None: """ Insert a node with the given data at the end of the Circular Linked List. - Args: - data (Any): The data to be inserted. """ self.insert_nth(len(self), data) def insert_head(self, data: Any) -> None: """ Insert a node with the given data at the beginning of the Circular Linked List. - Args: - data (Any): The data to be inserted. """ self.insert_nth(0, data) def insert_nth(self, index: int, data: Any) -> None: """ - Insert and return the data of the node at the nth pos in Circular Linked List. + Insert the data of the node at the nth pos in the Circular Linked List. Args: - index (int): The index at which to insert the data. - data (Any): The data to be inserted. + index: The index at which the data should be inserted. + data: The data to be inserted. Raises: IndexError: If the index is out of range. @@ -96,11 +90,9 @@ def insert_nth(self, index: int, data: Any) -> None: if index == len(self) - 1: # Insert at the tail self.tail = new_node - def delete_front(self): + def delete_front(self) -> Any: """ Delete and return the data of the node at the front of the Circular Linked List. - Returns: - Any: The data of the deleted node. Raises: IndexError: If the list is empty. """ @@ -112,7 +104,7 @@ def delete_tail(self) -> Any: Returns: Any: The data of the deleted node. Raises: - IndexError: If the list is empty. + IndexError: If the index is out of range. """ return self.delete_nth(len(self) - 1) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index c5e94ea5c62b..0f63d389cb8e 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -2,19 +2,21 @@ class Node: - def __init__(self, data: Any): + def __init__(self, data: Any) -> None: + """ Initialize a new Node with the given data. Args: - data (Any): The data to be stored in the node. + data: The data to be stored in the node. + """ self.data = data self.next = None # Reference to the next node class LinkedList: - def __init__(self): + def __init__(self) -> None: """ Initialize an empty Linked List. """ @@ -28,7 +30,7 @@ def print_list(self): while temp is not None: print(temp.data, end=" ") temp = temp.next - print() # Move to the next line after printing all elements + print() def push(self, new_data: Any) -> None: """ From 9ca01a75d7c8df8e8b5c8f025f2497a296e11dce Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Oct 2023 09:23:39 +0000 Subject: [PATCH 20/21] [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 | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 0f63d389cb8e..da6aa07a79fd 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -3,7 +3,6 @@ class Node: def __init__(self, data: Any) -> None: - """ Initialize a new Node with the given data. From 25dd039e2804fbdf69ea1ba8e90c035597bc1fe3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 4 Oct 2023 11:24:00 +0200 Subject: [PATCH 21/21] Update circular_linked_list.py --- data_structures/linked_list/circular_linked_list.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 8b47895576ba..72212f46be15 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -202,4 +202,6 @@ def test_circular_linked_list() -> None: if __name__ == "__main__": + import doctest + doctest.testmod()