Skip to content

data_structures/linked_list: Add __len__() function and tests #2047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions data_structures/linked_list/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ class Node:
def __init__(self, item, next):
self.item = item
self.next = 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():
return None
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): #add python predefined len function
return self.size
10 changes: 8 additions & 2 deletions data_structures/linked_list/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,13 +21,15 @@ 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
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
return temp

def insertTail(self, x):
Expand All @@ -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):
Expand All @@ -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)

Expand All @@ -67,7 +72,8 @@ def display(self): #Prints contents of the list
current.displayLink()
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
Expand Down
5 changes: 3 additions & 2 deletions maths/prime_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ 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)
# 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)


class Test(unittest.TestCase):
Expand Down