Test Index
CBSE Class 12 Computer Science 2025 Paper Compartment
© examsnet.com
Question : 30 of 37
Marks:
+1,
-0
(a) A stack named KeyStack contains records of some computer keyboards. Each record is represented as a list containing Make, Keys, Connectivity. The Make and Connectivity are strings, and Keys is an integer. For example, a record in the stack may be
('Hitech', 105, 'USB').Write the following user-defined functions in Python to perform the specified operations on KeyStack :(I) push_key(KeyStack, new_key): This function takes the stack KeyStack and a new record new_key as arguments and pushes this new record onto the stack.(II) pop_key(KeyStack): This function pops the topmost record from the stack and returns it. If the stack is already empty, the function should display the message “Underflow”.(III) isEmpty(KeyStack): This function checks whether the stack is empty. If the stack is empty, the function should return True, otherwise the function should return False.OR(b) Write the following user-defined functions in Python :(I) push_vowels(S,St): Here S is a string and St is a list representing a stack. The function should push all the vowels of the string S onto the stack St. For example, if the string S is "Easy Concepts", then the function push_vowels() should push the elements 'E','a','o','e' onto the stack.(II) pop_one(St) : The function should pop an element from the stack St, and return this element. If the stack is empty, then the function should display the message ‘Stack Underflow’, and return None.(III) display_all(St): The function should display all the elements of the stack St, without deleting them. If the stack is empty, the function should display the message ‘Empty Stack’.
Solution:
Concept: A Python list works as a stack when the end of the list is the top:
append() is push and pop() removes the last (top) element. An empty list has length 0.(a)# (I) push a record on the top of the stackdef push_key(KeyStack, new_key): KeyStack.append(new_key)# (II) pop the top record; report underflow if emptydef pop_key(KeyStack): if len(KeyStack) == 0: print("Underflow") else: return KeyStack.pop()# (III) True if the stack is empty, else Falsedef isEmpty(KeyStack): if len(KeyStack) == 0: return True else: return FalseExample: after push_key(K, ('Hitech', 105, 'USB')), isEmpty(K) returns False and pop_key(K) returns ('Hitech', 105, 'USB'); a second pop_key(K) prints Underflow.OR (b)# (I) push every vowel of S onto Stdef push_vowels(S, St): for ch in S: if ch in "aeiouAEIOU": St.append(ch)# (II) pop and return the top elementdef pop_one(St): if len(St) == 0: print("Stack Underflow") return None else: return St.pop()# (III) show all elements from top to bottom, without removing themdef display_all(St): if len(St) == 0: print("Empty Stack") else: for i in range(len(St) - 1, -1, -1): print(St[i])Example: push_vowels("Easy Concepts", St) makes St = ['E', 'a', 'o', 'e']; display_all(St) then prints e, o, a, E (top first) and the stack is unchanged.© examsnet.com
Go to Question: