Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+2 votes
206k views
in Computer by (46.3k points)
closed by

Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push all numbers divisible by 5 into a stack implemented by using a list. Display the stack if it has at least one element, otherwise display appropriate error message.

3 Answers

+3 votes
by (3.7k points)
selected by
 
Best answer

Here's a Python function that implements the required functionality:

def PUSH(Arr):
    stack = []
    for num in Arr:
        if num % 5 == 0:
            stack.append(num)
    
    if stack:
        print("Stack elements are:")
        for element in stack:
            print(element)
    else:
        print("No elements divisible by 5 found.")

Here's an explanation of how this function works:

  1. First, we initialize an empty list called stack which will serve as our stack.
  2. Next, we loop through each element in the Arr list using a for loop.
  3. Inside the loop, we use the % operator to check if the current number is divisible by 5. If it is, we append it to the stack list.
  4. Once we have finished looping through all the elements in Arr, we check if stack has any elements using an if statement. If it does, we print out each element in the stack using another for loop. If it doesn't, we print an appropriate error message.
  5. The function doesn't return anything, it simply prints the stack or an error message.

You can call this function with a list of numbers as an argument, like this:

PUSH([10, 20, 25, 30, 35, 40]) 

This will output:

Stack elements are:
10
20
25
30
35
40 

If you call the function with a list that doesn't contain any numbers divisible by 5, you'll get an error message:

PUSH([1, 2, 3, 4, 6, 7]) 

This will output:

No elements divisible by 5 found.

 

+6 votes
by (49.3k points)

Using of any correct code giving the same result is also accepted.

def PUSH(Arr,value): 

s=[]

for x in range(0,len(Arr)): 

if Arr[x]%5==0: 

s.append(Arr[x]) 

if len(s)==0:

print("Empty Stack") 

else: 

print(s)

+1 vote
by (15.1k points)

A stack is an assortment of items that supports quick toward the Last-in-first-out (LIFO) semantics for embeds and erases.

def PUSH(Arr):

s=[]

for i in range(0,len(Arr)):

if Arr[i]%5==0 :

s.append(Arr[x])

if len(s)==0:

print("Empty Stack")

else:

print(s)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...