Question 08(or)
7 June 2022 · Computer Science (083)
Write the definition of a user defined function Push3_5(N) which accepts a list of integers in a parameter N and pushes all those integers which are divisible by 3 or divisible by 5 from the list N into a list named Only3_5.
Write a program in Python to input 5 integers into a list named NUM. The program should then use the function Push3_5() to create the stack of the list Only3_5. Thereafter pop each integer from the list Only3_5 and display the popped value. When the list is empty, display the message 'StackEmpty'.
Answer
def Push3_5(N):
for i in N:
if i%3 == 0 or i%5 == 0:
Only3_5.append(i)
NUM = []
Only3_5 = []
for i in range(5):
NUM.append(int(input('Enter an Integer: ')))
Push3_5(NUM)
while Only3_5:
print(Only3_5.pop(), end=' ')
else:
print('StackEmpty')Explanation
The function checks each integer for divisibility by 3 or 5 and pushes matching values onto the stack. The while-else then pops and displays each value, printing 'StackEmpty' when the stack is exhausted.