Stack Top
Stack Top
Write the following user defined functions to perform given operations on the stack named "status":
(i) Push_element() - To Push an object containing Doc_ID and Doc_nameof doctors who
specialize in Anesthesia to the stack.
(ii) (ii) Pop_element() - To Pop the objects from the stack and display them. Also, display
―Stack Empty when there are no elements in the stack.
Ans:
def push_element(NList):
for i in NList:
if i[-1]==’Anesthesia’:
status.append([i[0],i[1]])
def pop_element():
while len(status)!=0:
print(status.pop())
print('stack is empty')
status=[]
Nlist=[[‘D01’,’GURU’,’9999988888’,’Anesthesia’], [‘D02’,’VARUN’,’9899988889’,’Cardiologist],
[‘D03’,’NEHA’,’979988878’,’Anesthesia’], [‘D04,’BEN’,’9349988834’’,’Medicine’]]]
push_element(NList)
pop_element()
2.Write a function in Python, Push(KItem), where KItem is a dictionary containing the details of
Kitchen items– {Item:price}. The function should push the names of those items in a stack which
have priceless than 100. Also display the average price of elements pushed into the stack.
Kitem={"Spoons":116,"Knife":50,"Plates":180,"Glass":60}
stk=[]
def Push(Kitem):
sum=0
if v<100:
stk.append(k)
sum=sum+v
Push(Kitem)
[Customer_name,Phone_number,City]
Write the following user defined functions to perform given operations on the stack named ‘status’:
i)Push_element() – To Push an object containing name and Phone number of customers who live in
Goa to the stack
ii)Pop_element() – To Pop the objects from the stack and display them. Also, display ‘Stack Empty’
when there are no elements in the stack
For example:
[‘Ashok’,’9999988888’ ,’Goa’]
[‘Avinash’, ‘8888899999’,’Mubai’]
[‘Mahesh’,’7777777777’,’Cochin’]
[‘Rakesh’, ‘6666666666’,’Goa’]
[‘Rakesh’,’666666666’]
[‘Ashok’,’ 9999988888’]
[‘Rakesh’,’666666666’]
[‘Ashok’,’ 9999988888’]
Stack Empty
Ans:
status=[]
def push_element(cust):
for i in cust:
if i[2]==’Goa’:
status.append([i[0],i[1]])
def pop_element():
while len(status)!=0:
print(status.pop())
print('stack is empty')
push_element(cust)
pop_element()
Ans:
def Reverse(Original):
stack=[]
r=’’
for x in original:
stack.append(x)
for x in range(len(stack)):
r=r+stack.pop()
Reverse(original)
5.Write down the status of stack after each operation
Stack=[10,20,30,40]
i)push 70
ii)Push 100
Ans:
i) [10,20,30,40,70]
ii) [10,20,30,40,70,100
iii)] [10,20,30,40,70]
iv)70
6. Write a function in python, Push(Package) and Pop(Package) to add details of employee contain
information (Empid, Ename and Salary) in the form of tuple in Package and delete a Package from a
List of Package Description, considering them to act as push and pop operations of the Stack data
structure.
Ans:
Package=[]
def Push(Package):
while True:
Empid=int(input(“Enter empid”))
Salary=int(input(“Enter Salary”))
if Ch in ‘Nn’:
break
Package.append((Empid,Ename,Salary))
def Pop(Package):
while len(Package)!=0:
print(Package.pop())
print(“Stack is empty”)
7. Choose the correct output for the following stack operation(* top position)
Push(5)
Push(8)
Pop()
Push(2)
Push(5)
Pop()
Push(1)
a) 8 5 2 5 1*
b) 8 5 5 2 1*
c) 2551*
d) 521*
Ans: 5 2 1*
a) STACK.pop( )
b) STACK.append(‘e’)
c) STACK.append(‘f’)
d) STACK.pop( )
Ans:
book(name and mobile numbers), from this dictionary push only phone numbers having
last digit is greater than 5 to a stack implemented by using list and also display the stack if
For example,
mydict={9446789123:"Ram",8889912345:"Sam",7789012367:"Sree"}
Phone number: 9446789123 last digit is less than five which can't be pushed Stack
Ans:
Mydict={9898456123:'tina',9090909097:'Sri'}
stk=[]
def push():
if k%10>5:
stk.append(k)
def pop():
while len(stk)!=0:
print(stk.pop())
print(‘Stack is empty’)
push()
pop()
10. A line of text is read from the input terminal into a stack. Write a program to output the string
in reverse order, each character appearing twice. (e.g., the string a b c d e should be changed to ee
dd cc bb aa)
Ans:
stack=[]
def push():
for x in line:
stack.append(x)
def pop():
while len(stack)!=0:
print(stack.pop()*2,end=' ')
push()
pop()