Stack Avg
Stack Avg
● Push the keys (name of the student) of the dictionary into a stack, where the corresponding
value (CS marks) are more than or equal to 90.
● Pop and display the content of the stack, if the stack is empty display the message as “UNDER
FLOW” For example: If the sample content of the dictionary is as follows:
Govind
Vishwa
Anu
“UNDER FLOW”
Ans:
stk=[]
def PUSH():
if v>=90:
stk.append(k)
def POP():
while len(stk):
print(stk.pop())
print(‘UNDERFLOW’)
PUSH()
POP()
2. Jiya has a list containing 8 integers. You need to help her create a program with two user defined
functions to perform the following operations based on this list.
• Traverse the content of the list and push those numbers into a stack which are divisible by both 5
and 3.
L=[5,15,21,30,45,50,60,75]
Ans:
L=[5,15,21,30,45,50,60,75]
stk=[]
def PUSH():
for x in L:
stk.append(x)
def POP():
while stk:
print(stack.pop())
print(‘Stack empty’)
PUSH()
POP()
[City, Country, distance from Delhi] Each of these records are nested together to form a nested list.
Write the following user defined functions in Python to perform the specified operations on the
stack named travel.
(i) Push_element(NList): It takes the nested list as an argument and pushes a list object
containing name of the city and country, which are not in India and distance is less
than 3500 km from Delhi.
(ii) (ii) Pop_element(): It pops the objects from the stack and displays them. Also, the
function should display “Stack Empty” when there are no elements in the stack.
For example: If the nested list contains the following data:
NList=[["New York", "U.S.A.", 11734], ["Naypyidaw", "Myanmar", 3219], ["Dubai",
"UAE", 2194], ["London", "England", 6693]]
Ans:
def push_element(NList):
for i in NList:
travel.append([i[0],i[1]])
def pop_element():
while len(travel)!=0:
print(travel.pop())
else:
print('stack is empty')
travel=[]
push_element(NList)
pop_element()
4.Which list method can be used to perform Push operation in a stack implemented
by list?
(a) append()
(b) extend()
(c) push()
(d) insert()
Ans:
a)append()
5. Write a function in Python PUSH(Arr),where Arr is a list of numbers, from this list push all even
numbers into a stack implemented by using a list. Display the stack if it has at least one element,
otherwise display “stack empty” message.
Ans:
stk=[]
def push(Arr):
for x in Arr:
if x %2 ==0:
stk.append(x)
def pop():
while len(stk0)!=0:
print(stk.pop())
print(‘stack is empty’)
Display the stack if it has at least one element, otherwise display “stack empty” message.
>>>mydict={1234567890:"Shyam",94567892:"Ram",8657456789012:"Karun",674123789:"Tharun"
}
>>> push(mydict)
Digit of phone number 8 Phone number: 94567892 doesn't have 10 digits, which can't be pushed
Digit of phone number 13 Phone number: 8657456789012 doesn't have 10 digits, which can't be
pushed Stack elements after push operation : [9674123789, 1234567890]
Ans:
mydict={1234567890:"Shyam",94567892:"Ram",8657456789012:"Karun",674123789:"Tharun"}
stk=[]
def PUSH():
if len(str(k))==10:
stk.append(k)
def POP():
while len(stk)!=0:
print(stk.pop())
print(‘stack is empty’)
PUSH()
POP()
7. Consider the following operation performed on a stack of size 3, What will be the output? (* top
position)
Push(10)
Push(20)
Push(30)
Pop()
Push(40)
Push(50)
(a) overflow
(b) underflow
(c) 10 20 30 40 50*
(d) 10 20 40 50*
Ans:
(d) 10 20 40 50*
The function should push the names of those items in the stack who have price
greater than 75. Also display the count of elements pushed into the stack.
For example:
Sitem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
Notebook
Pen
Ans:
stackItem=[]
def Push(SItem):
count=0
for k in SItem:
if (SItem[k]>=75):
stackItem.append(k)
count=count+1
SItem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
Push(SItem)
Ans:
def insert():
Stack.append(item)
def delete():
if len(stack)!=-0:
print(stack.pop())
else:
print(‘UNDERFLOW’)
10. What is LIFO data structure? Give any two applications of a stack?
The insertion of elements is known as PUSH operation and the removal of elements is known
as POP operation. Both operations take place through only one end of the stack called top of
the stack
Applications of a stack:
Reversing word
Finding factorials