Test Index

CBSE Class 12 Computer Science 2025 Paper Set 4 Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 30 of 37
Marks: +1, -0
(a) A stack, named ClrStack, contains records of some colors. Each record is represented as a tuple containing four elements – ColorName, RED, GREEN, BLUE. ColorName is a string, and RED, GREEN, BLUE are integers. For example, a record in the stack may be ('Yellow', 237, 250, 68)
Write the following user-defined functions in Python to perform the specified operations on ClrStack:
(i) push_Clr(ClrStack, new_Clr): This function takes the stack ClrStack and a new record new_Clr as arguments and pushes this new record onto the stack.
(ii) pop_Clr(ClrStack): 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(ClrStack): 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_trail(N,myStack): Here N and mystack are lists, and myStack represents a stack. The function should push the last 5 elements from the list N onto the stack myStack. For example, if the list N is [1,2,3,4,5,6,7], then the function push_trail() should push the elements 3,4,5,6,7 onto the stack. Therefore the value of stack will be [3,4,5,6,7].
Assume that N contains at least 5 elements.
(ii) pop_one(myStack): The function should pop an element from the stack myStack, and return this element. If the stack is empty, then the function should display the message 'Stack Underflow', and return None.
(iii) display_all(myStack): The function should display all the elements of the stack myStack, 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 both push and pop happen at the same end (the end of the list is the top): append() pushes, pop() removes the top element (LIFO).
(a)
def push_Clr(ClrStack, new_Clr):
ClrStack.append(new_Clr) # push on top
def pop_Clr(ClrStack):
if isEmpty(ClrStack):
print("Underflow") # nothing to pop
else:
return ClrStack.pop() # remove and return top record
def isEmpty(ClrStack):
if len(ClrStack) == 0:
return True
else:
return False
Check: after push_Clr(S, ('Yellow', 237, 250, 68)) and push_Clr(S, ('Red', 255, 0, 0)), pop_Clr(S) returns ('Red', 255, 0, 0) (last in, first out). Popping an empty stack prints Underflow, and isEmpty([]) returns True.
OR (b)
def push_trail(N, myStack):
for x in N[-5:]: # last 5 elements of N
myStack.append(x)
def pop_one(myStack):
if len(myStack) == 0:
print('Stack Underflow')
return None
return myStack.pop() # remove and return top
def display_all(myStack):
if len(myStack) == 0:
print('Empty Stack')
else:
for i in range(len(myStack) - 1, -1, -1): # top to bottom
print(myStack[i])
Check: with N = [1,2,3,4,5,6,7], N[-5:] is [3, 4, 5, 6, 7], so the stack becomes [3, 4, 5, 6, 7] with 7 on top. display_all() then prints 7, 6, 5, 4, 3 (top first) without removing anything, and pop_one() returns 7.
© examsnet.com
Advertisements
Go to Question: