Question:medium

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.

Show Hint

Use append() to push, pop() to remove the top element,
and always check length before popping to avoid errors.
Updated On: Jan 14, 2026
Show Solution

Solution and Explanation

(i) push_Clr():

def push_Clr(ClrStack, new_Clr):
    ClrStack.append(new_Clr)
    print("New color record pushed to stack.")
This function adds `new_Clr` to the end of `ClrStack` using the `append()` method and prints a confirmation.

(ii) pop_Clr():

def pop_Clr(ClrStack):
    if len(ClrStack) == 0:
        print("Underflow")
        return None
    else:
        return ClrStack.pop()
This function checks if `ClrStack` is empty. If it is, it prints "Underflow" and returns `None`. Otherwise, it removes and returns the top element.

(iii) isEmpty():

def isEmpty(ClrStack):
    if len(ClrStack) == 0:
        return True
    else:
        return False
This function returns `True` if `ClrStack` is empty (length is zero), and `False` otherwise.

Was this answer helpful?
0