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/13] 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/13] 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/13] 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/13] 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/13] 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/13] 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/13] 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 9db0c037cede17550b02ff2c45a6fad3b6f583fe Mon Sep 17 00:00:00 2001 From: Lakshmikanth2001 <52835045+Lakshmikanth2001@users.noreply.github.com> Date: Sat, 30 May 2020 15:03:23 +0530 Subject: [PATCH 08/13] commnet in linked_list construtor --- 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 9dd94ac2dd9f..f1cfcea5ac69 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -21,5 +21,5 @@ def remove(self): return item def is_empty(self): return self.head is None - def __len__(self): + def __len__(self): #add python predefined len function return self.size From 181ff13248770b130f348572d087bd580b6c318b Mon Sep 17 00:00:00 2001 From: Lakshmikanth2001 <52835045+Lakshmikanth2001@users.noreply.github.com> Date: Sat, 30 May 2020 17:37:03 +0530 Subject: [PATCH 09/13] Update data_structures/linked_list/__init__.py accepecting changes Co-authored-by: Christian Clauss --- 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 f1cfcea5ac69..9dd94ac2dd9f 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -21,5 +21,5 @@ def remove(self): return item def is_empty(self): return self.head is None - def __len__(self): #add python predefined len function + def __len__(self): return self.size From f6830645874104623779ffc5a906e186acf931fd Mon Sep 17 00:00:00 2001 From: Lakshmikanth2001 <52835045+Lakshmikanth2001@users.noreply.github.com> Date: Sat, 30 May 2020 18:56:38 +0530 Subject: [PATCH 10/13] Update data_structures/linked_list/__init__.py Co-authored-by: Christian Clauss --- 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 9dd94ac2dd9f..a93ec6612c6e 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -5,7 +5,7 @@ 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) From a08d5c7d8a077fee4bbad4e771343e8183881cdc Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 11:15:40 +0200 Subject: [PATCH 11/13] Update __init__.py --- data_structures/linked_list/__init__.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index a93ec6612c6e..3ddfea5c5abf 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 @@ -9,7 +11,7 @@ def __init__(self): def add(self, item): self.head = Node(item, self.head) - self.size+=1 + self.size += 1 def remove(self): if self.is_empty(): @@ -17,9 +19,28 @@ 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 + 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 3d253550329842b6fb2b65f4dfcd499a54b93319 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 11:16:56 +0200 Subject: [PATCH 12/13] Revert changes to doubly_linked_list.py --- .../linked_list/doubly_linked_list.py | 107 +++++++++--------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index ccca39cbcba5..e449ed6ec8ac 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -1,83 +1,84 @@ -''' -- 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''' -from __future__ import print_function +- 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 LinkedList: #making main class named linked list +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 - if(self.isEmpty() == True): #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.size+=1 - + self.head.previous = newLink # newLink <-- currenthead(head) + newLink.next = self.head # newLink <--> currenthead(head) + self.head = newLink # newLink(head) <--> oldhead + def deleteHead(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): - self.tail = None #if empty linked list - self.size-=1 + self.head = self.head.next # oldHead <--> 2ndElement(head) + # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be + # removed + self.head.previous = None + if self.head is None: + self.tail = None # if empty linked list return temp - + def insertTail(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) --> - self.size+=1 - + 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) --> + 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 + self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None + self.tail.next = None # 2ndlast(tail) --> None 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 - def isEmpty(self): #Will return True if the list is empty - return(self.head is None) - - def display(self): #Prints contents of the list + + else: # Before: 1 <--> 2(current) <--> 3 + current.previous.next = current.next # 1 --> 3 + current.next.previous = current.previous # 1 <--> 3 + + def isEmpty(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 is not None: current.displayLink() - current = current.next + current = current.next print() - def __len__(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 + 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=" ") + print(f"{self.value}", end=" ") From baf6878bbad0d60bf3900c6c717d20b13ebf939d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 31 May 2020 11:29:54 +0200 Subject: [PATCH 13/13] Revert changes to prime_check.py --- maths/prime_check.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/maths/prime_check.py b/maths/prime_check.py index aa70b3359125..9249834dc069 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -22,9 +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):