English

Write the definition of a user-defined function 'push_even(N)' which accepts a list of integers in a parameter 'N' and pushes all those integers which are even from the list 'N' - Computer Science (Python)

Advertisements
Advertisements

Question

Write the definition of a user-defined function 'push_even(N)' which accepts a list of integers in a parameter 'N' and pushes all those integers which are even from the list 'N' into a Stack named 'EvenNumbers'. Write function pop_even() to pop the topmost number from the stack and returns it. If the stack is already empty, the function should display "Empty". Write function Disp_even() to display all element of the stack without deleting them. If the stack is empty, the function should display 'None'.

For example:

If the integers input into the list 'VALUES' are:
[10, 5, 8, 3, 12]

Then the stack 'EvenNumbers' should store:
[10, 8, 12]

Code Writing

Solution

def push_even(N):
        EvenNumbers = []
        for num in N:
              if num % 2 == 0:
                  EvenNumbers.append(num)
        return EvenNumbers
VALUES = []

for i in range(5):
     VALUES.append(int(input("Enter an integer: ")))

EvenNumbers = push_even(VALUES)

def pop_even():
        if not EvenNumbers:
           print("Underflow")
       else:
           print(EvenNumbers.pop())

pop_even()
def Disp_even():
           if not EvenNumbers:
             print("None")
          else:
             print(EvenNumbers[-1])
Disp_even()
shaalaa.com
  Is there an error in this question or solution?
2024-2025 (March) Board Sample Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×