Q.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.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →This question asks you to build two Python functions that work with a stack: one to filter numbers with five or more digits from a list and push them onto the stack, and another to pop and display those numbers while handling the empty-stack case.
Let's think about what a stack is first. A stack is a linear data structure that follows the Last In, First Out (LIFO) principle — the last element you put in is the first one you take out. Think of a stack of plates in a cafeteria: you add a plate on top, and when you need one, you take the topmost plate. Python lists can easily act as stacks because they have built-in methods like append() to push an item onto the top, and pop() to remove and return the topmost item.
Now, the problem gives you a list called Nums containing random integers. Your first function, PushBig(), needs to examine each number in this list and decide whether it belongs in the stack BigNums. The condition is simple: the number must have five or more digits. How do you check the number of digits? The cleanest way in Python is to convert the integer to a string using str() and then check its length with len(). For example, len(str(10025)) gives 5, so 10025 qualifies. But len(str(213)) gives 3, so 213 does not qualify.
You could also check digits using repeated division by 10, but the string-length method is far more readable and perfectly acceptable in board exams. NCERT textbooks themselves use this approach in examples.
The function should iterate through Nums, and for each number that meets the condition, it should push it onto BigNums using BigNums.append(number). Notice that the order in the stack will be the same as the order in which qualifying numbers appear in the original list — because you're pushing them one by one as you encounter them. In the example, the stack ends up as [10025, 254923, 1297653, 31498, 92765], which is exactly the order they appear in Nums.
Your second function, PopBig(), does the opposite. It needs to pop numbers from the stack one by one and display them. But there's a catch: you must keep popping until the stack is empty, and when it becomes empty, you must display "Stack Empty". The natural way to do this is with a while loop that continues as long as the stack is not empty. Inside the loop, you pop the top element and print it. After the loop ends (meaning the stack is now empty), you print "Stack Empty".
The output in the example shows numbers printed one per line, in reverse order of how they were pushed. This is the LIFO behaviour: 92765 was pushed last, so it is popped first. The final line "Stack Empty" appears after all numbers have been popped. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.