stack class in Python - python

I have written the below code to implement the stack class instead of using the build in function, not sure if I have got it rigth. Can someone please check for me and advise if there is an error?
class DSAStack():
maxCap = 100
def __init__(self):
self.stack = []
self.count = 0
def isEmpty(self):
if self.count == 0:
return True
else:
return false
def isFull(self):
if self.count == maxCap:
return True
else:
return false
def push(self, item):
if self.isFull:
raise ('Stack is full...')
else:
self.stack[self.count] = item
self.count += 1
def pop(self):
self.topVal = self.top
self.stack[self.count - 1] = 0
self.count = self.count - 1
return self.topVal
def top(self):
if self.isEmpty:
raise ('Stack is empty...')
else:
self.top = self.stack[self.count - 1]

Firstly, you need not keep track of count. Accessing the length of the list will do that for you.
def __init__(self):
self.stack =[]
Now, your logic for isEmpty and isFull was alright, but since we are removing the self.count variable, here's how you'd go about it
def isEmpty(self):
return len(self.stack) == 0 #Directly return true or false
def isFull(self):
return len(self.stack) == maxCap #Directly return true or false
In your push function, there are a few things I'd like to point out.
First, you need to call the isFull function. So we need to add parentheses to it.
Secondly, you cant just raise ('Stack is full...'), you will get a TypeError: Exceptions must derive from BaseException. This basically means what you raise must be some type of error.
Lastly, you can't add a new element by doing self.stack[self.count]=item since you will get an IndexError
Here are the changes:
def push(self,item):
if self.isFull(): #Need to call the function, self.isFull is not a variable
raise Exception('Stack is full...') #Or any other type of error
else:
self.stack.append(item) #Adds new item to the end of self.stack
Now coming to the pop function, setting a value to zero in a list does not really get rid of it. And it can cause a lot of confusion, especially since we are using the len(self.stack) to implement this.
Here's how you would pop:
def pop(self):
if self.isEmpty():
raise Exception('Stack is empty...')
else:
return self.stack.pop() #List has a built-in pop method
So now we don't really need the top function. And that concludes that. Btw, in your top function, you have defined self.top = self.stack[self.count-1]
Giving the same name to a function and a variable is never really a good idea.
To implement the pop functionality yourself, you could do the following:
def pop(self):
if self.isEmpty():
raise Exception('Stack is empty...')
else:
topVal = self.stack[-1] #-1 gives you the last element
#if stack will contain objects instead of primitive data types, use self.stack[-1].copy()
del self.stack[-1] #Deletes the element
return topVal
Polishing your top function will be like this:
def top(self):
if self.isEmpty():
raise Exception('Stack is empty...')
else:
return self.stack[-1]
def pop(self):
topVal = self.top()
del self.stack[-1]
return topVal
Note how the top function is defined before the pop function.
Also, try to test the code out and resolving any issues.

Related

I am trying to write an algorithm that uses a stack to check if an expression has balanced parentheses but I keep encountering this error

def is_matched(expression):
left_bracket = "[({"
right_bracket = "])}"
my_stack = Stack(len(expression))
# our solution methodology is to go through the expression and push all of the the open brackets onto the stack and then
# with the closing brackets - each time we encounter a closing bracket we will pop the stack and compare
for character in expression:
if character in left_bracket:
my_stack.push(character)
elif character in right_bracket:
# first check to see that the stack is not empty i.e we actually have some opneing brackets in the expression
if my_stack.is_empty():
return False
# now we need to check that the type of braket we pop is the equivalent of it's closing bracket in the expression
if right_bracket.index(character) != left_bracket.index(my_stack.pop):
return False
return my_stack.is_empty()
print(is_matched("()"))
if right_bracket.index(character) != left_bracket.index(my_stack.pop):
TypeError: expected a string or other character buffer object
python-BaseException
here is my stack implementation:
class Stack:
def __init__(self, capacity):
"""Builds a stack with given capacity > 0."""
if capacity <= 0:
raise Exception("The capacity must be positive")
self.the_array = [None] * capacity
self.top = -1 # the index of the top element
def size(self):
"""Returns the size, i.e. the number
of elements in the container."""
return self.top + 1
def is_empty(self):
"""Returns True if and only if the container is empty."""
return self.size() == 0
def is_full(self):
"""Returns True if and only if the container is full."""
return self.size() >= len(self.the_array)
def push(self, item):
"""Places the given item at the top of the stack
if there is capacity, or raises an Exception."""
if self.is_full():
raise Exception("The stack is full")
self.top += 1
self.the_array[self.top] = item
def pop(self):
"""Removes and returns the top element of the stack,
or raises an Exception if there is none."""
if self.is_empty():
raise Exception("The stack is empty")
item = self.the_array[self.top]
# removes a reference to this item,
# helps with memory management and debugging
self.the_array[self.top] = None
self.top -= 1
return item
def reset(self):
"""Removes all elements from the container."""
while not self.is_empty():
self.pop()
assert (self.is_empty)
It should upon the second iteration pop the stack and notice that the indexes of the right and left bracket are the same and move into the final iteration where it realises the stack is empty and returns True but it is not doing so but instead throwing a typeError.
Any help is appreciated.
Thank you
at this line:
if right_bracket.index(character) != left_bracket.index(my_stack.pop):
you actually need to call pop method, since pop is a method, not a property.
therefore it should look like this:
if right_bracket.index(character) != left_bracket.index(my_stack.pop()):

Max stack in python - 2 stacks solution

I am trying to build a max stack in python, I am not sure what I am doing wrong.
Here is the question>
Design a max stack data structure that supports the stack operations
and supports finding the stack's maximum element.
Implement the MaxStack class:
MaxStack() Initializes the stack object.
void push(int x) Pushes element x onto the stack.
int pop() Removes the element on top of the stack and returns it.
int top() Gets the element on the top of the stack without removing it.
int peekMax() Retrieves the maximum element in the stack without removing it.
int popMax() Retrieves the maximum element in the stack and removes it.
If there is more than one maximum element, only remove the top-most one.
class MaxStack:
def __init__(self):
self.stack = []
self.stack_max = []
def push(self, x: int) -> None:
self.stack.append(x)
if not self.stack_max or x > self.stack_max[-1][0]:
self.stack_max.append([x, 1])
elif x == self.stack_max[-1][0]:
self.stack_max[-1][1] += 1
else:
self.stack_max.append(self.stack_max[-1])
def pop(self) -> int:
if not self.stack_max or self.stack:
return
if self.stack_max[-1][0] == self.stack[-1]:
self.stack_max[-1][1] -= 1
if self.stack_max[-1][1] == 0:
del self.stack_max[-1]
return self.stack.pop()
def top(self) -> int:
return self.stack[-1]
def peekMax(self) -> int:
if self.stack_max:
return self.stack_max[-1][0]
def popMax(self) -> int:
if self.stack_max:
return self.stack_max.pop()[0]
Example code:
obj = MaxStack()
obj.push(6)
param_2 = obj.pop()
param_3 = obj.top()
param_4 = obj.peekMax()
param_5 = obj.popMax()
Input :
["MaxStack","push","push","push","top","popMax","top","peekMax","pop","top"] [[],[5],[1],[5],[],[],[],[],[],[]]
Output:
[null,null,null,null,5,5,5,5,None,5]
Expected:
[null,null,null,null,5,5,1,5,1,5]
Reference: Leetcode 716. Max Stack
class MaxStack:
def __init__(self,value):#taking some value to avoid empty obj
self.stack=[value]
self.max_=[value,0]
self.length=1
def push(self,value):
self.stack.append(value)
self.length+=1
if value>self.max_[0]:
self.max_[0],self.max_[1]=value,self.length-1
def pop(self):
if self.length==0:
return
elif self.stack[-1]==self.max_[0]:
self.popMax()
else:
self.stack.pop()
self.length-=1
def top(self):
print(self.stack[-1])
def peekMax(self):
print(self.max_[0])
def popMax(self):
if self.length==0 or self.max_[1]==-1:
return
self.stack.pop(self.max_[1])
self.length-=1
self.max_[0],self.max_[1]=-1,-1
for i in range(self.length):
if self.stack[i]>self.max_[0]:
self.max_[0],self.max_[1] = self.stack[i],i
Sorry for the improper indentations, I tried a lot to fix it. Anyways this should work and I wanted to try it out on leetcode but it needs a login. Let me know if there is any issue.
To me, it seems a little confusing to track the count in the tuple of the value, when you could just continue adding to the max stack and counting them from the list if you need that.
class MaxStack:
def __init__(self):
self.stack = []
self.maxes = []
def push(self, n):
self.stack.append(n)
if not self.maxes or n >= max(self.maxes):
self.maxes.append(n)
def pop(self):
if self.stack:
val = self.stack.pop()
if val in self.maxes:
self.maxes.remove(val)
def top(self):
if self.stack:
return self.stack[-1]
def peek_max(self):
if self.maxes:
return self.maxes[-1]
def pop_max(self):
if self.maxes:
return self.maxes.pop()
Then if you need the count of the number of each, just use count():
def max_count(self):
if self.maxes:
return self.maxes.count(max(self.maxes))

Recursion and return statements in python

I am trying to implement a 2-3 tree but I am having trouble with the find method.
This method given an int as parameter should return the node that contains the int.
The problem is that sometimes it works, sometimes it does't and I don't know why.
I have added a test print. For a particular int that I know for sure that is part of the tree, the code executes the print statement, meaning that it has found the node, but does not return this node. Instead it return False which is at the end of the code.
Can you help me solving this ?
def find(self,data,node=0): #return problem ???
if node==0:
a=self.root
else:
a=node
if a.data2==None:
if data==a.data: ### here is the problem
print("qwertyuiop") ### it does not execute the return statement
return a
elif data < a.data:
if a.left!=None:
return self.find(data,a.left)
elif data > a.data:
if a.right!=None:
return self.find(data,a.right)
else:
if a.data2!=None:
if (data==a.data or data==a.data2):
return a
elif data<a.data:
if a.left!=None:
return self.find(data,a.left)
elif (data>a.data and data<a.data2):
if a.middle!=None:
return self.find(data,a.middle)
elif data>a.data2:
if a.right!=None:
return self.find(data,a.right)
print("Not Found") ### function executes this print
return False
self.root is the root of the tree and is an object of the following class
class Node:
def __init__(self, data=None, left=None, right=None):
self.data = data
self.data2 = None
self.data3 = None
self.left = left
self.right = right
self.middle = None
self.middle2 = None
Binary Search Tree:
class Nodeee:
def __init__(self, data=None, left=None, right=None):
self.data = data
self.left = left
self.right = right
class BST:
def __init__(self, root=None):
self.c=[]
self.total=0
self.root = None
def parent(self,data,node=5):
def search(nodee,cc,data):
if data==cc.data:
return nodee
else:
if data<cc.data:
nodee=cc
return search(nodee,cc.left,data)
elif data>cc.data:
nodee=cc
return search(nodee,cc.right,data)
print("Parent Error")
return False
if node==self.root:
print("Root has no parent")
else:
a=self.root
c=self.root
return search(a,c,data)
def lookup(self,data,node=0):
if node==0:
a=self.root
else:
a=node
if data < a.data:
if a.left==None:
return a
else:
return self.lookup(data,a.left)
elif data > a.data:
if a.right==None:
return a
else:
return self.lookup(data,a.right)
def find(self,data,node=0):
if node==0:
a=self.root
else:
a=node
if data==a.data:
print("WTF")
return a
elif data < a.data:
if a.left!=None:
return self.find(data,a.left)
elif data > a.data:
if a.right!=None:
return self.find(data,a.right)
print("Not Found")
return False
def find2(self,data,node=0):
if node==0:
a=self.root
else:
a=node
if data==a.data:
return True
elif data < a.data:
return self.find2(data,a.left)
elif data > a.data:
return self.find2(data,a.right)
return False
def is_empty(self):
if self.root==None:
return True
def is_leaf(self,n):
if (n.left==None and n.right==None):
return True
return False
def delete(self):
self.root=None
def insert(self, data):
if self.root==None:
self.root=Nodeee(data)
self.total+=1
return True
else:
b=self.lookup(data)
if data < b.data:
b.left=Nodeee(data)
self.total+=1
return True
elif data > b.data:
b.right=Nodeee(data)
self.total+=1
return True
print("Insert Error !")
return False
def inorder_swap(self,data):
a=self.find(data)
b=a.right
while self.is_leaf(b)!=True:
if b.left!=None:
b=b.left
elif b.left==None:
b=b.right
temp=a.data
a.data=b.data
b.data=temp
def remove(self,data):
a=self.find(data)
if self.is_leaf(a)==True:
b=self.parent(data)
if b.left==a:
b.left=None
elif b.right==a:
b.right=None
elif self.is_leaf(a)==False:
if a.left==None:
b=self.parent(data)
if b.left==a:
b.left=b.left.right
elif b.right==a:
b.right=b.right.right
elif a.right==None:
b=self.parent(data)
if b.left==a:
b.left=b.left.left
elif b.right==a:
b.right=b.right.left
elif (a.left!=None and a.right!=None):
self.inorder_swap(data)
self.remove(data)
def inorder(self,node):
if node!=None:
self.inorder(node.left)
self.c.append(node.data)
self.inorder(node.right)
def inorder_print(self):
self.c=[]
self.inorder(self.root)
print("\nStart")
for x in range(len(self.c)):
print(self.c[x], end=",")
print("\nFinish\n")
a=BST()
print(a.insert(234)==True)
print(a.insert(13)==True)
print(a.insert(65)==True)
print(a.insert(658)==True)
print(a.insert(324)==True)
print(a.insert(86)==True)
print(a.insert(5)==True)
print(a.insert(76)==True)
print(a.insert(144)==True)
print(a.insert(546)==True)
print(a.insert(2344)==True)
print(a.insert(1213)==True)
print(a.insert(6345)==True)
print(a.insert(653348)==True)
print(a.insert(35324)==True)
print(a.insert(8463)==True)
print(a.insert(5555)==True)
print(a.insert(76539)==True)
print(a.insert(14499)==True)
print(a.insert(59999946)==True)
a.inorder_print()
a.remove(35324)
a.remove(1213)
a.remove(2344)
a.remove(144)
a.remove(5555)
a.remove(6345)
a.remove(59999946)
a.remove(76)
print(a.root.data)
a.inorder_print()
def inorder_swap(self,data):
a=self.find(data)
b=a.right
while self.is_leaf(b)!=True:
if b.left!=None:
b=b.left
elif b.left==None:
b=b.right
temp=a.data
a.data=b.data
b.data=temp
a here is the node containing the passed data. This method does nothing else than swapping a's data with some leaf's data (first one while finds), thereby distorting the tree order. A follow-up find on the same data therefore fails and returns False. Since your code has no error checks, this results in an AttributeError.
You probably want to move nodes around in inorder_swap. However you only assign to the local name b. If you want to change nodes, then you need to use b.left = or b.right =.
It might be that there are more problems, that I don't see right now.
Also your code has several style problems, some of them:
You have four functions doing the same: find, find2, lookup and search in parent.
Most of the naming is not informative or even confusing.
Lines like if a.right==None: should be written as if not a.right: (or maybe if a.right is None:).
Check the return value of functions and don't just assume they return a valid node if they might not (i.e. find might return False instead of a node). Or alternatively use exception handling.
If you have an if ... elif ... elif block you don't have to check the last possibility if it is sure to be true, for example:
if b.left!=None:
# something
elif b.left==None:
# something else
should be
if b.left:
# something
else:
# something else

Ambiguous behavior of using "not" and "bool" together on an empty list in Python

In this simple implementation of Stack using Python lists. I have a small method to check if the stack is empty (list empty). I am using this method as a check condition for pop method. The pop method should raise an exception when trying to pop an empty stack. For some reason, this doesn't seem to work. Am I missing something here? Please help!
__author__ = '#arun'
# Stack simple implementation using Python list
class Stack(object):
def __init__(self):
self.items = []
def push(self, value):
self.items.append(value)
def is_empty(self):
return not bool(self.items)
def pop(self):
print self.is_empty()
if self.is_empty: # if not stack is empty
return self.items.pop()
else:
raise Exception("Stack Empty!")
def peek(self):
return self.items[-1]
def get_size(self):
return len(self.items)
if __name__ == '__main__':
stack = Stack()
print stack.get_size()
stack.push(23)
stack.push(3)
print stack.get_size()
print stack.pop()
print stack.pop()
print stack.get_size()
#print stack.pop()
Output:
0
2
False
3
False
23
0
Why is pop() method popping out elements when is_empty() is returning False?!
if self.is_empty:
will always evaluate to True, since it is a Truthy value. You need to call the function like this
if self.is_empty():
And a suggestion: You might want to pop the values only when is_empty() is False, like this
if not self.is_empty():

Stack data structure in python

I have 2 issues with the code below:
push(o) throws an exception TypeError: can only assign an iterable.
Should I throw an exception if pop() is invoked on an empty stack ?
class Stack(object):
def __init__(self):
self.storage = []
def isEmpty(self):
return len(self.storage) == 0
def push(self,p):
self.storage[:0] = p
def pop(self):
"""issue: throw exception?"""
return None
No need to jump through these loops, See 5.1.1 Using Lists as Stacks
If you insist on having methods isEmpty() and push() you can do:
class stack(list):
def push(self, item):
self.append(item)
def isEmpty(self):
return not self
You are right to use composition instead of inheritance, because inheritance brings methods in that you don't want to expose.
class Stack:
def __init__(self):
self.__storage = []
def isEmpty(self):
return len(self.__storage) == 0
def push(self,p):
self.__storage.append(p)
def pop(self):
return self.__storage.pop()
This way your interface works pretty much like list (same behavior on pop for example), except that you've locked it to ensure nobody messes with the internals.
I won't talk about the list structure as that's already been covered in this question. Instead I'll mention my preferred method for dealing with stacks:
I always use the Queue module. It supports FIFO and LIFO data structures and is thread safe.
See the docs for more info. It doesn't implement a isEmpty() function, it instead raises a Full or Empty exception if a push or pop can't be done.
Stack follows LIFO mechanism.You can create a list and do a normal append() to append the element to list and do pop() to retrieve the element out of the list which you just inserted.
Here is an example for stack class
class Stack(object):
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[-1]
def isEmpty(self):
return len(self.items) == 0
class Stack:
def __init__(self):
self.items=[]
def isEmpty(self):
return self.items==[]
def push(self , item):
self.items.append(item)
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
def peek(self):
return self.items[-1]
Create a stack
To create a new stack we can simply use Stack()
for example:
s=Stack()
"s" is the name of new stack
isEmpty
By using isEmpty() we can check our stack is empty or not
for example:
we have two stacks name s1=(0,1,4,5,6) and s2=()
if we use print(s1.isEmpty()) it will return False
if we use print(s2.isEmpty()) it will return True
push
By using push operation we can add items to top of the stack
we can add "6" to the stack name "s" using
s.push(6)
pop
we can use pop operation to remove and return the top item of a stack
if there is a stack name "s" with n amount items (n>0)
we can remove it's top most item by using
s.pop()
size
This operation will return how many items are in the stack
if there is a stack name "s" s=(1,2,3,4,5,3)
print(s.size())
will return "6"
peek
This operation returns the top item without removing it
print(s.peek())
"we can print items of the stack using print(s.items)"
class Stack:
def __init__(self):
self.stack = []
def pop(self):
if self.is_empty():
return None
else:
return self.stack.pop()
def push(self, d):
return self.stack.append(d)
def peek(self):
if self.is_empty():
return None
else:
return self.stack[-1]
def size(self):
return len(self.stack)
def is_empty(self):
return self.size() == 0
class stack:
def __init__(self,n):##constructor
self.no = n ##size of stack
self.Stack = [] ##list for store stack items
self.top = -1
def push(self):##push method
if self.top == self.no - 1 :##check full condition
print("Stack Overflow.....")
else:
n = int(input("enter an element :: "))
self.Stack.append(n) ## in list add stack items use of append method
self.top += 1##increment top by 1
def pop(self):## pop method
if self.top == -1: #check empty condition
print("Stack Underflow....")
else:
self.Stack.pop()## delete item from top of stack using pop method
self.top -= 1 ## decrement top by 1
def peep(self): ##peep method
print(self.top,"\t",self.Stack[-1]) ##display top item
def disp (self): #display method
if self.top == -1:# check empty condition
print("Stack Underflow....")
else:
print("TOP \tELEMENT")
for i in range(self.top,-1,-1): ## print items and top
print(i," \t",self.Stack[i])
n = int(input("Enter Size :: ")) # size of stack
stk = stack(n) ## object and pass n as size
while(True): ## loop for choice as a switch case
print(" 1: PUSH ")
print(" 2: POP ")
print(" 3: PEEP ")
print(" 4: PRINT ")
print(" 5: EXIT ")
option = int(input("enter your choice :: "))
if option == 1:
stk.push()
elif option == 2:
stk.pop()
elif option == 3:
stk.peep()
elif option == 4:
stk.disp()
elif option == 5:
print("you are exit!!!!!")
break
else:
print("Incorrect option")

Categories

Resources