Skip to content

Stacks and Queues #49

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions Shizler/Stacks and Queues/Queues/deque.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import collections
def Deque():
de = collections.deque([1,2,3])
print("Initial deque",de)

de.append(4)
print("Element added from right",de)

de.appendleft(10)
print("Element added from left",de)

de.pop()
print("Element removed from right",de)

de.popleft()
print("Element removed from left",de)


Deque()
42 changes: 42 additions & 0 deletions Shizler/Stacks and Queues/Queues/myQueue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).

# Implement the MyQueue class:

# void push(int x) Pushes element x to the back of the queue.

# int pop() Removes the element from the front of the queue and returns it.

# int peek() Returns the element at the front of the queue.

# boolean empty() Returns true if the queue is empty, false otherwise.
class MyQueue:
def __init__(self) -> None:
self.stackInput = []
self.stackOutput = []
def push(self,val):
self.stackInput.append(val)
def pop(self):
if not self.stackOutput:
while self.stackInput:
self.stackOutput.append(self.stackInput.pop())
return self.stackOutput.pop()
def peek(self):
if not self.stackOutput:
while self.stackInput:
self.stackOutput.append(self.stackInput.pop())
return self.stackOutput[-1]
def empty(self):
return not self.stackInput and not self.stackOutput


if __name__ == '__main__':
que = MyQueue()
que.push(42)
print(que.pop()) #42
que.push(14)
print(que.empty()) #False
print(que.peek()) #14
que.push(28)
que.push(60)
print(que.empty()) #False
print(que.pop()) #14
47 changes: 47 additions & 0 deletions Shizler/Stacks and Queues/Queues/myStack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Q2: Implement Stack using Queues

# Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

# Implement the MyStack class:

# void push(int x) Pushes element x to the top of the stack.

# int pop() Removes the element on the top of the stack and returns it.

# int top() Returns the element on the top of the stack.

# boolean empty() Returns true if the stack is empty, false otherwise.
class MyStack:
def __init__(self) -> None:
self.queueInput = []
self.queueOutput = []
self.top_elem = 0
def push(self,val):
self.queueInput.append(val)
self.top_elem = val
print(self.queueInput)
def pop(self):
i = len(self.queueInput)
while i > 1:
self.queueOutput.append(self.queueInput.pop(0))
i-=1
self.top_elem = self.queueInput.pop()

self.queueInput, self.queueOutput = self.queueOutput, self.queueInput

return self.top_elem
def top(self):
return self.queueInput[-1]
def empty(self):
return not self.queueOutput


if __name__ == '__main__':
st = MyStack()
st.push(1)
st.push(2)
st.push(3)
print(st.top())
print(st.pop())
print(st.top())
print(st.empty())
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions Shizler/Stacks and Queues/Stacks/minStack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# MinStack() initializes the stack object.
# void push(int val) pushes the element val onto the stack.
# void pop() removes the element on the top of the stack.
# int top() gets the top element of the stack.
class MinStack:
def minStack(self):
global stack, min_stack
stack = []
min_stack = []
print(stack)
def push(self,a):
if len(min_stack) == 0 or a < min_stack[-1]:
min_stack.append(a)
stack.append(a)
print(stack)
def pop(self):
if stack[-1] == min_stack[-1]:
min_stack.pop()
stack.pop()
print(stack)
def top(self):
if len(stack) > 0:
print(stack[-1])
def getMin(self):
val = min_stack[-1]
print(val)
# Input
# ["MinStack","push","push","push","getMin","pop","top","getMin"]
# [[],[-2],[0],[-3],[],[],[],[]]
if __name__ == '__main__':
s1 = MinStack()
s1.minStack()
s1.push(-2)
s1.push(0)
s1.push(-3)
s1.getMin()
s1.pop()
s1.top()
s1.getMin()



16 changes: 16 additions & 0 deletions Shizler/Stacks and Queues/Stacks/reverseString.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def reverseString(s):
stack = [] #create new stack
new_s = ''
# add all chars to stack
for i in s:
stack.append(i)

# remove chars from stack and add them to string
for i in s:
popped = stack.pop()
new_s = new_s + popped

print(new_s)

s = 'qwerty'
reverseString(s)
28 changes: 28 additions & 0 deletions Shizler/Stacks and Queues/Stacks/scoreOfParenthesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Given a balanced parentheses string s, return the score of the string.
# The score of a balanced parentheses string is based on the following rule:
# "()" has score 1.
# AB has score A + B, where A and B are balanced parentheses strings.
# (A) has score 2 * A, where A is a balanced parentheses string.
# Examples:
# Input: s = "()"
# Output: 1
# Input: s = "(())"
# Output: 2
# Input: s = "()()"
# Output: 2
# Input: s = "(()(()))"
# Output: 6
def scoreOfParenthesis(s):
stack = []
score=0
for bracket in s:
if bracket == '(':
stack.append(score)
score = 0
else:
score = stack.pop() + max(2*score,1)
return score
if __name__ == '__main__':
ss = ["()","(())","()()","(()(()))"]
for s in ss:
print(scoreOfParenthesis(s))
25 changes: 25 additions & 0 deletions Shizler/Stacks and Queues/Stacks/stackOverflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class stackOverflow():
def __init__(self,size):
self.size = size
self.top = 0
self.stack = [0]*self.size

def push(self,val):
if(self.top > len(self.stack)-1):
print("Stack overflow")
# extend the array and push()
self.stack = self.stack + ([0]*len(self.stack))
self.stack[self.top] = val
self.top+=1
else:
self.stack[self.top] = val
self.top+=1

return self.stack

if __name__ == '__main__':
st = stackOverflow(3)
print(st.push(1))
print(st.push(2))
print(st.push(3))
print(st.push(4))
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.