Test Index
CBSE Class 12 Computer Science 2026 Paper Compartment
© examsnet.com
Question : 30 of 37
Marks:
+1,
-0
(a) Consider a globally declared dictionary named EMPLOYEE_PERFORMANCE which contains employee IDs and their respective performance scores (out of 100) as key:value pairs. For example, the dictionary EMPLOYEE_PERFORMANCE may contain items as follows :
EMPLOYEE_PERFORMANCE = { "E101": 85, "E102": 92, "E103": 78, "E104": 95, "E105": 80, "E106": 70}Write the following user-defined functions to perform the following given operations on a globally declared list implementing a stack named TOP_PERFORMERS_LIST :(I) PushHighPerformers(): This function should push all employee IDs from the dictionary EMPLOYEE_PERFORMANCE whose performance score is greater than or equal to 90 into the stack TOP_PERFORMERS_LIST. For example, on execution of the PushHighPerformers() function, the stack TOP_PERFORMERS_LIST should contain ["E102", "E104"] (assuming the order of insertion is E102 then E104) from the above considered example of the dictionary EMPLOYEE_PERFORMANCE.(II) IsPerformanceListEmpty(): This function should check whether the stack TOP_PERFORMERS_LIST is empty or not. If it is empty, the function should return True; otherwise, it should return False.(III) PopPerformer(): This function should pop and return the topmost employee ID from the stack TOP_PERFORMERS_LIST. If the stack is already empty (check using the IsPerformanceListEmpty() function), then the function should display the message "Stack Underflow: No top performer to pop."OR(b) Consider a globally declared list of product prices named PRODUCT_PRICES as follows :PRODUCT_PRICES = [ 150.75, 85.00, 299.99, 50.50, 120.00, 99.95, 450.00 ]Write the following user-defined functions to perform the following given operations on a globally declared list implementing a stack named DISCOUNT_PRICES :(I) PushDiscPrices(): This function should push all those prices from the list PRODUCT_PRICES which are less than or equal to 100.00 into the stack DISCOUNT_PRICES. For example, on execution of the PushDiscPrices() function, the stack DISCOUNT_PRICES should contain [85.00, 50.50, 99.95] (assuming this order of pushing is based on appearance in the PRODUCT_PRICES list) from the above considered example of the list PRODUCT_PRICES.(II) PopDiscPrices(): This function should pop and display the last (topmost) price from the stack DISCOUNT_PRICES. If the stack is already empty, then the function should display the message "No More Discount Prices to Pop" and return None.(III) ShowDiscPrices(): This function should display all the prices from the stack DISCOUNT_PRICES without deleting any price from the stack. The content of DISCOUNT_PRICES should be displayed as per stack LIFO rule (i.e., last price to first price). If there are no prices in the stack, then display the message "Stack is Empty: No discount prices to show".
Solution:
Concept: A Python list works as a stack when items are added and removed at the same end:
append() is PUSH and pop() (no index) removes the last item, the TOP. This gives LIFO (last in, first out) order.(a)EMPLOYEE_PERFORMANCE = {"E101": 85, "E102": 92, "E103": 78, "E104": 95, "E105": 80, "E106": 70}TOP_PERFORMERS_LIST = []# (I)def PushHighPerformers(): for emp_id in EMPLOYEE_PERFORMANCE: if EMPLOYEE_PERFORMANCE[emp_id] >= 90: TOP_PERFORMERS_LIST.append(emp_id)# (II)def IsPerformanceListEmpty(): if len(TOP_PERFORMERS_LIST) == 0: return True else: return False# (III)def PopPerformer(): if IsPerformanceListEmpty(): print("Stack Underflow: No top performer to pop.") else: return TOP_PERFORMERS_LIST.pop()Explanation:(I) Looping over the dictionary gives its keys in insertion order. E102 (92) and E104 (95) have scores >= 90, so the stack becomes ["E102", "E104"] with E104 at the top.(II) The stack is empty when its length is 0 (return len(TOP_PERFORMERS_LIST) == 0 is an equally good one-line body).(III) The first call returns "E104", the next returns "E102"; a third call finds the stack empty and prints the underflow message (the function then returns None).OR (b)PRODUCT_PRICES = [150.75, 85.00, 299.99, 50.50, 120.00, 99.95, 450.00]DISCOUNT_PRICES = []# (I)def PushDiscPrices(): for price in PRODUCT_PRICES: if price <= 100.00: DISCOUNT_PRICES.append(price)# (II)def PopDiscPrices(): if len(DISCOUNT_PRICES) == 0: print("No More Discount Prices to Pop") return None else: price = DISCOUNT_PRICES.pop() print(price)# (III)def ShowDiscPrices(): if len(DISCOUNT_PRICES) == 0: print("Stack is Empty: No discount prices to show") else: for i in range(len(DISCOUNT_PRICES) - 1, -1, -1): print(DISCOUNT_PRICES[i])Explanation:(I) 85.00, 50.50 and 99.95 are <= 100.00, so they are pushed in that order; Python stores them as [85.0, 50.5, 99.95] with 99.95 at the top.(II) pop() removes and returns the top item, which is then displayed (first call prints 99.95). On an empty stack the message is shown and None is returned.(III) The loop runs the index from the last position down to 0, so the prices are shown top to bottom (99.95, 50.5, 85.0) and the list itself is not changed. for p in DISCOUNT_PRICES[::-1]: is an equally good loop.Key point: Showing a stack must not delete items, so use indexing or a reversed copy, never pop(), in ShowDiscPrices().© examsnet.com
Go to Question: