From 8e8bea682ddb90a6ae2398ca9e5ddedf6946ef2d Mon Sep 17 00:00:00 2001 From: mendacium Date: Wed, 30 Aug 2023 16:34:20 +0530 Subject: [PATCH 01/13] added addAtPosition to simple linked list --- data_structures/linked_list/__init__.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 56b0e51baa93..00f80df05274 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -25,6 +25,23 @@ def add(self, item: Any) -> None: self.head = Node(item, self.head) self.size += 1 + def add_at_position(self, item: Any, position: int) -> bool: + if position < 0: + return False + + current = self.head + counter = 0 + while current and counter < position: + current = current.next + counter += 1 + + if current: # Check if current is not None + new_node = Node(item, current.next) + current.next = new_node + return True + else: + return False + def remove(self) -> Any: # Switched 'self.is_empty()' to 'self.head is None' # because mypy was considering the possibility that 'self.head' From a6fbb449ba43a5343516a444b414949b1b7b3e79 Mon Sep 17 00:00:00 2001 From: mendacium Date: Wed, 30 Aug 2023 16:40:29 +0530 Subject: [PATCH 02/13] added addAtPosition to simple linked list --- data_structures/linked_list/__init__.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 00f80df05274..f1cd8691bbeb 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -26,6 +26,24 @@ def add(self, item: Any) -> None: self.size += 1 def add_at_position(self, item: Any, position: int) -> bool: + """ + Adds a new node with the given item at the specified position in the linked list + + Args: + item (Any): The item to be added to the linked list. + position (int): The position at which the item should be inserted. + + Returns: + bool: True if the insertion was successful, False otherwise. + + >>> linked_list = LinkedList() + >>> linked_list.add(1) + >>> linked_list.add(2) + >>> linked_list.add(3) + >>> linked_list.add_at_position(10, 1) + True + + """ if position < 0: return False From e00d58ed1b068086aea35665342c687857a758fb Mon Sep 17 00:00:00 2001 From: mendacium Date: Wed, 6 Sep 2023 00:36:51 +0530 Subject: [PATCH 03/13] modified the add function to take an optional position command --- CONTRIBUTING.md | 2 +- data_structures/linked_list/__init__.py | 57 ++++++++++++++----------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4a1bb652738f..69210d0ed247 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,4 @@ -# Contributing guidelines + ## Before contributing diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index f1cd8691bbeb..ede7827891f4 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -21,44 +21,49 @@ def __init__(self) -> None: self.head: Node | None = None self.size = 0 - def add(self, item: Any) -> None: - self.head = Node(item, self.head) - self.size += 1 - - def add_at_position(self, item: Any, position: int) -> bool: + def add(self, item: Any, position: int = 0) -> bool: """ - Adds a new node with the given item at the specified position in the linked list + Add an item to the LinkedList at the specified position. + Default position is 0 (the head). Args: - item (Any): The item to be added to the linked list. - position (int): The position at which the item should be inserted. + item (Any): The item to add to the LinkedList. + position (int, optional): The position at which to add the item. + Defaults to 0. - Returns: - bool: True if the insertion was successful, False otherwise. + Raises: + ValueError: If the position is negative. >>> linked_list = LinkedList() >>> linked_list.add(1) >>> linked_list.add(2) >>> linked_list.add(3) - >>> linked_list.add_at_position(10, 1) - True - + >>> linked_list.add(4, 2) + >>> print(linked_list) + 3 --> 2 --> 4 --> 1 """ if position < 0: - return False - - current = self.head - counter = 0 - while current and counter < position: - current = current.next - counter += 1 - - if current: # Check if current is not None - new_node = Node(item, current.next) - current.next = new_node - return True + raise ValueError("Position must be non-negative") + + if position == 0 or self.head is None: + new_node = Node(item, self.head) + self.head = new_node else: - return False + current = self.head + counter = 0 + while current and counter < position: + current = current.next + counter += 1 + + if current: # Check if current is not None + new_node = Node(item, current.next) + current.next = new_node + return True + else: + return False + + self.size += 1 + return True def remove(self) -> Any: # Switched 'self.is_empty()' to 'self.head is None' From 823da1bd4b045f6ca39e692249e3bcb6f6af2fab Mon Sep 17 00:00:00 2001 From: mendacium Date: Wed, 6 Sep 2023 07:22:48 +0530 Subject: [PATCH 04/13] fixed type safety errors: --- data_structures/linked_list/__init__.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index ede7827891f4..ea418658309c 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -21,7 +21,7 @@ def __init__(self) -> None: self.head: Node | None = None self.size = 0 - def add(self, item: Any, position: int = 0) -> bool: + def add(self, item: Any, position: int = 0) -> None: """ Add an item to the LinkedList at the specified position. Default position is 0 (the head). @@ -58,12 +58,8 @@ def add(self, item: Any, position: int = 0) -> bool: if current: # Check if current is not None new_node = Node(item, current.next) current.next = new_node - return True - else: - return False self.size += 1 - return True def remove(self) -> Any: # Switched 'self.is_empty()' to 'self.head is None' From 142d318e53409c7c6766efd1df4b436d0bc90dfe Mon Sep 17 00:00:00 2001 From: mendacium Date: Wed, 6 Sep 2023 07:40:22 +0530 Subject: [PATCH 05/13] fixed type safety errors: --- data_structures/linked_list/__init__.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index ea418658309c..42248bc71b5a 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -21,7 +21,7 @@ def __init__(self) -> None: self.head: Node | None = None self.size = 0 - def add(self, item: Any, position: int = 0) -> None: + def add(self, item: Any, position: int = 0) -> bool: """ Add an item to the LinkedList at the specified position. Default position is 0 (the head). @@ -36,9 +36,13 @@ def add(self, item: Any, position: int = 0) -> None: >>> linked_list = LinkedList() >>> linked_list.add(1) + True >>> linked_list.add(2) + True >>> linked_list.add(3) + True >>> linked_list.add(4, 2) + True >>> print(linked_list) 3 --> 2 --> 4 --> 1 """ @@ -58,8 +62,11 @@ def add(self, item: Any, position: int = 0) -> None: if current: # Check if current is not None new_node = Node(item, current.next) current.next = new_node + return True + return False self.size += 1 + return True def remove(self) -> Any: # Switched 'self.is_empty()' to 'self.head is None' From a78cda08ba7e255a3d476aae6c544f936c505eed Mon Sep 17 00:00:00 2001 From: mendacium Date: Wed, 6 Sep 2023 07:48:50 +0530 Subject: [PATCH 06/13] fixed type safety errors: --- data_structures/linked_list/__init__.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 42248bc71b5a..ea418658309c 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -21,7 +21,7 @@ def __init__(self) -> None: self.head: Node | None = None self.size = 0 - def add(self, item: Any, position: int = 0) -> bool: + def add(self, item: Any, position: int = 0) -> None: """ Add an item to the LinkedList at the specified position. Default position is 0 (the head). @@ -36,13 +36,9 @@ def add(self, item: Any, position: int = 0) -> bool: >>> linked_list = LinkedList() >>> linked_list.add(1) - True >>> linked_list.add(2) - True >>> linked_list.add(3) - True >>> linked_list.add(4, 2) - True >>> print(linked_list) 3 --> 2 --> 4 --> 1 """ @@ -62,11 +58,8 @@ def add(self, item: Any, position: int = 0) -> bool: if current: # Check if current is not None new_node = Node(item, current.next) current.next = new_node - return True - return False self.size += 1 - return True def remove(self) -> Any: # Switched 'self.is_empty()' to 'self.head is None' From b4485c5e08cd363287f6a9cfc82c2c20c936274a Mon Sep 17 00:00:00 2001 From: mendacium Date: Wed, 6 Sep 2023 07:56:21 +0530 Subject: [PATCH 07/13] fixed type safety errors: --- data_structures/linked_list/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index ea418658309c..7d813b2f281a 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -51,7 +51,7 @@ def add(self, item: Any, position: int = 0) -> None: else: current = self.head counter = 0 - while current and counter < position: + while current and counter < position - 1: current = current.next counter += 1 From 6623820c1153b8253e4473e15d476624033ea9d6 Mon Sep 17 00:00:00 2001 From: mendacium Date: Wed, 6 Sep 2023 17:03:56 +0530 Subject: [PATCH 08/13] fixed size error --- data_structures/linked_list/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 7d813b2f281a..3ea6dce0c42d 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -58,8 +58,7 @@ def add(self, item: Any, position: int = 0) -> None: if current: # Check if current is not None new_node = Node(item, current.next) current.next = new_node - - self.size += 1 + self.size += 1 def remove(self) -> Any: # Switched 'self.is_empty()' to 'self.head is None' From 8b33a4b7b0f5aecda4747b222d8a00520f88b611 Mon Sep 17 00:00:00 2001 From: mendacium Date: Wed, 6 Sep 2023 17:12:47 +0530 Subject: [PATCH 09/13] fixed size error --- data_structures/linked_list/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 3ea6dce0c42d..da21eac056a3 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -58,7 +58,9 @@ def add(self, item: Any, position: int = 0) -> None: if current: # Check if current is not None new_node = Node(item, current.next) current.next = new_node - self.size += 1 + else: + raise ValueError("Out of bounds") + self.size += 1 def remove(self) -> Any: # Switched 'self.is_empty()' to 'self.head is None' From a07cf7f885cc6f35165ec90f987847aefea2a184 Mon Sep 17 00:00:00 2001 From: mendacium Date: Thu, 7 Sep 2023 11:02:47 +0530 Subject: [PATCH 10/13] added doctest and updates the else after checking if posiiton argument less than 0 or not --- data_structures/linked_list/__init__.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index da21eac056a3..853413b3feb0 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -33,6 +33,7 @@ def add(self, item: Any, position: int = 0) -> None: Raises: ValueError: If the position is negative. + ValueError: If the position is Out of Bounds. >>> linked_list = LinkedList() >>> linked_list.add(1) @@ -50,16 +51,12 @@ def add(self, item: Any, position: int = 0) -> None: self.head = new_node else: current = self.head - counter = 0 - while current and counter < position - 1: + for _ in range(position - 1): current = current.next - counter += 1 - - if current: # Check if current is not None - new_node = Node(item, current.next) - current.next = new_node - else: - raise ValueError("Out of bounds") + if current is None: + raise ValueError("Out of bounds") + new_node = Node(item, current.next) + current.next = new_node self.size += 1 def remove(self) -> Any: From 4a32c1f47dfd29ca85c96f88cbaec025df817876 Mon Sep 17 00:00:00 2001 From: mendacium Date: Thu, 7 Sep 2023 11:35:35 +0530 Subject: [PATCH 11/13] added doctest and updates the else after checking if posiiton argument less than 0 or not --- data_structures/linked_list/__init__.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 853413b3feb0..00b939b35b6c 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -29,11 +29,10 @@ def add(self, item: Any, position: int = 0) -> None: Args: item (Any): The item to add to the LinkedList. position (int, optional): The position at which to add the item. - Defaults to 0. + Defaults to 0. Raises: - ValueError: If the position is negative. - ValueError: If the position is Out of Bounds. + ValueError: If the position is negative or out of bounds. >>> linked_list = LinkedList() >>> linked_list.add(1) @@ -42,6 +41,17 @@ def add(self, item: Any, position: int = 0) -> None: >>> linked_list.add(4, 2) >>> print(linked_list) 3 --> 2 --> 4 --> 1 + + # Test adding to a negative position + >>> linked_list.add(5, -3) + Traceback (most recent call last): + ... + ValueError: Position must be non-negative + + # Test adding to an out-of-bounds position + >>> linked_list.add(5, 4) + >>> print(linked_list) + 3 --> 2 --> 4 --> 1 --> 5 """ if position < 0: raise ValueError("Position must be non-negative") From 46cbb2b04f331d621adbf2d65d22dba91f2f1961 Mon Sep 17 00:00:00 2001 From: mendacium Date: Fri, 8 Sep 2023 10:33:37 +0530 Subject: [PATCH 12/13] fixed the contributing.md mistake --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 69210d0ed247..4a1bb652738f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,4 @@ - +# Contributing guidelines ## Before contributing From 542d79550cb4c3c3155c8ceeff33eec900aa9107 Mon Sep 17 00:00:00 2001 From: mendacium Date: Fri, 8 Sep 2023 10:42:20 +0530 Subject: [PATCH 13/13] added doctest for out of bounds position value, both negative and positive --- data_structures/linked_list/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 00b939b35b6c..225113f72cee 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -49,6 +49,10 @@ def add(self, item: Any, position: int = 0) -> None: ValueError: Position must be non-negative # Test adding to an out-of-bounds position + >>> linked_list.add(5,7) + Traceback (most recent call last): + ... + ValueError: Out of bounds >>> linked_list.add(5, 4) >>> print(linked_list) 3 --> 2 --> 4 --> 1 --> 5