Advertisements
Advertisements
Question
A list contains the following record of a customer: [Customer_name, Phone_number, City]
Write the following user-defined functions to perform given operations on the stack named ‘status’:
- Push_element() - Push an object containing the name and Phone number of customers who live in Goa to the stack.
- Pop_element() - Pop the objects from the stack and display them. Also, display “Stack Empty” when there are no elements in the stack.
For example:
If the lists of customer details are:
[“Gurdas”, “99999999999”,”Goa”]
[“Julee”, “8888888888”,”Mumbai”] [“Murugan”,”77777777777”,”Cochin”]
[“Ashmit”, “1010101010”,”Goa”]
The stack should contain
[“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
The output should be:
[“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
Stack Empty
Short Note
Solution
status=[]
def Push_element (cust) :
if cust[2]=="Goa":
L1=[cust[0], cust[1]]
status.append (L1)
def Pop_element ():
num=len (status)
while len(status)!=0:
dele=status.pop ()
print (dele)
num=num-1
else:
print("Stack Empty")
shaalaa.com
Is there an error in this question or solution?