From 1cd0e54478b99dd17d68461b900add135ccc1794 Mon Sep 17 00:00:00 2001 From: Lakshmikanth2001 <52835045+Lakshmikanth2001@users.noreply.github.com> Date: Wed, 15 Apr 2020 23:48:11 +0530 Subject: [PATCH 01/22] Update __init__.py please add a function to get length of linked list --- data_structures/linked_list/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 6d50f23c1f1a..f8889ef05e59 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -2,6 +2,7 @@ class Node: def __init__(self, item, next): self.item = item self.next = next + self.size=0 class LinkedList: def __init__(self): @@ -9,6 +10,7 @@ def __init__(self): def add(self, item): self.head = Node(item, self.head) + self.size+=1 def remove(self): if self.is_empty(): @@ -16,7 +18,10 @@ def remove(self): else: item = self.head.item self.head = self.head.next + self.size-=1 return item def is_empty(self): return self.head is None + def size(self): + return self.size From 21d73339aae74f481d9bac81bb1f7b8246f71b7f Mon Sep 17 00:00:00 2001 From: Lakshmikanth2001 <52835045+Lakshmikanth2001@users.noreply.github.com> Date: Thu, 16 Apr 2020 13:52:47 +0530 Subject: [PATCH 02/22] Update __init__.py --- 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 f8889ef05e59..5c7eb7c77500 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -2,11 +2,11 @@ class Node: def __init__(self, item, next): self.item = item self.next = next - self.size=0 class LinkedList: def __init__(self): self.head = None + self.size=0 def add(self, item): self.head = Node(item, self.head) From dc0c69407564e307c714b8d207c669b64d7ad745 Mon Sep 17 00:00:00 2001 From: Lakshmikanth2001 <52835045+Lakshmikanth2001@users.noreply.github.com> Date: Thu, 16 Apr 2020 13:59:19 +0530 Subject: [PATCH 03/22] Update doubly_linked_list.py all size function lo doubly linked list class --- data_structures/linked_list/doubly_linked_list.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 75b1f889dfc2..3c2bd68d55dc 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -11,6 +11,7 @@ class LinkedList: #making main class named linked list def __init__(self): self.head = None self.tail = None + self.size=0 def insertHead(self, x): newLink = Link(x) #Create a new link with a value attached to it @@ -20,6 +21,7 @@ def insertHead(self, x): self.head.previous = newLink # newLink <-- currenthead(head) newLink.next = self.head # newLink <--> currenthead(head) self.head = newLink # newLink(head) <--> oldhead + self.size+=1 def deleteHead(self): temp = self.head @@ -27,6 +29,7 @@ def deleteHead(self): self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed if(self.head is None): self.tail = None #if empty linked list + self.size-=1 return temp def insertTail(self, x): @@ -35,11 +38,13 @@ def insertTail(self, x): self.tail.next = newLink # currentTail(tail) --> newLink --> newLink.previous = self.tail #currentTail(tail) <--> newLink --> self.tail = newLink # oldTail <--> newLink(tail) --> + self.size+=1 def deleteTail(self): temp = self.tail self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None self.tail.next = None # 2ndlast(tail) --> None + self.size-=1 return temp def delete(self, x): @@ -57,7 +62,7 @@ def delete(self, x): else: #Before: 1 <--> 2(current) <--> 3 current.previous.next = current.next # 1 --> 3 current.next.previous = current.previous # 1 <--> 3 - + self.size-=1 def isEmpty(self): #Will return True if the list is empty return(self.head is None) @@ -67,7 +72,8 @@ def display(self): #Prints contents of the list current.displayLink() current = current.next print() - + def size(self): + return self.size class Link: next = None #This points to the link in front of the new link previous = None #This points to the link behind the new link From 6a19daf9a426688e5c12288ea8bd69c647c1a1d3 Mon Sep 17 00:00:00 2001 From: Lakshmikanth2001 <52835045+Lakshmikanth2001@users.noreply.github.com> Date: Thu, 28 May 2020 23:09:58 +0530 Subject: [PATCH 04/22] prime number _better method --- maths/prime_check.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/prime_check.py b/maths/prime_check.py index 9249834dc069..3d822b6879ba 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -22,8 +22,8 @@ def prime_check(number): # Except 2, all primes are odd. If any odd value divide # the number, then that number is not prime. - odd_numbers = range(3, int(math.sqrt(number)) + 1, 2) - return not any(number % i == 0 for i in odd_numbers) + special_number=range(5,int(math.sqrt(number)),6) + return not any((number % i == 0 or number%(i+2)==0) for i in special_number) class Test(unittest.TestCase): From bcafc1120f62be11a11013f90018aa4fadc5a00d Mon Sep 17 00:00:00 2001 From: Lakshmikanth2001 <52835045+Lakshmikanth2001@users.noreply.github.com> Date: Thu, 28 May 2020 23:11:54 +0530 Subject: [PATCH 05/22] comments --- maths/prime_check.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/prime_check.py b/maths/prime_check.py index 3d822b6879ba..aa70b3359125 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -22,6 +22,7 @@ def prime_check(number): # Except 2, all primes are odd. If any odd value divide # the number, then that number is not prime. + # prime number all are in 6k+1 or 6k-1 form where k belongs to integers so i have modified range(5,root(n),6) special_number=range(5,int(math.sqrt(number)),6) return not any((number % i == 0 or number%(i+2)==0) for i in special_number) From f110e490d0a79119a9064ec622833b8766cad202 Mon Sep 17 00:00:00 2001 From: Lakshmikanth2001 <52835045+Lakshmikanth2001@users.noreply.github.com> Date: Sat, 30 May 2020 14:29:15 +0530 Subject: [PATCH 06/22] Updated init.py 2 made it more pythonic --- data_structures/linked_list/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 5c7eb7c77500..9dd94ac2dd9f 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -2,7 +2,6 @@ class Node: def __init__(self, item, next): self.item = item self.next = next - class LinkedList: def __init__(self): self.head = None @@ -20,8 +19,7 @@ def remove(self): self.head = self.head.next self.size-=1 return item - def is_empty(self): return self.head is None - def size(self): + def __len__(self): return self.size From bc75424234cfb6279d8358bb5098991bdb76074e Mon Sep 17 00:00:00 2001 From: Lakshmikanth2001 <52835045+Lakshmikanth2001@users.noreply.github.com> Date: Sat, 30 May 2020 14:34:35 +0530 Subject: [PATCH 07/22] updated length function --- data_structures/linked_list/doubly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 3c2bd68d55dc..ccca39cbcba5 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -72,7 +72,7 @@ def display(self): #Prints contents of the list current.displayLink() current = current.next print() - def size(self): + def __len__(self): return self.size class Link: next = None #This points to the link in front of the new link From ffb58d2a373666ea62f09fcd4897ea78b30b7297 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 09:30:03 +0200 Subject: [PATCH 08/22] Add doctests to LinkedList().__len__() --- data_structures/linked_list/__init__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 9dd94ac2dd9f..25a80c0bc04c 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -2,6 +2,8 @@ class Node: def __init__(self, item, next): self.item = item self.next = next + + class LinkedList: def __init__(self): self.head = None @@ -19,7 +21,26 @@ def remove(self): self.head = self.head.next self.size-=1 return item + def is_empty(self): return self.head is None + def __len__(self): + """ + >>> linked_list = LinkedList() + >>> len(linked_list) + 0 + >>> linked_list.add("a") + >>> len(linked_list) + 1 + >>> linked_list.add("b") + >>> len(linked_list) + 2 + >>> _ = linked_list.remove() + >>> len(linked_list) + 1 + >>> _ = linked_list.remove() + >>> len(linked_list) + 0 + """ return self.size From df7a07c60f7917fd1ec7837690bd03a207105763 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 09:40:13 +0200 Subject: [PATCH 09/22] Add doctests to LinkedList().__len__() --- .../linked_list/doubly_linked_list.py | 60 +++++++++++++------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index ccca39cbcba5..eb2dd6acd526 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -3,19 +3,29 @@ - This is an example of a double ended, doubly linked list. - Each link references the next link and the previous one. - A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list. - - Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent''' -from __future__ import print_function +- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent +''' + +class Link: + next = None #This points to the link in front of the new link + previous = None #This points to the link behind the new link + + def __init__(self, x): + self.value = x + + def displayLink(self): + print(self.value, end=" ") -class LinkedList: #making main class named linked list +class LinkedList: def __init__(self): self.head = None self.tail = None self.size=0 - def insertHead(self, x): - newLink = Link(x) #Create a new link with a value attached to it - if(self.isEmpty() == True): #Set the first element added to be the tail + def insert_head(self, x): + newLink = Link(x) # Create a new link with a value attached to it + if self.isEmpty(): # Set the first element added to be the tail self.tail = newLink else: self.head.previous = newLink # newLink <-- currenthead(head) @@ -23,7 +33,7 @@ def insertHead(self, x): self.head = newLink # newLink(head) <--> oldhead self.size+=1 - def deleteHead(self): + def delete_head(self): temp = self.head self.head = self.head.next # oldHead <--> 2ndElement(head) self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed @@ -32,7 +42,7 @@ def deleteHead(self): self.size-=1 return temp - def insertTail(self, x): + def insert_tail(self, x): newLink = Link(x) newLink.next = None # currentTail(tail) newLink --> self.tail.next = newLink # currentTail(tail) --> newLink --> @@ -40,7 +50,7 @@ def insertTail(self, x): self.tail = newLink # oldTail <--> newLink(tail) --> self.size+=1 - def deleteTail(self): + def delete_tail(self): temp = self.tail self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None self.tail.next = None # 2ndlast(tail) --> None @@ -63,21 +73,33 @@ def delete(self, x): current.previous.next = current.next # 1 --> 3 current.next.previous = current.previous # 1 <--> 3 self.size-=1 - def isEmpty(self): #Will return True if the list is empty - return(self.head is None) + + def is_empty(self): #Will return True if the list is empty + return self.head is None def display(self): #Prints contents of the list current = self.head - while(current != None): + while current != None: current.displayLink() current = current.next print() + def __len__(self): + """ + >>> linked_list = LinkedList() + >>> len(linked_list) + 0 + >>> linked_list.insert_head("a") + >>> len(linked_list) + 1 + >>> linked_list.insert_tail("b") + >>> len(linked_list) + 2 + >>> _ = linked_list.delete_tail() + >>> len(linked_list) + 1 + >>> _ = linked_list.delete_head() + >>> len(linked_list) + 0 + """ return self.size -class Link: - next = None #This points to the link in front of the new link - previous = None #This points to the link behind the new link - def __init__(self, x): - self.value = x - def displayLink(self): - print("{}".format(self.value), end=" ") From da43761569a632af3e660cab435ff6d3319129ee Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 09:41:36 +0200 Subject: [PATCH 10/22] Revert changes to prime_check.py --- maths/prime_check.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/maths/prime_check.py b/maths/prime_check.py index aa70b3359125..642ce64e1197 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -22,10 +22,8 @@ def prime_check(number): # Except 2, all primes are odd. If any odd value divide # the number, then that number is not prime. - # prime number all are in 6k+1 or 6k-1 form where k belongs to integers so i have modified range(5,root(n),6) - special_number=range(5,int(math.sqrt(number)),6) - return not any((number % i == 0 or number%(i+2)==0) for i in special_number) - + odd_numbers = range(3, int(math.sqrt(number)) + 1, 2) + return not any(number % i == 0 for i in odd_numbers) class Test(unittest.TestCase): def test_primes(self): From 6d549a30396108844855d692b8626392f417f6a5 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 09:42:01 +0200 Subject: [PATCH 11/22] Revert changes to prime_check.py --- maths/prime_check.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/prime_check.py b/maths/prime_check.py index 642ce64e1197..9249834dc069 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -25,6 +25,7 @@ def prime_check(number): odd_numbers = range(3, int(math.sqrt(number)) + 1, 2) return not any(number % i == 0 for i in odd_numbers) + class Test(unittest.TestCase): def test_primes(self): self.assertTrue(prime_check(2)) From 911dde99aa64a9b0042e4e048c92a804f4dd7c21 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 10:00:17 +0200 Subject: [PATCH 12/22] Update doubly_linked_list.py --- .../linked_list/doubly_linked_list.py | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index eb2dd6acd526..713c15d0788b 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -1,14 +1,18 @@ -''' -- A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes. +""" +- A linked list is similar to an array, it holds values. However, links in a linked + list do not have indexes. - This is an example of a double ended, doubly linked list. - Each link references the next link and the previous one. -- A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list. -- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent -''' +- A Doubly Linked List (DLL) contains an extra pointer, typically called previous + pointer, together with next pointer and data which are there in singly linked list. +- Advantages over SLL - IT can be traversed in both forward and backward direction. + Delete operation is more efficient. +""" + class Link: - next = None #This points to the link in front of the new link - previous = None #This points to the link behind the new link + next = None. # This points to the link in front of the new link + previous = None # This points to the link behind the new link def __init__(self, x): self.value = x @@ -24,60 +28,58 @@ def __init__(self): self.size=0 def insert_head(self, x): - newLink = Link(x) # Create a new link with a value attached to it - if self.isEmpty(): # Set the first element added to be the tail + newLink = Link(x) # Create a new link with a value attached to it + if self.isEmpty(): # Set the first element added to be the tail self.tail = newLink else: - self.head.previous = newLink # newLink <-- currenthead(head) - newLink.next = self.head # newLink <--> currenthead(head) - self.head = newLink # newLink(head) <--> oldhead + self.head.previous = newLink. # newLink <-- currenthead(head) + newLink.next = self.head. # newLink <--> currenthead(head) + self.head = newLink. # newLink(head) <--> oldhead self.size+=1 def delete_head(self): temp = self.head - self.head = self.head.next # oldHead <--> 2ndElement(head) - self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed + self.head = self.head.next # oldHead <--> 2ndElement(head) + self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed if(self.head is None): - self.tail = None #if empty linked list + self.tail = None # if empty linked list self.size-=1 return temp def insert_tail(self, x): newLink = Link(x) - newLink.next = None # currentTail(tail) newLink --> - self.tail.next = newLink # currentTail(tail) --> newLink --> - newLink.previous = self.tail #currentTail(tail) <--> newLink --> - self.tail = newLink # oldTail <--> newLink(tail) --> + newLink.next = None # currentTail(tail) newLink --> + self.tail.next = newLink # currentTail(tail) --> newLink --> + newLink.previous = self.tail # currentTail(tail) <--> newLink --> + self.tail = newLink # oldTail <--> newLink(tail) --> self.size+=1 def delete_tail(self): temp = self.tail - self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None - self.tail.next = None # 2ndlast(tail) --> None + self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None + self.tail.next = None # 2ndlast(tail) --> None self.size-=1 return temp def delete(self, x): current = self.head - while(current.value != x): # Find the position to delete + while current.value != x: # Find the position to delete current = current.next - if(current == self.head): + if current == self.head: self.deleteHead() - - elif(current == self.tail): + elif current == self.tail: self.deleteTail() - - else: #Before: 1 <--> 2(current) <--> 3 - current.previous.next = current.next # 1 --> 3 - current.next.previous = current.previous # 1 <--> 3 - self.size-=1 + else: # Before: 1 <--> 2(current) <--> 3 + current.previous.next = current.next # 1 --> 3 + current.next.previous = current.previous # 1 <--> 3 + self.size -= 1 - def is_empty(self): #Will return True if the list is empty + def is_empty(self): # Will return True if the list is empty return self.head is None - def display(self): #Prints contents of the list + def display(self): # Prints contents of the list current = self.head while current != None: current.displayLink() From 639d77bca3a929ffd8ca2f107ceb206078c73dc3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 10:01:04 +0200 Subject: [PATCH 13/22] Update doubly_linked_list.py --- data_structures/linked_list/doubly_linked_list.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 713c15d0788b..c95b276b8298 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -1,12 +1,12 @@ """ - A linked list is similar to an array, it holds values. However, links in a linked - list do not have indexes. + list do not have indexes. - This is an example of a double ended, doubly linked list. - Each link references the next link and the previous one. - A Doubly Linked List (DLL) contains an extra pointer, typically called previous - pointer, together with next pointer and data which are there in singly linked list. + pointer, together with next pointer and data which are there in singly linked list. - Advantages over SLL - IT can be traversed in both forward and backward direction. - Delete operation is more efficient. + Delete operation is more efficient. """ From 0587999dbb77588d1a7b89c0e2d7dd751c484849 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 10:02:12 +0200 Subject: [PATCH 14/22] Update doubly_linked_list.py --- data_structures/linked_list/doubly_linked_list.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index c95b276b8298..8df4ca7b64b9 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -32,9 +32,9 @@ def insert_head(self, x): if self.isEmpty(): # Set the first element added to be the tail self.tail = newLink else: - self.head.previous = newLink. # newLink <-- currenthead(head) - newLink.next = self.head. # newLink <--> currenthead(head) - self.head = newLink. # newLink(head) <--> oldhead + self.head.previous = newLink # newLink <-- currenthead(head) + newLink.next = self.head # newLink <--> currenthead(head) + self.head = newLink # newLink(head) <--> oldhead self.size+=1 def delete_head(self): From 81b3a685f6e08d0e395579f8824e6b29cd2a1c83 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 10:02:43 +0200 Subject: [PATCH 15/22] Update doubly_linked_list.py --- data_structures/linked_list/doubly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 8df4ca7b64b9..7acc874f3f09 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -45,7 +45,7 @@ def delete_head(self): self.tail = None # if empty linked list self.size-=1 return temp - + def insert_tail(self, x): newLink = Link(x) newLink.next = None # currentTail(tail) newLink --> From dbb57e9895649628c00215990ccf7963e3eb9df4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 10:04:59 +0200 Subject: [PATCH 16/22] Update doubly_linked_list.py --- data_structures/linked_list/doubly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 7acc874f3f09..fc4daefe9361 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -41,7 +41,7 @@ def delete_head(self): temp = self.head self.head = self.head.next # oldHead <--> 2ndElement(head) self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed - if(self.head is None): + if not self.head: self.tail = None # if empty linked list self.size-=1 return temp From 1d6b37ea7672135f3bec6e0c999c22c60a79ec43 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 10:08:05 +0200 Subject: [PATCH 17/22] Update doubly_linked_list.py --- data_structures/linked_list/doubly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index fc4daefe9361..e2fd1b3a96b5 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -11,7 +11,7 @@ class Link: - next = None. # This points to the link in front of the new link + next = None # This points to the link in front of the new link previous = None # This points to the link behind the new link def __init__(self, x): From b1967c55111f61d00615de261579d069d6e9da5f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 31 May 2020 08:08:45 +0000 Subject: [PATCH 18/22] fixup! Format Python code with psf/black push --- data_structures/linked_list/__init__.py | 8 ++--- .../linked_list/doubly_linked_list.py | 30 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 25a80c0bc04c..3ddfea5c5abf 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -7,11 +7,11 @@ def __init__(self, item, next): class LinkedList: def __init__(self): self.head = None - self.size=0 + self.size = 0 def add(self, item): self.head = Node(item, self.head) - self.size+=1 + self.size += 1 def remove(self): if self.is_empty(): @@ -19,9 +19,9 @@ def remove(self): else: item = self.head.item self.head = self.head.next - self.size-=1 + self.size -= 1 return item - + def is_empty(self): return self.head is None diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index e2fd1b3a96b5..37a3085551c9 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -11,7 +11,7 @@ class Link: - next = None # This points to the link in front of the new link + next = None # This points to the link in front of the new link previous = None # This points to the link behind the new link def __init__(self, x): @@ -25,8 +25,8 @@ class LinkedList: def __init__(self): self.head = None self.tail = None - self.size=0 - + self.size = 0 + def insert_head(self, x): newLink = Link(x) # Create a new link with a value attached to it if self.isEmpty(): # Set the first element added to be the tail @@ -35,15 +35,15 @@ def insert_head(self, x): self.head.previous = newLink # newLink <-- currenthead(head) newLink.next = self.head # newLink <--> currenthead(head) self.head = newLink # newLink(head) <--> oldhead - self.size+=1 - + self.size += 1 + def delete_head(self): temp = self.head - self.head = self.head.next # oldHead <--> 2ndElement(head) + self.head = self.head.next # oldHead <--> 2ndElement(head) self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed if not self.head: self.tail = None # if empty linked list - self.size-=1 + self.size -= 1 return temp def insert_tail(self, x): @@ -52,21 +52,21 @@ def insert_tail(self, x): self.tail.next = newLink # currentTail(tail) --> newLink --> newLink.previous = self.tail # currentTail(tail) <--> newLink --> self.tail = newLink # oldTail <--> newLink(tail) --> - self.size+=1 - + self.size += 1 + def delete_tail(self): temp = self.tail self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None self.tail.next = None # 2ndlast(tail) --> None - self.size-=1 + self.size -= 1 return temp - + def delete(self, x): current = self.head - + while current.value != x: # Find the position to delete current = current.next - + if current == self.head: self.deleteHead() elif current == self.tail: @@ -78,12 +78,12 @@ def delete(self, x): def is_empty(self): # Will return True if the list is empty return self.head is None - + def display(self): # Prints contents of the list current = self.head while current != None: current.displayLink() - current = current.next + current = current.next print() def __len__(self): From 8285c08afdbdf1678ee0ee6129bf823c6353ed3a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 10:09:00 +0200 Subject: [PATCH 19/22] Update doubly_linked_list.py --- .../linked_list/doubly_linked_list.py | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 37a3085551c9..c78dd411e1f1 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -11,7 +11,7 @@ class Link: - next = None # This points to the link in front of the new link + next = None # This points to the link in front of the new link previous = None # This points to the link behind the new link def __init__(self, x): @@ -25,8 +25,8 @@ class LinkedList: def __init__(self): self.head = None self.tail = None - self.size = 0 - + self.size=0 + def insert_head(self, x): newLink = Link(x) # Create a new link with a value attached to it if self.isEmpty(): # Set the first element added to be the tail @@ -35,15 +35,15 @@ def insert_head(self, x): self.head.previous = newLink # newLink <-- currenthead(head) newLink.next = self.head # newLink <--> currenthead(head) self.head = newLink # newLink(head) <--> oldhead - self.size += 1 - + self.size+=1 + def delete_head(self): temp = self.head - self.head = self.head.next # oldHead <--> 2ndElement(head) + self.head = self.head.next # oldHead <--> 2ndElement(head) self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed if not self.head: self.tail = None # if empty linked list - self.size -= 1 + self.size-=1 return temp def insert_tail(self, x): @@ -52,21 +52,21 @@ def insert_tail(self, x): self.tail.next = newLink # currentTail(tail) --> newLink --> newLink.previous = self.tail # currentTail(tail) <--> newLink --> self.tail = newLink # oldTail <--> newLink(tail) --> - self.size += 1 - + self.size+=1 + def delete_tail(self): temp = self.tail self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None self.tail.next = None # 2ndlast(tail) --> None - self.size -= 1 + self.size-=1 return temp - + def delete(self, x): current = self.head - + while current.value != x: # Find the position to delete current = current.next - + if current == self.head: self.deleteHead() elif current == self.tail: @@ -78,12 +78,12 @@ def delete(self, x): def is_empty(self): # Will return True if the list is empty return self.head is None - + def display(self): # Prints contents of the list current = self.head - while current != None: + while current: current.displayLink() - current = current.next + current = current.next print() def __len__(self): From dc86f40474376eddc185e5c51ce8a696965ca5b6 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 31 May 2020 08:09:34 +0000 Subject: [PATCH 20/22] fixup! Format Python code with psf/black push --- .../linked_list/doubly_linked_list.py | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index c78dd411e1f1..d00c14343905 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -11,7 +11,7 @@ class Link: - next = None # This points to the link in front of the new link + next = None # This points to the link in front of the new link previous = None # This points to the link behind the new link def __init__(self, x): @@ -25,8 +25,8 @@ class LinkedList: def __init__(self): self.head = None self.tail = None - self.size=0 - + self.size = 0 + def insert_head(self, x): newLink = Link(x) # Create a new link with a value attached to it if self.isEmpty(): # Set the first element added to be the tail @@ -35,15 +35,15 @@ def insert_head(self, x): self.head.previous = newLink # newLink <-- currenthead(head) newLink.next = self.head # newLink <--> currenthead(head) self.head = newLink # newLink(head) <--> oldhead - self.size+=1 - + self.size += 1 + def delete_head(self): temp = self.head - self.head = self.head.next # oldHead <--> 2ndElement(head) + self.head = self.head.next # oldHead <--> 2ndElement(head) self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed if not self.head: self.tail = None # if empty linked list - self.size-=1 + self.size -= 1 return temp def insert_tail(self, x): @@ -52,21 +52,21 @@ def insert_tail(self, x): self.tail.next = newLink # currentTail(tail) --> newLink --> newLink.previous = self.tail # currentTail(tail) <--> newLink --> self.tail = newLink # oldTail <--> newLink(tail) --> - self.size+=1 - + self.size += 1 + def delete_tail(self): temp = self.tail self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None self.tail.next = None # 2ndlast(tail) --> None - self.size-=1 + self.size -= 1 return temp - + def delete(self, x): current = self.head - + while current.value != x: # Find the position to delete current = current.next - + if current == self.head: self.deleteHead() elif current == self.tail: @@ -78,12 +78,12 @@ def delete(self, x): def is_empty(self): # Will return True if the list is empty return self.head is None - + def display(self): # Prints contents of the list current = self.head while current: current.displayLink() - current = current.next + current = current.next print() def __len__(self): From 54e661ba64b48b91a1edf3b48b91e8b085d330ea Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 10:23:36 +0200 Subject: [PATCH 21/22] self.is_empty() --- data_structures/linked_list/doubly_linked_list.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index d00c14343905..05cd5fe6a3c5 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -28,13 +28,13 @@ def __init__(self): self.size = 0 def insert_head(self, x): - newLink = Link(x) # Create a new link with a value attached to it - if self.isEmpty(): # Set the first element added to be the tail - self.tail = newLink + new_link = Link(x) # Create a new link with a value attached to it + if self.is_empty(): # Set the first element added to be the tail + self.tail = new_link else: - self.head.previous = newLink # newLink <-- currenthead(head) - newLink.next = self.head # newLink <--> currenthead(head) - self.head = newLink # newLink(head) <--> oldhead + self.head.previous = new_link # new_link <-- currenthead(head) + new_link.next = self.head # new_link <--> currenthead(head) + self.head = new_link # new_link(head) <--> oldhead self.size += 1 def delete_head(self): From 93b06526722c1db13fbb50a26dd0ee28502f5613 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 1 Jun 2020 11:08:05 +0200 Subject: [PATCH 22/22] Revert all changes to __init__.py --- data_structures/linked_list/__init__.py | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 3ddfea5c5abf..a050adba42b2 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -7,11 +7,9 @@ def __init__(self, item, next): class LinkedList: def __init__(self): self.head = None - self.size = 0 def add(self, item): self.head = Node(item, self.head) - self.size += 1 def remove(self): if self.is_empty(): @@ -19,28 +17,7 @@ def remove(self): else: item = self.head.item self.head = self.head.next - self.size -= 1 return item def is_empty(self): return self.head is None - - def __len__(self): - """ - >>> linked_list = LinkedList() - >>> len(linked_list) - 0 - >>> linked_list.add("a") - >>> len(linked_list) - 1 - >>> linked_list.add("b") - >>> len(linked_list) - 2 - >>> _ = linked_list.remove() - >>> len(linked_list) - 1 - >>> _ = linked_list.remove() - >>> len(linked_list) - 0 - """ - return self.size