मराठी

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 - Computer Science (Python)

Advertisements
Advertisements

प्रश्न

  • 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 Push 3_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".

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

कोड लेखन

उत्तर

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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2021-2022 (April) Set 4
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×