From 955a2d92608001695bcbc0c8f8939a957bb35dfa Mon Sep 17 00:00:00 2001 From: Vikas Kumar <54888022+vikasit12@users.noreply.github.com> Date: Mon, 25 Nov 2019 15:53:28 +0530 Subject: [PATCH 1/3] Update singly_linked_list.py printing current.data rather than node address in __repr__ for a more readable print statement --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 7137f4e66deb..0a39b173a8d2 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -76,7 +76,7 @@ def __repr__(self): # String representation/visualization of a Linked Lists current = self.head string_repr = "" while current: - string_repr += f"{current} ---> " + string_repr += f"{current.data} ---> " current = current.next # END represents end of the LinkedList string_repr += "END" From 59215dcc148b68cb3b4e0b5753918cd2c19ec6ed Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 26 Nov 2019 06:30:41 +0100 Subject: [PATCH 2/3] eval(repr(c)) == c The output of `__repr__()` _should look like a valid Python expression that could be used to recreate an object with the same value_. https://docs.python.org/3.4/reference/datamodel.html#object.__repr__ --- .../linked_list/singly_linked_list.py | 76 ++++++++----------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 0a39b173a8d2..e6c93ac411b6 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -3,30 +3,30 @@ def __init__(self, data): self.data = data # given data self.next = None # given next to None - def __repr__(self): # String Representation of a Node - return f"" + def __repr__(self): # string representation of a Node + return f"Node({self.data})" class LinkedList: def __init__(self): - self.head = None # Initialize head to None + self.head = None # initialize head to None - def insert_tail(self, data): + def insert_tail(self, data) -> None: if self.head is None: - self.insert_head(data) # If this is first node, call insert_head + self.insert_head(data) # if this is first node, call insert_head else: temp = self.head while temp.next: # traverse to last node temp = temp.next temp.next = Node(data) # create node & link to tail - def insert_head(self, data): + def insert_head(self, data) -> None: newNod = Node(data) # create a new node if self.head: newNod.next = self.head # link newNode to head self.head = newNod # make NewNode as head - def printList(self): # print every node data + def print_list(self) -> None: # print every node data temp = self.head while temp: print(temp.data) @@ -47,14 +47,12 @@ def delete_tail(self): # delete from tail else: while temp.next.next: # find the 2nd last element temp = temp.next - temp.next, temp = ( - None, - temp.next, - ) # (2nd last element).next = None and temp = last element + # (2nd last element).next = None and temp = last element + temp.next, temp = None, temp.next return temp - def isEmpty(self): - return self.head is None # Return if head is none + def is_empty(self) -> bool: + return self.head is None # return True if head is none def reverse(self): prev = None @@ -76,17 +74,16 @@ def __repr__(self): # String representation/visualization of a Linked Lists current = self.head string_repr = "" while current: - string_repr += f"{current.data} ---> " + string_repr += f"{current} --> " current = current.next # END represents end of the LinkedList - string_repr += "END" - return string_repr + return string_repr += "END" # Indexing Support. Used to get a node at particaular position def __getitem__(self, index): current = self.head - # If LinkedList is Empty + # If LinkedList is empty if current is None: raise IndexError("The Linked List is empty") @@ -113,39 +110,30 @@ def __setitem__(self, index, data): def main(): A = LinkedList() - print("Inserting 1st at head") - a1 = input() - A.insert_head(a1) - print("Inserting 2nd at head") - a2 = input() - A.insert_head(a2) - print("\nPrint List : ") - A.printList() - print("\nInserting 1st at Tail") - a3 = input() - A.insert_tail(a3) - print("Inserting 2nd at Tail") - a4 = input() - A.insert_tail(a4) - print("\nPrint List : ") - A.printList() + A.insert_head(input("Inserting 1st at head ").strip()) + A.insert_head(input("Inserting 2nd at head ").strip()) + print("\nPrint list:") + A.print_list() + A.insert_tail(input("\nInserting 1st at tail ").strip()) + A.insert_tail(input("Inserting 2nd at tail ").strip()) + print("\nPrint list:") + A.print_list() print("\nDelete head") A.delete_head() - print("Delete Tail") + print("Delete tail") A.delete_tail() - print("\nPrint List : ") - A.printList() - print("\nReverse Linked List") + print("\nPrint list:") + A.print_list() + print("\nReverse linked list") A.reverse() - print("\nPrint List : ") - A.printList() - print("\nString Representation of Linked List:") + print("\nPrint list:") + A.print_list() + print("\nString representation of linked list:") print(A) - print("\n Reading/Changing Node Data using Indexing:") + print("\nReading/changing Node data using indexing:") print(f"Element at Position 1: {A[1]}") - p1 = input("Enter New Value: ") - A[1] = p1 - print("New List:") + A[1] = input("Enter New Value: ").strip() + print("New list:") print(A) From dcbe65adaab19c6d5f765cbc0dda14854c77c5ca Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 26 Nov 2019 06:34:20 +0100 Subject: [PATCH 3/3] += --> + --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index e6c93ac411b6..8730a6d2a9e8 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -77,7 +77,7 @@ def __repr__(self): # String representation/visualization of a Linked Lists string_repr += f"{current} --> " current = current.next # END represents end of the LinkedList - return string_repr += "END" + return string_repr + "END" # Indexing Support. Used to get a node at particaular position def __getitem__(self, index):