© examsnet.com
Question : 5 of 12
Marks: +1, -0
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 :
Nums = [213,10025,167,254923,14,1297653,31498,386,92765]
Then on execution of PushBig(), the stack BigNums should store :
[10025, 254923, 1297653, 31498, 92765]
And on execution of PopBig(), the following output should be displayed :
92765
31498
1297653
254923
10025
Stack Empty
[CBSE 2024 Set 4]
Solution:  
Nums = [213, 10025, 167, 254923, 14, 1297653, 31498, 386, 92765]
BigNums = [] # the stack
def PushBig():
for N in Nums:
if len(str(abs(N))) >= 5: # 5 or more digits
BigNums.append(N) # push
def PopBig():
while len(BigNums) > 0: # stack not empty
print(BigNums.pop()) # pop the top and display it
print("Stack Empty")
PushBig()
PopBig()
Explanation:
PushBig(): the number of digits is the length of the number written as a string (str(10025) has 5 characters). The stack becomes [10025, 254923, 1297653, 31498, 92765], with 92765 on top.
PopBig(): pop() removes the last element (the top), so the numbers come out in reverse order (LIFO): 92765, 31498, 1297653, 254923, 10025. When the stack becomes empty the loop ends and "Stack Empty" is displayed.
© examsnet.com
Advertisements
Go to Question: