Test Index
Stack
© examsnet.com
Question : 10 of 12
Marks:
+1,
-0
A list contains following record of course details for a University :
[Course_name, Fees, Duration]Write the following user defined functions to perform given operations on the stack named 'Univ' :(i) Push_element() – To push an object containing the Course_name, Fees and Duration of a course, which has fees greater than 100000 to the stack.(ii) Pop_element() – To pop the object from the stack and display it. Also, display “Underflow” when there is no element in the stack.For example :If the lists of courses details are :[“MCA”,200000,3][“MBA”,500000,2][“BA”,100000,3]The stack should contain :[“MBA”,500000,2][“MCA”,200000,3][CBSE 2023 Compartment]
Solution:
Univ = []def Push_element(Course): for Rec in Course: if Rec[1] > 100000: Univ.append(Rec)def Pop_element(): while len(Univ) > 0: print(Univ.pop()) else: print("Underflow")Course = [["MCA", 200000, 3], ["MBA", 500000, 2], ["BA", 100000, 3]]Push_element(Course)Pop_element()Explanation:(i) Push_element() checks the fees Rec[1] of each record; MCA (200000) and MBA (500000) are pushed with append(). BA (100000) is not, since its fees are not greater than 100000. MBA, pushed last, is on the top.(ii) Pop_element() removes the top element with pop() and displays it, till the stack is empty; then (and also for an empty stack) "Underflow" is displayed.Output:['MBA', 500000, 2]['MCA', 200000, 3]Underflow
© examsnet.com
Go to Question: