0% found this document useful (0 votes)
2 views6 pages

Stack 1 Self

Uploaded by

A. Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Stack 1 Self

Uploaded by

A. Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

STACK IMPLEMENTATION LIST

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [4]: class stackUsingList :


def __init__(self) :
self.__stack = []
def push(self, data) :
self.__stack.append(data)
print(f"pushed {data} into the stack")
def size(self) :
return len(self.__stack)
def is_empty(self) :
return self.size() == 0

def top(self) :
if self.is_empty() :
print("Stack is empty nothing is on the top")
return
return self.__stack[-1]
def pop(self) :
if self.is_empty() :
print("Stack is empty cannot pop from empty stack")
return
return self.__stack.pop()

In [6]: stackSimpleList = stackUsingList()


print(stackSimpleList.is_empty())
print(stackSimpleList.size())
print(stackSimpleList.push(10))
print(stackSimpleList.push(20))
print(stackSimpleList.push(30))
print(stackSimpleList.push(40))
print(stackSimpleList.push(50))
print(stackSimpleList.size())
print(stackSimpleList.pop())
print(stackSimpleList.pop())
print(stackSimpleList.size())
print(stackSimpleList.is_empty())
print(stackSimpleList.top())
True
0
pushed 10 into the stack
None
pushed 20 into the stack
None
pushed 30 into the stack
None
pushed 40 into the stack
None
pushed 50 into the stack
None
5
50
40
3
False
30

STACK USING LINKED LIST

In [ ]:

In [ ]:

In [21]: class Node() :


def __init__(self,data) :
self.data = data
self.head = None

class stackUsingLL() :
def __init__(self) :
self.head = None
self.size = 0

def push(data) :
newNode = Node(data)
if self.head == None :
self.head = newnode
self.size += 1
return f"added {data} to the stack"
else :
newNode.next = self.head
self.head = newNode
return f"added {data} to the stack"

def top(self) :

if self.head is None or self.size() == 0 :


return "stack is empty, no top element "
return self.head.data

def pop(self) :
if self.head is None or self.size == 0 :
return "cannot pop on value from empty stack"
ans = self.head.data
self.head = self.head.next
self.size -= 1

return ans
def is_empty(self) :
return self.size == 0

def lengthLL(self) :
return self.size
In [22]: mystack = stackUsingLL()

In [23]: print(mystack.is_empty())
print(mystack.lengthLL())
print(mystack.push(10))
print(mystack.push(20))
print(mystack.push(30))
print(mystack.push(40))
print(mystack.is_empty())
print(mystack.pop())
print(mystack.pop())
print(mystack.size())
print(mystack.top())

True
0
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[23], line 3
1 print(mystack.is_empty())
2 print(mystack.lengthLL())
----> 3 print(mystack.push(10))
4 print(mystack.push(20))
5 print(mystack.push(30))

TypeError: stackUsingLL.push() takes 1 positional argument but 2 were given

In [ ]:

In [3]: class Stack() :


def __init__(self) :
self.stack = []
self.len = 0
def push(self, data) :
self.stack.append(data)
self.len += 1
def top(self) :
if self.len == 0 :
return None
else :
return self.stack[ -1 ]
def pop(self) :
if self.len == 0 :
return None
self.stack.pop()
self.len -= 1
def is_empty(self) :
return self.len == 0

def next_greater_element(a, n):


# TODO: Implement this function
ans = []
s = Stack()
for i in range(n) :

while s.is_empty() == False :

if s.top() > a[n - i - 1] :


ans.insert(0,s.top())
s.push(a[n - i - 1])
break
else :
s.pop()

if (s.is_empty()) :
ans.insert(0,-1)
s.push(a[n - i - 1])
return ans
pass

In [4]: next_greater_element([4, 5, 2, 25], 4)

Out[4]: [5, 25, 25, -1]

In [11]: def largestRectangleArea(heights):


# Implement your solution here
# 1, 4, 2 , 5, 3
areas = []

for i in range(len(heights)) :
left = i
right = i
while left >= 0 and heights[left] >= heights[i] :
left -= 1
while right < len(heights) and heights[right] >= heights[i] :
right += 1
areas.append((right - left - 1 ) * heights[i])

return max(areas)
pass

In [12]: largestRectangleArea( [2, 1, 5, 6, 2, 3] )

Out[12]: 10

In [15]: class Stack() :


def __init__(self) :
self.stack = []
self.length = 0
def push(self,data) :
self.stack.append(data)
self.length += 1
def size(self) :
return self.length
def is_empty(self) :
return self.length == 0
def pop(self) :
if self.length == 0 :
return None
else :
length -= 1
return self.stack.pop()
def top(self) :
if self.length == 0 :
return None
else :
return self.stack[-1]

def prev_smalller_index(arr) :
ps = []
stack = Stack()
for i in range(len(arr)) :
while stack.is_empty == False :
if stack.top() > arr[i] :
stack.pop()
else :
ps.append(i)
stack.push(arr[i])
if stack.is_empty() :
stack.push(arr[i])
ps.append(-1)

return ps
def next_smaller_index(arr) :
ns = []
stack = Stack()
for i in range(len(arr)) :
while stack.is_empty == False :
if stack.top() > arr[len(arr) - i - 1] :
stack.pop()
else :
ns.insert(0,i)
stack.push(arr[len(arr) - i - 1])
if stack.is_empty() :
ns.insert(0, len(arr))
stack.push(arr[len(arr) - i - 1])
return ns

def largestRectangleArea(heights):
# Implement your solution here

# ******************************************************************************

# NAIVE BRUTE FORCE APPROACH BUT GOOD FOR UNDERSTANDING AT LEAST


# 1, 4, 2 , 5, 3
# areas = []

# for i in range(len(heights)) :
# left = i
# right = i
# while left >= 0 and heights[left] >= heights[i] :
# left -= 1
# while right < len(heights) and heights[right] >= heights[i] :
# right += 1
# areas.append((right - left - 1 ) * heights[i])

# return max(areas)
# ********************************************************************************

ps = prev_smalller_index(heights)
ns = next_smaller_index(heights)
print(ps)
print(ns)
max_area = 0
for i in range(heights) :

area = (ps[i] - ns[i] -1 ) * heights[i]


if area > ma_area :
max_area = area
return max_area

pass

In [16]: largestRectangleArea( [2, 1, 5, 6, 2, 3] )

[-1]
[6]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[16], line 1
----> 1 largestRectangleArea( [2, 1, 5, 6, 2, 3] )

Cell In[15], line 79, in largestRectangleArea(heights)


77 print(ns)
78 max_area = 0
---> 79 for i in range(heights) :
81 area = (ps[i] - ns[i] -1 ) * heights[i]
82 if area > ma_area :

TypeError: 'list' object cannot be interpreted as an integer

In [17]: prev_smalller_index([2, 1, 5, 6, 2, 3])


Out[17]: [-1]

In [24]: class Stack() :


def __init__(self) :
self.stack = []
self.length = 0
def push(self,data) :
self.stack.append(data)
self.length += 1
def size(self) :
return self.length
def is_empty(self) :
return self.length == 0
def pop(self) :
if self.length == 0 :
return None
else :
length -= 1
return self.stack.pop()
def top(self) :
if self.length == 0 :
return None
else :
return self.stack[-1]

def prev_smalller_index(arr) :
ps = []
stack = Stack()
for i in range(len(arr)) :
while stack.is_empty == False :

if stack.top() > arr[i] :


stack.pop()
else :
ps.append(i - 1 )
stack.push(arr[i])
if stack.is_empty() :
stack.push(arr[i])
ps.append(-1)
print(stack.stack)

return ps

In [25]: prev_smalller_index([2, 1, 5, 6, 2, 3])

[2]
Out[25]: [-1]

In [ ]:

In [ ]:

In [ ]:

You might also like