From 0a1c71e34de2cc1aebe43be33e8b5e90965f31af Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 12:53:22 +0500 Subject: [PATCH 01/46] Sorted linked list added --- .../linked_list/sorted_linked_list.py | 223 ++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 data_structures/linked_list/sorted_linked_list.py diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py new file mode 100644 index 000000000000..60c240080bcb --- /dev/null +++ b/data_structures/linked_list/sorted_linked_list.py @@ -0,0 +1,223 @@ +from __future__ import annotations + +from collections.abc import Iterator +from dataclasses import dataclass + +""" +This is a sorted linked list class that +creates a sorted linked list of integer datatype +""" + +@dataclass +class Node: + def __init__(self, data): + self.data: int = data + self.next_node: Node | None = None + + def __repr__(self): + return f"Node({self.data}, {self.next_node})" + +class SortedLinedList: + def __init__(self): + self.numNodes : int = 0 + self.head: Node | None = None + self.tail: Node | None = None + + def __repr__(self): + nodes = [] + temp = self.head + while temp: + nodes.append(str(temp.data)) + temp = temp.next_node + return f"SortedLinkedList({', '.join(nodes)})" + + def insert(self, data: int): + """This Function inserts node in it's sorted position + This function can be re written for any data type but + the comparator her must have to be changed + + Args: + data (int): the data of linked list + """ + new_node = Node(data) + if self.head is None: + self.head = new_node + self.tail = new_node + elif data < self.head.data: + new_node.next_node = self.head + self.head = new_node + else: + temp_node = self.head + while temp_node.next_node and temp_node.next_node.data < data: + temp_node = temp_node.next_node + new_node.next_node = temp_node.next_node + temp_node.next_node = new_node + if new_node.next_node is None: + self.tail = new_node + self.numNodes += 1 + + def display(self): + """This function displays whole list + """ + temp=self.head + while temp: + print(temp.data,end=" ") + temp=temp.next_node + print() + + def delete(self, data: int) -> bool: + """This Function deletes first appearance of node with + data from it's sorted position + + This function can be re written for any data type but + the comparator her must have to be changed + + Args: + data (int): the data of the node that is needed to be deleted + + Returns: + bool: status whether the node got deleted or not + """ + if self.head is None: + return False + + if self.head.data == data: + self.head = self.head.next_node + if self.head is None: + self.tail = None + return True + + temp_node = self.head + while temp_node.next_node: + if temp_node.next_node.data == data: + temp_node.next_node = temp_node.next_node.next_node + if temp_node.next_node is None: + self.tail = temp_node + return True + temp_node = temp_node.next_node + + return False + + def search(self, data: int) -> bool: + """This function searches the data given input from user + and return whether the data exists or not + + Args: + data (int): Data to be searched + + Returns: + bool: flag indicating whether data exists or not + """ + temp = self.head + while temp: + if temp.data == data: + return True + temp = temp.next_node + return False + def is_empty(self) -> bool: + """This function will check whether the list is empty or not + + Returns: + bool: flag indicating whether list is empty or not + """ + return self.head is None + + def length (self) -> int: + """This function returns the length of the linked list + + + Returns: + int: The length of linked list + """ + return numNodes + + def min_value(self) -> int | None: + """This function will return minimum value + + Returns: + int | None: min value or None if list is empty + """ + if self.head is None: + return None + return self.head.data + + def max_value(self) -> int | None: + """This function will return maximum value + + + Returns: + int | None: max value or None if list is empty + """ + if self.tail is None: + return None + return self.tail.data + + def remove_duplicates(self): + """This Function will remove the duplicates from the list + """ + temp = self.head + while temp and temp.next_node: + if temp.data == temp.next_node.data: + temp.next_node = temp.next_node.next_node + else: + temp = temp.next_node + + def reverse(self): + """This function will reveres the list + """ + prev = None + current = self.head + while current: + next_node = current.next_node + current.next_node = prev + prev = current + current = next_node + self.head, self.tail = self.tail, self.head + + def merge(self, other_list: SortedLinkedList): + """This Function will merge the input list with current list + + Args: + other_list (SortedLinkedList): The list to be merged + """ + if other_list.head is None: + return + if self.head is None: + self.head = other_list.head + self.tail = other_list.tail + return + self.tail.next_node = other_list.head + self.tail = other_list.tail + + +if __name__ == "__main__": + linkedList=SortedLinedList() + while True: + print("Enter") + print("1. Insert") + print("2. Display") + print("3. Delete") + print("4. Exit") + choice= input("Enter your choice: ") + + if choice == '1': + data = int(input("Enter a number: ")) + linkedList.insert(data) + elif choice == '2': + linkedList.display() + elif choice == '3': + data = int(input("Enter the data to delete: ")) + if linkedList.delete(data): + print("Node with data {} deleted successfully".format(data)) + else: + print("Node with data {} not found in the list".format(data)) + elif choice == '4': + break + else: + print("Wrong input") + + + + + + \ No newline at end of file From 583e5649878ad22494b784665d7b940b407d36ca Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 08:03:02 +0000 Subject: [PATCH 02/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../linked_list/sorted_linked_list.py | 86 +++++++++---------- 1 file changed, 40 insertions(+), 46 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 60c240080bcb..eb5ddda42e47 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -4,25 +4,27 @@ from dataclasses import dataclass """ -This is a sorted linked list class that -creates a sorted linked list of integer datatype +This is a sorted linked list class that +creates a sorted linked list of integer datatype """ + @dataclass class Node: def __init__(self, data): self.data: int = data self.next_node: Node | None = None - + def __repr__(self): return f"Node({self.data}, {self.next_node})" - + + class SortedLinedList: def __init__(self): - self.numNodes : int = 0 + self.numNodes: int = 0 self.head: Node | None = None self.tail: Node | None = None - + def __repr__(self): nodes = [] temp = self.head @@ -30,12 +32,12 @@ def __repr__(self): nodes.append(str(temp.data)) temp = temp.next_node return f"SortedLinkedList({', '.join(nodes)})" - + def insert(self, data: int): """This Function inserts node in it's sorted position - This function can be re written for any data type but + This function can be re written for any data type but the comparator her must have to be changed - + Args: data (int): the data of linked list """ @@ -55,21 +57,20 @@ def insert(self, data: int): if new_node.next_node is None: self.tail = new_node self.numNodes += 1 - + def display(self): - """This function displays whole list - """ - temp=self.head + """This function displays whole list""" + temp = self.head while temp: - print(temp.data,end=" ") - temp=temp.next_node + print(temp.data, end=" ") + temp = temp.next_node print() - + def delete(self, data: int) -> bool: - """This Function deletes first appearance of node with - data from it's sorted position - - This function can be re written for any data type but + """This Function deletes first appearance of node with + data from it's sorted position + + This function can be re written for any data type but the comparator her must have to be changed Args: @@ -97,9 +98,9 @@ def delete(self, data: int) -> bool: temp_node = temp_node.next_node return False - + def search(self, data: int) -> bool: - """This function searches the data given input from user + """This function searches the data given input from user and return whether the data exists or not Args: @@ -114,6 +115,7 @@ def search(self, data: int) -> bool: return True temp = temp.next_node return False + def is_empty(self) -> bool: """This function will check whether the list is empty or not @@ -121,8 +123,8 @@ def is_empty(self) -> bool: bool: flag indicating whether list is empty or not """ return self.head is None - - def length (self) -> int: + + def length(self) -> int: """This function returns the length of the linked list @@ -130,7 +132,7 @@ def length (self) -> int: int: The length of linked list """ return numNodes - + def min_value(self) -> int | None: """This function will return minimum value @@ -140,7 +142,7 @@ def min_value(self) -> int | None: if self.head is None: return None return self.head.data - + def max_value(self) -> int | None: """This function will return maximum value @@ -151,20 +153,18 @@ def max_value(self) -> int | None: if self.tail is None: return None return self.tail.data - + def remove_duplicates(self): - """This Function will remove the duplicates from the list - """ + """This Function will remove the duplicates from the list""" temp = self.head while temp and temp.next_node: if temp.data == temp.next_node.data: temp.next_node = temp.next_node.next_node else: temp = temp.next_node - + def reverse(self): - """This function will reveres the list - """ + """This function will reveres the list""" prev = None current = self.head while current: @@ -188,36 +188,30 @@ def merge(self, other_list: SortedLinkedList): return self.tail.next_node = other_list.head self.tail = other_list.tail - + if __name__ == "__main__": - linkedList=SortedLinedList() + linkedList = SortedLinedList() while True: print("Enter") print("1. Insert") print("2. Display") print("3. Delete") print("4. Exit") - choice= input("Enter your choice: ") - - if choice == '1': + choice = input("Enter your choice: ") + + if choice == "1": data = int(input("Enter a number: ")) linkedList.insert(data) - elif choice == '2': + elif choice == "2": linkedList.display() - elif choice == '3': + elif choice == "3": data = int(input("Enter the data to delete: ")) if linkedList.delete(data): print("Node with data {} deleted successfully".format(data)) else: print("Node with data {} not found in the list".format(data)) - elif choice == '4': + elif choice == "4": break else: print("Wrong input") - - - - - - \ No newline at end of file From 4ae57dc896e931e51784f812f4557a1a8653393f Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 13:12:39 +0500 Subject: [PATCH 03/46] Corrected errors in ruff test --- data_structures/linked_list/sorted_linked_list.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 60c240080bcb..de47f5b480ab 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -1,6 +1,5 @@ from __future__ import annotations -from collections.abc import Iterator from dataclasses import dataclass """ @@ -17,7 +16,7 @@ def __init__(self, data): def __repr__(self): return f"Node({self.data}, {self.next_node})" -class SortedLinedList: +class SortedLinkedList: def __init__(self): self.numNodes : int = 0 self.head: Node | None = None @@ -191,7 +190,7 @@ def merge(self, other_list: SortedLinkedList): if __name__ == "__main__": - linkedList=SortedLinedList() + linked_list=SortedLinkedList() while True: print("Enter") print("1. Insert") @@ -202,15 +201,15 @@ def merge(self, other_list: SortedLinkedList): if choice == '1': data = int(input("Enter a number: ")) - linkedList.insert(data) + linked_list.insert(data) elif choice == '2': - linkedList.display() + linked_list.display() elif choice == '3': data = int(input("Enter the data to delete: ")) - if linkedList.delete(data): - print("Node with data {} deleted successfully".format(data)) + if linked_list.delete(data): + print(f"Node with data {data} deleted successfully") else: - print("Node with data {} not found in the list".format(data)) + print(f"Node with data {data} not found in the list") elif choice == '4': break else: From 187f4da98adc783f3cca9ddabc69328459122f8b Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 13:25:32 +0500 Subject: [PATCH 04/46] Errors from ruff tests solved --- data_structures/linked_list/sorted_linked_list.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 89e9a6c59006..4e3da7d2952b 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -18,7 +18,7 @@ def __repr__(self): return f"Node({self.data}, {self.next_node})" -class SortedLinedList: +class SortedLinkedList: def __init__(self): self.numNodes: int = 0 self.head: Node | None = None @@ -130,7 +130,7 @@ def length(self) -> int: Returns: int: The length of linked list """ - return numNodes + return self.numNodes def min_value(self) -> int | None: """This function will return minimum value @@ -190,7 +190,7 @@ def merge(self, other_list: SortedLinkedList): if __name__ == "__main__": - linkedList = SortedLinedList() + linked_list = SortedLinedList() while True: print("Enter") print("1. Insert") @@ -201,9 +201,9 @@ def merge(self, other_list: SortedLinkedList): if choice == "1": data = int(input("Enter a number: ")) - linkedList.insert(data) + linked_list.insert(data) elif choice == "2": - linkedList.display() + linked_list.display() elif choice == "3": data = int(input("Enter the data to delete: ")) if linked_list.delete(data): From 8c1d01684dd64efc9aaeba1ada9811163fa03d5a Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 13:29:22 +0500 Subject: [PATCH 05/46] format call removed --- data_structures/linked_list/sorted_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 4e3da7d2952b..93a82bdf0562 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -209,7 +209,7 @@ def merge(self, other_list: SortedLinkedList): if linked_list.delete(data): print(f"Node with data {data} deleted successfully") else: - print("Node with data {} not found in the list".format(data)) + print(f"Node with data {data} not found in the list") elif choice == "4": break else: From 156392f49f24727fa2b065b132fa2d06ede0810f Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 13:33:55 +0500 Subject: [PATCH 06/46] return types added --- .../linked_list/sorted_linked_list.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 93a82bdf0562..44caa941b0ef 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -10,21 +10,21 @@ @dataclass class Node: - def __init__(self, data): + def __init__(self, data) -> None: self.data: int = data self.next_node: Node | None = None - def __repr__(self): + def __repr__(self) -> str: return f"Node({self.data}, {self.next_node})" class SortedLinkedList: - def __init__(self): + def __init__(self) -> None: self.numNodes: int = 0 self.head: Node | None = None self.tail: Node | None = None - def __repr__(self): + def __repr__(self) -> str: nodes = [] temp = self.head while temp: @@ -32,7 +32,7 @@ def __repr__(self): temp = temp.next_node return f"SortedLinkedList({', '.join(nodes)})" - def insert(self, data: int): + def insert(self, data: int) -> None: """This Function inserts node in it's sorted position This function can be re written for any data type but the comparator her must have to be changed @@ -57,7 +57,7 @@ def insert(self, data: int): self.tail = new_node self.numNodes += 1 - def display(self): + def display(self) -> None: """This function displays whole list""" temp = self.head while temp: @@ -153,7 +153,7 @@ def max_value(self) -> int | None: return None return self.tail.data - def remove_duplicates(self): + def remove_duplicates(self) -> None: """This Function will remove the duplicates from the list""" temp = self.head while temp and temp.next_node: @@ -162,7 +162,7 @@ def remove_duplicates(self): else: temp = temp.next_node - def reverse(self): + def reverse(self) -> None: """This function will reveres the list""" prev = None current = self.head @@ -173,7 +173,7 @@ def reverse(self): current = next_node self.head, self.tail = self.tail, self.head - def merge(self, other_list: SortedLinkedList): + def merge(self, other_list: SortedLinkedList) -> None: """This Function will merge the input list with current list Args: From eec1c3a98bea1a70d5bb16b8d3d390fee30a1884 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 14:05:02 +0500 Subject: [PATCH 07/46] Doctests added --- .../linked_list/sorted_linked_list.py | 180 ++++++++++++++++-- 1 file changed, 162 insertions(+), 18 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 44caa941b0ef..0bb863ca5315 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -2,29 +2,62 @@ from dataclasses import dataclass -""" -This is a sorted linked list class that -creates a sorted linked list of integer datatype -""" +# This is a sorted linked list class that creates a sorted linked list of integer datatype @dataclass class Node: + """ + Create and initialize Node class instance. + >>> Node(20) + Node(20) + >>> Node(27) + Node(27) + >>> Node(None) + Node(None) + """ def __init__(self, data) -> None: self.data: int = data self.next_node: Node | None = None def __repr__(self) -> str: - return f"Node({self.data}, {self.next_node})" + """ + Get the string representation of this node. + >>> Node(10).__repr__() + 'Node(10)' + >>> repr(Node(10)) + 'Node(10)' + >>> str(Node(10)) + 'Node(10)' + >>> Node(10) + Node(10) + """ + return f"Node({self.data})" class SortedLinkedList: def __init__(self) -> None: + """ + Create and initialize LinkedList class instance. + >>> linked_list = LinkedList() + >>> linked_list.head is None + True + """ self.numNodes: int = 0 self.head: Node | None = None self.tail: Node | None = None def __repr__(self) -> str: + """ + >>> linkedList=SortedLinedList() + >>> linkedList.insert(2) + >>> linkedList.insert(12) + >>> linkedList.insert(21) + >>> linkedList.insert(23) + >>> linkedList.insert(72) + >>> linkedList.__repr__() + SortedLinkedList(2, 12, 21, 23, 72) + """ nodes = [] temp = self.head while temp: @@ -39,6 +72,14 @@ def insert(self, data: int) -> None: Args: data (int): the data of linked list + + Doctests + >>> linked_list = SortedLinkedList() + >>> linked_list.insert_tail(32) + >>> linked_list.insert_tail(57) + >>> linked_list.insert_tail(45) + >>> linked_list + SortedLinkedList(32, 45, 57) """ new_node = Node(data) if self.head is None: @@ -58,7 +99,18 @@ def insert(self, data: int) -> None: self.numNodes += 1 def display(self) -> None: - """This function displays whole list""" + """ + This function displays whole list + + + Doctests + >>> linkedList=SortedLinedList() + >>> linkedList.insert(32) + >>> linkedList.insert(57) + >>> linkedList.insert(45) + >>> linkedList.display() + 32 45 57 + """ temp = self.head while temp: print(temp.data, end=" ") @@ -77,6 +129,19 @@ def delete(self, data: int) -> bool: Returns: bool: status whether the node got deleted or not + + Doctests + + >>> linkedList=SortedLinedList() + >>> linkedList.insert(32) + >>> linkedList.insert(57) + >>> linkedList.insert(45) + >>> linkedList.display() + 32 45 57 + >>> linkedList.delete(45) + True + >>> linkedList.display() + 32 57 """ if self.head is None: return False @@ -107,6 +172,16 @@ def search(self, data: int) -> bool: Returns: bool: flag indicating whether data exists or not + + Doctests + >>> linkedList=SortedLinedList() + >>> linkedList.insert(32) + >>> linkedList.insert(57) + >>> linkedList.insert(45) + >>> linkedList.search(45) + True + >>> linkedList.search(90) + False """ temp = self.head while temp: @@ -120,7 +195,20 @@ def is_empty(self) -> bool: Returns: bool: flag indicating whether list is empty or not + + Doctests + + >>> linkedList=SortedLinedList() + >>> linkedList.is_empty() + True + >>> linkedList.insert(32) + >>> linkedList.insert(57) + >>> linkedList.insert(45) + >>> linkedList.search(45) + >>> linkedList.is_empty() + False """ + return self.head is None def length(self) -> int: @@ -129,6 +217,25 @@ def length(self) -> int: Returns: int: The length of linked list + + Doctests + + >>> linkedList=SortedLinedList() + >>> linkedList.length() + 0 + >>> linkedList.insert(32) + >>> linkedList.length() + 1 + >>> linkedList.insert(57) + >>> linkedList.length() + 2 + >>> linkedList.insert(45) + >>> linkedList.length() + 3 + >>> linkedList.delete(45) + True + >>> linkedList.length() + 2 """ return self.numNodes @@ -137,6 +244,15 @@ def min_value(self) -> int | None: Returns: int | None: min value or None if list is empty + + Doctests + + >>> linkedList=SortedLinedList() + >>> linkedList.insert(32) + >>> linkedList.insert(57) + >>> linkedList.insert(45) + >>> linkedList.min_value() + 32 """ if self.head is None: return None @@ -148,13 +264,38 @@ def max_value(self) -> int | None: Returns: int | None: max value or None if list is empty + + Doctests + + >>> linkedList=SortedLinedList() + >>> linkedList.insert(32) + >>> linkedList.insert(57) + >>> linkedList.insert(45) + >>> linkedList.max_value() + 57 """ if self.tail is None: return None return self.tail.data def remove_duplicates(self) -> None: - """This Function will remove the duplicates from the list""" + """ + This Function will remove the duplicates from the list + + Doctests + + >>> linkedList=SortedLinedList() + >>> linkedList.insert(32) + >>> linkedList.insert(57) + >>> linkedList.insert(45) + >>> linkedList.insert(45) + >>> linkedList.display() + 32 45 45 57 + >>> linkedList.remove_duplicates() + >>> linkedList.display() + 32 45 57 + """ + temp = self.head while temp and temp.next_node: if temp.data == temp.next_node.data: @@ -162,22 +303,25 @@ def remove_duplicates(self) -> None: else: temp = temp.next_node - def reverse(self) -> None: - """This function will reveres the list""" - prev = None - current = self.head - while current: - next_node = current.next_node - current.next_node = prev - prev = current - current = next_node - self.head, self.tail = self.tail, self.head - def merge(self, other_list: SortedLinkedList) -> None: """This Function will merge the input list with current list Args: other_list (SortedLinkedList): The list to be merged + + Doctests + + >>> linkedList=SortedLinedList() + >>> linkedList.insert(32) + >>> linkedList.insert(57) + >>> linkedList.insert(45) + >>> linkedList2=SortedLinedList() + >>> linkedList2.insert(23) + >>> linkedList2.insert(47) + >>> linkedList2.insert(95) + >>> linkedList.merge(linkedList2) + >>> linkedList.display() + 23 32 45 47 57 95 """ if other_list.head is None: return From e89bb5c3a9cbfffd73f99c59d53cfd2c5ef55567 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 09:05:40 +0000 Subject: [PATCH 08/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../linked_list/sorted_linked_list.py | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 0bb863ca5315..d3d305314b30 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -16,6 +16,7 @@ class Node: >>> Node(None) Node(None) """ + def __init__(self, data) -> None: self.data: int = data self.next_node: Node | None = None @@ -72,7 +73,7 @@ def insert(self, data: int) -> None: Args: data (int): the data of linked list - + Doctests >>> linked_list = SortedLinkedList() >>> linked_list.insert_tail(32) @@ -101,8 +102,8 @@ def insert(self, data: int) -> None: def display(self) -> None: """ This function displays whole list - - + + Doctests >>> linkedList=SortedLinedList() >>> linkedList.insert(32) @@ -129,9 +130,9 @@ def delete(self, data: int) -> bool: Returns: bool: status whether the node got deleted or not - + Doctests - + >>> linkedList=SortedLinedList() >>> linkedList.insert(32) >>> linkedList.insert(57) @@ -172,7 +173,7 @@ def search(self, data: int) -> bool: Returns: bool: flag indicating whether data exists or not - + Doctests >>> linkedList=SortedLinedList() >>> linkedList.insert(32) @@ -195,12 +196,12 @@ def is_empty(self) -> bool: Returns: bool: flag indicating whether list is empty or not - + Doctests - + >>> linkedList=SortedLinedList() >>> linkedList.is_empty() - True + True >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) @@ -208,7 +209,7 @@ def is_empty(self) -> bool: >>> linkedList.is_empty() False """ - + return self.head is None def length(self) -> int: @@ -217,9 +218,9 @@ def length(self) -> int: Returns: int: The length of linked list - + Doctests - + >>> linkedList=SortedLinedList() >>> linkedList.length() 0 @@ -244,9 +245,9 @@ def min_value(self) -> int | None: Returns: int | None: min value or None if list is empty - + Doctests - + >>> linkedList=SortedLinedList() >>> linkedList.insert(32) >>> linkedList.insert(57) @@ -264,9 +265,9 @@ def max_value(self) -> int | None: Returns: int | None: max value or None if list is empty - + Doctests - + >>> linkedList=SortedLinedList() >>> linkedList.insert(32) >>> linkedList.insert(57) @@ -281,9 +282,9 @@ def max_value(self) -> int | None: def remove_duplicates(self) -> None: """ This Function will remove the duplicates from the list - + Doctests - + >>> linkedList=SortedLinedList() >>> linkedList.insert(32) >>> linkedList.insert(57) @@ -295,7 +296,7 @@ def remove_duplicates(self) -> None: >>> linkedList.display() 32 45 57 """ - + temp = self.head while temp and temp.next_node: if temp.data == temp.next_node.data: @@ -308,9 +309,9 @@ def merge(self, other_list: SortedLinkedList) -> None: Args: other_list (SortedLinkedList): The list to be merged - + Doctests - + >>> linkedList=SortedLinedList() >>> linkedList.insert(32) >>> linkedList.insert(57) From 3621bf39c9c76773d0c17714884056a3ab0d71d1 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 14:07:51 +0500 Subject: [PATCH 09/46] Data type of data given and docstring generated for Node class constructor --- data_structures/linked_list/sorted_linked_list.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index d3d305314b30..c99bd2eaf102 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -17,7 +17,12 @@ class Node: Node(None) """ - def __init__(self, data) -> None: + def __init__(self, data: int) -> None: + """COnstructor of Node class + + Args: + data (int): Data of node + """ self.data: int = data self.next_node: Node | None = None From 0795e15a6c5ba1656f4d4cf22799beb4e6f2efbd Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 14:10:05 +0500 Subject: [PATCH 10/46] "Updated docstrings and comments in SortedLinkedList class and Node dataclass." --- .../linked_list/sorted_linked_list.py | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index c99bd2eaf102..4c1da7ae15b6 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -2,26 +2,27 @@ from dataclasses import dataclass -# This is a sorted linked list class that creates a sorted linked list of integer datatype +# This is a sorted linked list class that creates +# a sorted linked list of integer datatype @dataclass class Node: - """ - Create and initialize Node class instance. - >>> Node(20) - Node(20) - >>> Node(27) - Node(27) - >>> Node(None) - Node(None) - """ def __init__(self, data: int) -> None: - """COnstructor of Node class + """Constructor of Node class Args: data (int): Data of node + + Doctests + + >>> Node(20) + Node(20) + >>> Node(27) + Node(27) + >>> Node(None) + Node(None) """ self.data: int = data self.next_node: Node | None = None @@ -340,7 +341,7 @@ def merge(self, other_list: SortedLinkedList) -> None: if __name__ == "__main__": - linked_list = SortedLinedList() + linked_list = SortedLinkedList() while True: print("Enter") print("1. Insert") From 2a10afe217e8a0fa9d8b5074cb130262457c7677 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 09:10:40 +0000 Subject: [PATCH 11/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 4c1da7ae15b6..02fc3807eb04 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -2,21 +2,20 @@ from dataclasses import dataclass -# This is a sorted linked list class that creates +# This is a sorted linked list class that creates # a sorted linked list of integer datatype @dataclass class Node: - def __init__(self, data: int) -> None: """Constructor of Node class Args: data (int): Data of node - + Doctests - + >>> Node(20) Node(20) >>> Node(27) From 807ceada2e9d3931bf04a1d4196bf86ffd175d61 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 14:12:15 +0500 Subject: [PATCH 12/46] "Removed type hints for None in Node and SortedLinkedList classes." --- data_structures/linked_list/sorted_linked_list.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 4c1da7ae15b6..b1632306c658 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -25,7 +25,7 @@ def __init__(self, data: int) -> None: Node(None) """ self.data: int = data - self.next_node: Node | None = None + self.next_node: Node = None def __repr__(self) -> str: """ @@ -51,8 +51,8 @@ def __init__(self) -> None: True """ self.numNodes: int = 0 - self.head: Node | None = None - self.tail: Node | None = None + self.head: Node = None + self.tail: Node = None def __repr__(self) -> str: """ From 5535759e99241c192b51574a78bd724f43aa8603 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 14:17:10 +0500 Subject: [PATCH 13/46] "Fixed typo in class name 'SortedLinedList' to 'SortedLinkedList' in doctests." --- .../linked_list/sorted_linked_list.py | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 0719a3dc0d86..9e386c54439f 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -55,7 +55,7 @@ def __init__(self) -> None: def __repr__(self) -> str: """ - >>> linkedList=SortedLinedList() + >>> linkedList=SortedLinkedList() >>> linkedList.insert(2) >>> linkedList.insert(12) >>> linkedList.insert(21) @@ -110,7 +110,7 @@ def display(self) -> None: Doctests - >>> linkedList=SortedLinedList() + >>> linkedList=SortedLinkedList() >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) @@ -138,7 +138,7 @@ def delete(self, data: int) -> bool: Doctests - >>> linkedList=SortedLinedList() + >>> linkedList=SortedLinkedList() >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) @@ -180,7 +180,7 @@ def search(self, data: int) -> bool: bool: flag indicating whether data exists or not Doctests - >>> linkedList=SortedLinedList() + >>> linkedList=SortedLinkedList() >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) @@ -204,7 +204,7 @@ def is_empty(self) -> bool: Doctests - >>> linkedList=SortedLinedList() + >>> linkedList=SortedLinkedList() >>> linkedList.is_empty() True >>> linkedList.insert(32) @@ -226,7 +226,7 @@ def length(self) -> int: Doctests - >>> linkedList=SortedLinedList() + >>> linkedList=SortedLinkedListtt() >>> linkedList.length() 0 >>> linkedList.insert(32) @@ -253,7 +253,7 @@ def min_value(self) -> int | None: Doctests - >>> linkedList=SortedLinedList() + >>> linkedList=SortedLinkedListt() >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) @@ -273,7 +273,7 @@ def max_value(self) -> int | None: Doctests - >>> linkedList=SortedLinedList() + >>> linkedList=SortedLinkedList() >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) @@ -290,7 +290,7 @@ def remove_duplicates(self) -> None: Doctests - >>> linkedList=SortedLinedList() + >>> linkedList=SortedLinkedList() >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) @@ -317,11 +317,11 @@ def merge(self, other_list: SortedLinkedList) -> None: Doctests - >>> linkedList=SortedLinedList() + >>> linkedList=SortedLinkedListt() >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) - >>> linkedList2=SortedLinedList() + >>> linkedList2=SortedLinkedList() >>> linkedList2.insert(23) >>> linkedList2.insert(47) >>> linkedList2.insert(95) From cb0c3a33b6f37a69e83b2f96ed35d066844c2e60 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 14:34:07 +0500 Subject: [PATCH 14/46] "Fixed typo in class name in docstrings" --- data_structures/linked_list/sorted_linked_list.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 9e386c54439f..fdda9997d614 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -45,7 +45,7 @@ class SortedLinkedList: def __init__(self) -> None: """ Create and initialize LinkedList class instance. - >>> linked_list = LinkedList() + >>> linked_list = SortedLinkedList() >>> linked_list.head is None True """ @@ -226,7 +226,7 @@ def length(self) -> int: Doctests - >>> linkedList=SortedLinkedListtt() + >>> linkedList=SortedLinkedList() >>> linkedList.length() 0 >>> linkedList.insert(32) From 13600242ca8957586682a0f663fa517a93452381 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 14:42:28 +0500 Subject: [PATCH 15/46] Updated doctests --- data_structures/linked_list/sorted_linked_list.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index fdda9997d614..328196731082 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -62,7 +62,7 @@ def __repr__(self) -> str: >>> linkedList.insert(23) >>> linkedList.insert(72) >>> linkedList.__repr__() - SortedLinkedList(2, 12, 21, 23, 72) + 'SortedLinkedList(2, 12, 21, 23, 72)' """ nodes = [] temp = self.head @@ -81,9 +81,9 @@ def insert(self, data: int) -> None: Doctests >>> linked_list = SortedLinkedList() - >>> linked_list.insert_tail(32) - >>> linked_list.insert_tail(57) - >>> linked_list.insert_tail(45) + >>> linked_list.insert(32) + >>> linked_list.insert(57) + >>> linked_list.insert(45) >>> linked_list SortedLinkedList(32, 45, 57) """ From d96cc64ce7b48a0fe20dca44f81b40629033e0a2 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 14:45:49 +0500 Subject: [PATCH 16/46] "Updated type hints for Node and SortedLinkedList attributes to include None option" --- data_structures/linked_list/sorted_linked_list.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 328196731082..8d1f43d091fc 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -24,7 +24,7 @@ def __init__(self, data: int) -> None: Node(None) """ self.data: int = data - self.next_node: Node = None + self.next_node: Node | None = None def __repr__(self) -> str: """ @@ -50,8 +50,8 @@ def __init__(self) -> None: True """ self.numNodes: int = 0 - self.head: Node = None - self.tail: Node = None + self.head: Node | None = None + self.tail: Node | None = None def __repr__(self) -> str: """ From fdbea27d4137ccb8b4a8f5f8d346ca92590f2086 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 15:22:35 +0500 Subject: [PATCH 17/46] updated merge function --- data_structures/linked_list/sorted_linked_list.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 8d1f43d091fc..8a133e5680fe 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -331,12 +331,15 @@ def merge(self, other_list: SortedLinkedList) -> None: """ if other_list.head is None: return - if self.head is None: + elif self.head is None: self.head = other_list.head self.tail = other_list.tail return - self.tail.next_node = other_list.head - self.tail = other_list.tail + else: + temp=otherother_list.head + while temp: + self.insert(temp.data) + temp=temp.next_node if __name__ == "__main__": From 57405add1103175c3303f7a06c422b7641341626 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:23:07 +0000 Subject: [PATCH 18/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 8a133e5680fe..8a87718133d0 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -336,10 +336,10 @@ def merge(self, other_list: SortedLinkedList) -> None: self.tail = other_list.tail return else: - temp=otherother_list.head + temp = otherother_list.head while temp: self.insert(temp.data) - temp=temp.next_node + temp = temp.next_node if __name__ == "__main__": From 8334ee60326ab5a9e9ef1e523bf4c865d9fe31b5 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 15:24:11 +0500 Subject: [PATCH 19/46] Fixed typo in merge function --- data_structures/linked_list/sorted_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 8a133e5680fe..4c0020fa81af 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -336,7 +336,7 @@ def merge(self, other_list: SortedLinkedList) -> None: self.tail = other_list.tail return else: - temp=otherother_list.head + temp=other_list.head while temp: self.insert(temp.data) temp=temp.next_node From 99baa932fcfc40c3dda7e94666448f7690489056 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:25:58 +0000 Subject: [PATCH 20/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 37ac2caa5e8f..507d06f028fb 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -336,7 +336,7 @@ def merge(self, other_list: SortedLinkedList) -> None: self.tail = other_list.tail return else: - temp=other_list.head + temp = other_list.head while temp: self.insert(temp.data) temp = temp.next_node From 2389245669234555351fba113dca856dcb16b7f4 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 15:41:31 +0500 Subject: [PATCH 21/46] "Added type hints for variables in SortedLinkedList class" --- data_structures/linked_list/sorted_linked_list.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 37ac2caa5e8f..866570874bf9 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -65,7 +65,7 @@ def __repr__(self) -> str: 'SortedLinkedList(2, 12, 21, 23, 72)' """ nodes = [] - temp = self.head + temp: Optional[Node] = self.head while temp: nodes.append(str(temp.data)) temp = temp.next_node @@ -95,7 +95,7 @@ def insert(self, data: int) -> None: new_node.next_node = self.head self.head = new_node else: - temp_node = self.head + temp_node: Optional[Node] = self.head while temp_node.next_node and temp_node.next_node.data < data: temp_node = temp_node.next_node new_node.next_node = temp_node.next_node @@ -117,7 +117,7 @@ def display(self) -> None: >>> linkedList.display() 32 45 57 """ - temp = self.head + temp: Optional[Node] = self.head while temp: print(temp.data, end=" ") temp = temp.next_node @@ -158,7 +158,7 @@ def delete(self, data: int) -> bool: self.tail = None return True - temp_node = self.head + temp_node: Optional[Node] = self.head while temp_node.next_node: if temp_node.next_node.data == data: temp_node.next_node = temp_node.next_node.next_node @@ -189,7 +189,7 @@ def search(self, data: int) -> bool: >>> linkedList.search(90) False """ - temp = self.head + temp: Optional[Node] = self.head while temp: if temp.data == data: return True @@ -302,7 +302,7 @@ def remove_duplicates(self) -> None: 32 45 57 """ - temp = self.head + temp: Optional[Node] = self.head while temp and temp.next_node: if temp.data == temp.next_node.data: temp.next_node = temp.next_node.next_node @@ -336,7 +336,7 @@ def merge(self, other_list: SortedLinkedList) -> None: self.tail = other_list.tail return else: - temp=other_list.head + temp: Optional[Node]=other_list.head while temp: self.insert(temp.data) temp = temp.next_node From 0847316786c4ee52eb5848c45566b20bc512f13f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:44:25 +0000 Subject: [PATCH 22/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 99e9a1f6f244..114a8ddd355e 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -336,7 +336,7 @@ def merge(self, other_list: SortedLinkedList) -> None: self.tail = other_list.tail return else: - temp : Optional[Node] = other_list.head + temp: Optional[Node] = other_list.head while temp: self.insert(temp.data) From 4cb8282bd120c07e5714512b367b17e1dd60ec83 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 15:45:29 +0500 Subject: [PATCH 23/46] "Added import statement for Optional type from typing module." --- data_structures/linked_list/sorted_linked_list.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 99e9a1f6f244..eb0f14894947 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -1,5 +1,6 @@ from __future__ import annotations +from typing import Optional from dataclasses import dataclass # This is a sorted linked list class that creates From 3ab0f7e585337a9e62a3c83e23c5a61cc1e202d7 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 15:49:07 +0500 Subject: [PATCH 24/46] Corrected doctests --- data_structures/linked_list/sorted_linked_list.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index b1a8304c30fb..71f25e3b27f6 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -116,7 +116,7 @@ def display(self) -> None: >>> linkedList.insert(57) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 57 + 32 45 57 """ temp: Optional[Node] = self.head while temp: @@ -144,11 +144,11 @@ def delete(self, data: int) -> bool: >>> linkedList.insert(57) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 57 + 32 45 57 >>> linkedList.delete(45) True >>> linkedList.display() - 32 57 + 32 57 """ if self.head is None: return False @@ -211,7 +211,6 @@ def is_empty(self) -> bool: >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) - >>> linkedList.search(45) >>> linkedList.is_empty() False """ @@ -297,10 +296,10 @@ def remove_duplicates(self) -> None: >>> linkedList.insert(45) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 45 57 + 32 45 45 57 >>> linkedList.remove_duplicates() >>> linkedList.display() - 32 45 57 + 32 45 57 """ temp: Optional[Node] = self.head @@ -328,7 +327,7 @@ def merge(self, other_list: SortedLinkedList) -> None: >>> linkedList2.insert(95) >>> linkedList.merge(linkedList2) >>> linkedList.display() - 23 32 45 47 57 95 + 23 32 45 47 57 95 """ if other_list.head is None: return From 5d98fd74dc9d554c563f947b422cadece7ca02ce Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:49:39 +0000 Subject: [PATCH 25/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 71f25e3b27f6..0df47e369f5d 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -116,7 +116,7 @@ def display(self) -> None: >>> linkedList.insert(57) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 57 + 32 45 57 """ temp: Optional[Node] = self.head while temp: @@ -144,11 +144,11 @@ def delete(self, data: int) -> bool: >>> linkedList.insert(57) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 57 + 32 45 57 >>> linkedList.delete(45) True >>> linkedList.display() - 32 57 + 32 57 """ if self.head is None: return False @@ -296,10 +296,10 @@ def remove_duplicates(self) -> None: >>> linkedList.insert(45) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 45 57 + 32 45 45 57 >>> linkedList.remove_duplicates() >>> linkedList.display() - 32 45 57 + 32 45 57 """ temp: Optional[Node] = self.head @@ -327,7 +327,7 @@ def merge(self, other_list: SortedLinkedList) -> None: >>> linkedList2.insert(95) >>> linkedList.merge(linkedList2) >>> linkedList.display() - 23 32 45 47 57 95 + 23 32 45 47 57 95 """ if other_list.head is None: return From 15026d88f89cac6ba97aba95d5a460c387930ebf Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 15:54:37 +0500 Subject: [PATCH 26/46] removed trailing whitespace and formatted import section --- data_structures/linked_list/sorted_linked_list.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 0df47e369f5d..645c7751bf88 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -1,8 +1,7 @@ from __future__ import annotations -from typing import Optional from dataclasses import dataclass - +from typing import Optional # This is a sorted linked list class that creates # a sorted linked list of integer datatype @@ -367,4 +366,4 @@ def merge(self, other_list: SortedLinkedList) -> None: elif choice == "4": break else: - print("Wrong input") + print("Wrong input") \ No newline at end of file From 6838bd89eaf7290b762b97ad1760444e7ae14018 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:55:22 +0000 Subject: [PATCH 27/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 645c7751bf88..e1f2161cd43f 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -366,4 +366,4 @@ def merge(self, other_list: SortedLinkedList) -> None: elif choice == "4": break else: - print("Wrong input") \ No newline at end of file + print("Wrong input") From fa29e39c2a1142668bcbbd03a8583bc29c0a5395 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 15:56:54 +0500 Subject: [PATCH 28/46] "Updated type hints for Optional[Node] to Node | None in SortedLinkedList class" --- data_structures/linked_list/sorted_linked_list.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 645c7751bf88..c3386a460d4c 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -65,7 +65,7 @@ def __repr__(self) -> str: 'SortedLinkedList(2, 12, 21, 23, 72)' """ nodes = [] - temp: Optional[Node] = self.head + temp: Node | None = self.head while temp: nodes.append(str(temp.data)) temp = temp.next_node @@ -95,7 +95,7 @@ def insert(self, data: int) -> None: new_node.next_node = self.head self.head = new_node else: - temp_node: Optional[Node] = self.head + temp_node: Node | None = self.head while temp_node.next_node and temp_node.next_node.data < data: temp_node = temp_node.next_node new_node.next_node = temp_node.next_node @@ -117,7 +117,7 @@ def display(self) -> None: >>> linkedList.display() 32 45 57 """ - temp: Optional[Node] = self.head + temp: Node | None = self.head while temp: print(temp.data, end=" ") temp = temp.next_node @@ -158,7 +158,7 @@ def delete(self, data: int) -> bool: self.tail = None return True - temp_node: Optional[Node] = self.head + temp_node: Node | None = self.head while temp_node.next_node: if temp_node.next_node.data == data: temp_node.next_node = temp_node.next_node.next_node @@ -189,7 +189,7 @@ def search(self, data: int) -> bool: >>> linkedList.search(90) False """ - temp: Optional[Node] = self.head + temp: Node | None = self.head while temp: if temp.data == data: return True @@ -301,7 +301,7 @@ def remove_duplicates(self) -> None: 32 45 57 """ - temp: Optional[Node] = self.head + temp: Node | None = self.head while temp and temp.next_node: if temp.data == temp.next_node.data: temp.next_node = temp.next_node.next_node @@ -335,7 +335,7 @@ def merge(self, other_list: SortedLinkedList) -> None: self.tail = other_list.tail return else: - temp: Optional[Node] = other_list.head + temp: Node | None = other_list.head while temp: self.insert(temp.data) From 5be1839fc7b5eef293233a686938db6fb4b8ae22 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 15:58:28 +0500 Subject: [PATCH 29/46] "Added two blank lines to sorted_linked_list.py" --- data_structures/linked_list/sorted_linked_list.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 6fe95faebb69..79e12ab16c8c 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -2,6 +2,8 @@ from dataclasses import dataclass from typing import Optional + + # This is a sorted linked list class that creates # a sorted linked list of integer datatype From 9e92e1e82570d3764738493133d4dc6e798124ac Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 15:58:49 +0500 Subject: [PATCH 30/46] removed empty line in the end --- data_structures/linked_list/sorted_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 79e12ab16c8c..ef0c4c97193f 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -368,4 +368,4 @@ def merge(self, other_list: SortedLinkedList) -> None: elif choice == "4": break else: - print("Wrong input") + print("Wrong input") \ No newline at end of file From 2208f7542401b6f6bdfb85509c8525eb1de304a5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:59:36 +0000 Subject: [PATCH 31/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index ef0c4c97193f..79e12ab16c8c 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -368,4 +368,4 @@ def merge(self, other_list: SortedLinkedList) -> None: elif choice == "4": break else: - print("Wrong input") \ No newline at end of file + print("Wrong input") From 26b7912d0ecea87570f17bb0c7e27b6a25c53bee Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 16:00:02 +0500 Subject: [PATCH 32/46] Remove unnecessary import and comments from sorted_linked_list.py --- data_structures/linked_list/sorted_linked_list.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index ef0c4c97193f..8cfd2eb23744 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -1,11 +1,6 @@ from __future__ import annotations from dataclasses import dataclass -from typing import Optional - - -# This is a sorted linked list class that creates -# a sorted linked list of integer datatype @dataclass From 44ebb6632b80459e13193c80541f0d1c9a84a5d0 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 16:07:48 +0500 Subject: [PATCH 33/46] Applied Non safety --- .../linked_list/sorted_linked_list.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 6f0630b14424..fa2a660ed48e 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -93,12 +93,13 @@ def insert(self, data: int) -> None: self.head = new_node else: temp_node: Node | None = self.head - while temp_node.next_node and temp_node.next_node.data < data: - temp_node = temp_node.next_node - new_node.next_node = temp_node.next_node - temp_node.next_node = new_node - if new_node.next_node is None: - self.tail = new_node + if temp: + while temp_node.next_node and temp_node.next_node.data < data: + temp_node = temp_node.next_node + new_node.next_node = temp_node.next_node + temp_node.next_node = new_node + if new_node.next_node is None: + self.tail = new_node self.numNodes += 1 def display(self) -> None: @@ -156,13 +157,14 @@ def delete(self, data: int) -> bool: return True temp_node: Node | None = self.head - while temp_node.next_node: - if temp_node.next_node.data == data: - temp_node.next_node = temp_node.next_node.next_node - if temp_node.next_node is None: - self.tail = temp_node - return True - temp_node = temp_node.next_node + if temp_node: + while temp_node.next_node: + if temp_node.next_node.data == data: + temp_node.next_node = temp_node.next_node.next_node + if temp_node.next_node is None: + self.tail = temp_node + return True + temp_node = temp_node.next_node return False @@ -363,4 +365,4 @@ def merge(self, other_list: SortedLinkedList) -> None: elif choice == "4": break else: - print("Wrong input") + print("Wrong input") \ No newline at end of file From 2f9dc7d5b78bcb68d906f3e5da54eb71dfb31f32 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 16:09:05 +0500 Subject: [PATCH 34/46] "Fixed variable name in conditional statement and removed newline character from print statement." --- data_structures/linked_list/sorted_linked_list.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index fa2a660ed48e..98d858e11cf5 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -93,7 +93,7 @@ def insert(self, data: int) -> None: self.head = new_node else: temp_node: Node | None = self.head - if temp: + if temp_node: while temp_node.next_node and temp_node.next_node.data < data: temp_node = temp_node.next_node new_node.next_node = temp_node.next_node @@ -365,4 +365,4 @@ def merge(self, other_list: SortedLinkedList) -> None: elif choice == "4": break else: - print("Wrong input") \ No newline at end of file + print("Wrong input" \ No newline at end of file From 2d61f2e5bbf41749e298cef8c1a389d377c75f36 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 16:11:09 +0500 Subject: [PATCH 35/46] Fixed missing closing parenthesis in print statement. --- data_structures/linked_list/sorted_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 98d858e11cf5..9b317acce8ba 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -365,4 +365,4 @@ def merge(self, other_list: SortedLinkedList) -> None: elif choice == "4": break else: - print("Wrong input" \ No newline at end of file + print("Wrong input") \ No newline at end of file From 13ae9ce1c0f4a1ae77d68d85be142d8e6f8286e0 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 16:12:00 +0500 Subject: [PATCH 36/46] added newline at end of file --- data_structures/linked_list/sorted_linked_list.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 9b317acce8ba..101fa35c803a 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -365,4 +365,5 @@ def merge(self, other_list: SortedLinkedList) -> None: elif choice == "4": break else: - print("Wrong input") \ No newline at end of file + print("Wrong input") + \ No newline at end of file From 7cb36dd99749aca107601e28a5e415d3a461ca3e Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 16:13:25 +0500 Subject: [PATCH 37/46] removed whitespace --- data_structures/linked_list/sorted_linked_list.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 101fa35c803a..c118696bccbf 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -366,4 +366,3 @@ def merge(self, other_list: SortedLinkedList) -> None: break else: print("Wrong input") - \ No newline at end of file From c2c9a9e357e67e6615bc018f6cd5bc7b6d9095f1 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 16:20:23 +0500 Subject: [PATCH 38/46] "Renamed `numNodes` to `num_nodes` and decremented num_nodes in delete function --- data_structures/linked_list/sorted_linked_list.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index c118696bccbf..a46172686107 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -46,7 +46,7 @@ def __init__(self) -> None: >>> linked_list.head is None True """ - self.numNodes: int = 0 + self.num_nodes: int = 0 self.head: Node | None = None self.tail: Node | None = None @@ -100,7 +100,7 @@ def insert(self, data: int) -> None: temp_node.next_node = new_node if new_node.next_node is None: self.tail = new_node - self.numNodes += 1 + self.num_nodes += 1 def display(self) -> None: """ @@ -165,7 +165,7 @@ def delete(self, data: int) -> bool: self.tail = temp_node return True temp_node = temp_node.next_node - + self.num_nodes-=1 return False def search(self, data: int) -> bool: @@ -241,7 +241,7 @@ def length(self) -> int: >>> linkedList.length() 2 """ - return self.numNodes + return self.num_nodes def min_value(self) -> int | None: """This function will return minimum value From 94d30b9ff3e22bb8f0092cedcba46f00475bfbf4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:20:55 +0000 Subject: [PATCH 39/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index a46172686107..128212912881 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -165,7 +165,7 @@ def delete(self, data: int) -> bool: self.tail = temp_node return True temp_node = temp_node.next_node - self.num_nodes-=1 + self.num_nodes -= 1 return False def search(self, data: int) -> bool: From 00086389ad404608cf219ccc69d80c6a74bbae79 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Tue, 1 Oct 2024 16:32:38 +0500 Subject: [PATCH 40/46] "Updated whitespace and trailing spaces in SortedLinkedList class" --- data_structures/linked_list/sorted_linked_list.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 128212912881..3a8ff9b0e8b8 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -113,7 +113,7 @@ def display(self) -> None: >>> linkedList.insert(57) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 57 + 32 45 57 """ temp: Node | None = self.head while temp: @@ -141,11 +141,11 @@ def delete(self, data: int) -> bool: >>> linkedList.insert(57) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 57 + 32 45 57 >>> linkedList.delete(45) True >>> linkedList.display() - 32 57 + 32 57 """ if self.head is None: return False @@ -163,9 +163,10 @@ def delete(self, data: int) -> bool: temp_node.next_node = temp_node.next_node.next_node if temp_node.next_node is None: self.tail = temp_node + self.num_nodes -= 1 return True temp_node = temp_node.next_node - self.num_nodes -= 1 + return False def search(self, data: int) -> bool: @@ -251,7 +252,7 @@ def min_value(self) -> int | None: Doctests - >>> linkedList=SortedLinkedListt() + >>> linkedList=SortedLinkedList() >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) @@ -294,10 +295,10 @@ def remove_duplicates(self) -> None: >>> linkedList.insert(45) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 45 57 + 32 45 45 57 >>> linkedList.remove_duplicates() >>> linkedList.display() - 32 45 57 + 32 45 57 """ temp: Node | None = self.head From b7fef5c16931c1252e17109b62ef9d1af018ac48 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:33:12 +0000 Subject: [PATCH 41/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 3a8ff9b0e8b8..da1f593f3533 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -113,7 +113,7 @@ def display(self) -> None: >>> linkedList.insert(57) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 57 + 32 45 57 """ temp: Node | None = self.head while temp: @@ -141,11 +141,11 @@ def delete(self, data: int) -> bool: >>> linkedList.insert(57) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 57 + 32 45 57 >>> linkedList.delete(45) True >>> linkedList.display() - 32 57 + 32 57 """ if self.head is None: return False @@ -166,7 +166,7 @@ def delete(self, data: int) -> bool: self.num_nodes -= 1 return True temp_node = temp_node.next_node - + return False def search(self, data: int) -> bool: @@ -295,10 +295,10 @@ def remove_duplicates(self) -> None: >>> linkedList.insert(45) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 45 57 + 32 45 45 57 >>> linkedList.remove_duplicates() >>> linkedList.display() - 32 45 57 + 32 45 57 """ temp: Node | None = self.head From a3becf31001b8bbb850d2d7d3c14d069119c46b3 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 06:39:41 +0500 Subject: [PATCH 42/46] Linted the code and doctests changed --- .../linked_list/sorted_linked_list.py | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index da1f593f3533..0fe774ceb1ae 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -5,6 +5,8 @@ @dataclass class Node: + """THis class represents a node in the linked list.""" + def __init__(self, data: int) -> None: """Constructor of Node class @@ -39,6 +41,8 @@ def __repr__(self) -> str: class SortedLinkedList: + """This class represents a sorted linked list.""" + def __init__(self) -> None: """ Create and initialize LinkedList class instance. @@ -59,14 +63,14 @@ def __repr__(self) -> str: >>> linkedList.insert(23) >>> linkedList.insert(72) >>> linkedList.__repr__() - 'SortedLinkedList(2, 12, 21, 23, 72)' + 2, 12, 21, 23, 72 """ nodes = [] temp: Node | None = self.head while temp: nodes.append(str(temp.data)) temp = temp.next_node - return f"SortedLinkedList({', '.join(nodes)})" + return f"{', '.join(nodes)}" def insert(self, data: int) -> None: """This Function inserts node in it's sorted position @@ -82,7 +86,7 @@ def insert(self, data: int) -> None: >>> linked_list.insert(57) >>> linked_list.insert(45) >>> linked_list - SortedLinkedList(32, 45, 57) + 32, 45, 57 """ new_node = Node(data) if self.head is None: @@ -106,20 +110,17 @@ def display(self) -> None: """ This function displays whole list - Doctests + + >>> linkedList=SortedLinkedList() >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 57 + 32, 45, 57 """ - temp: Node | None = self.head - while temp: - print(temp.data, end=" ") - temp = temp.next_node - print() + print(repr(self)) def delete(self, data: int) -> bool: """This Function deletes first appearance of node with @@ -141,11 +142,11 @@ def delete(self, data: int) -> bool: >>> linkedList.insert(57) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 57 + 32, 45, 57 >>> linkedList.delete(45) True >>> linkedList.display() - 32 57 + 32, 57 """ if self.head is None: return False @@ -295,10 +296,10 @@ def remove_duplicates(self) -> None: >>> linkedList.insert(45) >>> linkedList.insert(45) >>> linkedList.display() - 32 45 45 57 + 32, 45, 45, 57 >>> linkedList.remove_duplicates() >>> linkedList.display() - 32 45 57 + 32, 45, 57 """ temp: Node | None = self.head @@ -326,7 +327,7 @@ def merge(self, other_list: SortedLinkedList) -> None: >>> linkedList2.insert(95) >>> linkedList.merge(linkedList2) >>> linkedList.display() - 23 32 45 47 57 95 + 23, 32, 45, 47, 57, 95 """ if other_list.head is None: return @@ -353,16 +354,16 @@ def merge(self, other_list: SortedLinkedList) -> None: choice = input("Enter your choice: ") if choice == "1": - data = int(input("Enter a number: ")) - linked_list.insert(data) + nodeData = int(input("Enter a number: ")) + linked_list.insert(nodeData) elif choice == "2": linked_list.display() elif choice == "3": - data = int(input("Enter the data to delete: ")) - if linked_list.delete(data): - print(f"Node with data {data} deleted successfully") + nodeData = int(input("Enter the data to delete: ")) + if linked_list.delete(nodeData): + print(f"Node with data {nodeData} deleted successfully") else: - print(f"Node with data {data} not found in the list") + print(f"Node with data {nodeData} not found in the list") elif choice == "4": break else: From 7d414b489254c802a535237cf4949d0a476d929e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Oct 2024 01:40:16 +0000 Subject: [PATCH 43/46] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/linked_list/sorted_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 0fe774ceb1ae..66a2a4804ebd 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -111,7 +111,7 @@ def display(self) -> None: This function displays whole list Doctests - + >>> linkedList=SortedLinkedList() >>> linkedList.insert(32) From 041da89caba0e62e0cba865334d49a9f787c55e6 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 06:43:03 +0500 Subject: [PATCH 44/46] "Rename variable 'nodeData' to 'node_data' in main function" --- data_structures/linked_list/sorted_linked_list.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 0fe774ceb1ae..f9c3c77dbf5c 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -354,16 +354,16 @@ def merge(self, other_list: SortedLinkedList) -> None: choice = input("Enter your choice: ") if choice == "1": - nodeData = int(input("Enter a number: ")) - linked_list.insert(nodeData) + node_data = int(input("Enter a number: ")) + linked_list.insert(node_data) elif choice == "2": linked_list.display() elif choice == "3": - nodeData = int(input("Enter the data to delete: ")) - if linked_list.delete(nodeData): - print(f"Node with data {nodeData} deleted successfully") + node_data = int(input("Enter the data to delete: ")) + if linked_list.delete(node_data): + print(f"Node with data {node_data} deleted successfully") else: - print(f"Node with data {nodeData} not found in the list") + print(f"Node with data {node_data} not found in the list") elif choice == "4": break else: From 9a73b0a78e31945a144281af6d9ec00e17fb475f Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 06:47:20 +0500 Subject: [PATCH 45/46] "Fixed typo in class name and added quotes to __repr__ output" --- data_structures/linked_list/sorted_linked_list.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 3745163bc62a..fd0c34f9cb26 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -63,7 +63,7 @@ def __repr__(self) -> str: >>> linkedList.insert(23) >>> linkedList.insert(72) >>> linkedList.__repr__() - 2, 12, 21, 23, 72 + '2, 12, 21, 23, 72' """ nodes = [] temp: Node | None = self.head @@ -317,7 +317,7 @@ def merge(self, other_list: SortedLinkedList) -> None: Doctests - >>> linkedList=SortedLinkedListt() + >>> linkedList=SortedLinkedList() >>> linkedList.insert(32) >>> linkedList.insert(57) >>> linkedList.insert(45) From 96e8c77470fe179c5c868052cfc57d561759a0cc Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 9 Oct 2024 07:22:11 +0000 Subject: [PATCH 46/46] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..c9184b47ae2c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -272,6 +272,7 @@ * [Rotate To The Right](data_structures/linked_list/rotate_to_the_right.py) * [Singly Linked List](data_structures/linked_list/singly_linked_list.py) * [Skip List](data_structures/linked_list/skip_list.py) + * [Sorted Linked List](data_structures/linked_list/sorted_linked_list.py) * [Swap Nodes](data_structures/linked_list/swap_nodes.py) * Queue * [Circular Queue](data_structures/queue/circular_queue.py)