Advertisements
Advertisements
Question
- Write the definition of a user defined function
Push3_5 (N)
which accepts a list of integers in a parameterN
andpushes
all those integers which aredivisible by 3 or divisible by 5
from the listN
into a list namedOnly3_5
. - Write a program in Python to input 5 integers into a list named
NUM
.
The program should then use the functionPush 3_5()
to create the stack of the listOnly3_5
. Thereafterpop
each integer from the listOnly3_5
and display the popped value. When the list is empty, display the message"StackEmpty"
.
For example:
If the integers input into the list NUM
are:
[10, 6, 14, 18, 30]
Then the stack Only3_5
should store
[10, 6, 18, 30]
And the output should be displayed as
30 18 6 10 StackEmpty
Code Writing
Solution
Definition of Push3_5()
function:
def Push3_5(N):
for i in N:
if i%3==0 or i%5==0:
only3_5.append(i)
top=len(Only3_5)−1
Full program code:
Only3_5=[]
Num=[]
top=None
def isEmpty():
if Only3_5==[]:
reture True
else:
return False
def Pop():
if isEmpty():
print("EmptyStack")
else:
s=Only3_5.pop()
print("deleted element is :−",s)
if len(Only3_5)==0:
top=None
else:
top=len(Only3_5)−1
def Push3_5(N):
for i in N:
if i %3==0 or i %5==0:
Only3_5.append(i)
top=len(Only3_5)−1
def display():
if isEmpty ():
print("EmptyStack")
else:
top=len(Only3_5)−1
print(Only3_5[top])
for i in range(top−1, −1, −1):
print(Only3_5[i])
#___main___
for i in range(5):
Num+=[int(input("Enter the numbers"))]
print(Num)
Push3_5(Num)
display()
for i in range(len(Only3_5)+1):
Pop()
Output:
shaalaa.com
Is there an error in this question or solution?