Check sibling questions
Computer Science - Class 12
Chapter 3 - Stacks

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.

Answer:

def PUSH(Arr):

stack=[]

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

if Arr[i]%5==0:

stack.append(Arr[i])

if len(stack)==0:

print("Stack is Empty")

else:

print(stack)

 

Slide26.JPG

Are ads bothering you?


Transcript

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. Answer: def PUSH(Arr): stack=[] for i in range(0,len(Arr)): if Arr[i]%5==0: stack.append(Arr[i]) if len(stack)==0: print("Stack is Empty") else: print(stack)

Davneet Singh's photo - Teacher, Engineer, Marketer

Made by

Davneet Singh

Davneet Singh is a graduate from Indian Institute of Technology, Kanpur. He has been teaching from the past 12 years. He provides courses for Maths and Science at Teachoo.