CBSE · 083Class XII · 2024Section C3 marks

Question 30

2 April 2024 · Computer Science (083)

Consider a list named Nums which contains random integers. Write the following user defined functions in Python and perform the specified operations on a stack named BigNums. (i) PushBig() : It checks every number from the list Nums and pushes all such numbers which have 5 or more digits into the stack, BigNums. (ii) PopBig() : It pops the numbers from the stack, BigNums and displays them. The function should also display "Stack Empty" when there are no more numbers left in the stack.

For example, if the list Nums contains the following data:

python
Nums = [213, 10025, 167, 254923, 14, 1297653, 31498, 386, 92765]

Then on execution of PushBig(), the stack BigNums should store:

text
[10025, 254923, 1297653, 31498, 92765]

And on execution of PopBig(), the following output should be displayed:

text
92765
31498
1297653
254923
10025
Stack Empty

Answer

python
def PushBig(Nums, BigNums):
    for N in Nums:
        if len(str(N)) >= 5:
            BigNums.append(N)

def PopBig(BigNums):
    while BigNums:
        print(BigNums.pop())
    print("Stack Empty")

Explanation

PushBig appends every number with 5 or more digits. PopBig pops and prints until empty, then displays 'Stack Empty'.