Test Index
Stack
© examsnet.com
Question : 8 of 12
Marks:
+1,
-0
(a) A list contains following record of customer:
[Customer_name, Room Type]Write the following user defined functions to perform given operations on the stack named
'Hotel' :(i) Push_Cust() – To Push customers’ names of those customers who are staying in ‘Delux’ Room Type.(ii) Pop_Cust() – To Pop the names of customers from the stack and display them. Also, display “Underflow” when there are no customers in the stack.For example :If the lists with customer details are as follows :["Siddharth", "Delux"]["Rahul", "Standard"]["Jerry", "Delux"]The stack should contain
JerrySiddharthThe output should be:
JerrySiddharthUnderflowOR(b) Write a function in Python,
Push (Vehicle) where, Vehicle is a dictionary containing details of vehicles – {Car_Name: Maker}.The function should push the name of car manufactured by ‘TATA’ (including all the possible cases like Tata, TaTa, etc.) to the stack.For example:If the dictionary contains the following data:Vehicle={"Santro":"Hyundai","Nexon":"TATA","Safari":"Tata"}The stack should containSafariNexon
[CBSE 2023 Set 4]
Solution:
(a)
Hotel = []Customer = [["Siddharth", "Delux"], ["Rahul", "Standard"], ["Jerry", "Delux"]]def Push_Cust(): for rec in Customer: if rec[1] == "Delux": Hotel.append(rec[0])def Pop_Cust(): while len(Hotel) > 0: print(Hotel.pop()) print("Underflow")Push_Cust()Pop_Cust()Explanation: Push_Cust() pushes Siddharth and then Jerry (Rahul is Standard), so Jerry is on top. Pop_Cust() pops and prints Jerry, then Siddharth; when the stack becomes empty it prints Underflow.JerrySiddharthUnderflowOR (b)
stack = []def Push(Vehicle): for car in Vehicle: if Vehicle[car].upper() == "TATA": stack.append(car)Vehicle = {"Santro": "Hyundai", "Nexon": "TATA", "Safari": "Tata"}Push(Vehicle)Explanation: Looping over a dictionary gives its keys (car names). upper() converts the maker to capitals, so TATA, Tata, TaTa all match. Nexon and then Safari are pushed, so the stack (top first) is Safari, Nexon.© examsnet.com
Go to Question: