English

Write the following user-defined functions in Python: push_trail(N,myStack): Here N and mystack are lists, and myStack represents a stack. The function should push the last 5 elements from the list N - Computer Science (Python)

Advertisements
Advertisements

Question

Write the following user-defined functions in Python:

  1. push_trail(N,myStack): Here N and mystack are lists, and myStack represents a stack. The function should push the last 5 elements from the list N onto the stack myStack. For example, if the list N is [1,2,3,4,5,6,7], then the function push_trail() Should push the elements 3, 4, 5, 6, 7 onto the stack. Therefore the value of stack will be [3, 4, 5, 6, 7]. Assume that N contains at least 5 elements.
  2. pop_one(myStack): The function should pop an element from the stack myStack, and return this element. If the stack is empty, then the function should display the message ‘Stack Underflow', and return None.
  3. display_all(myStack): The function should display all the elements of the stack myStack, without deleting them. If the stack is empty, the function should display the message 'Empty Stack'.
Code Writing
Advertisements

Solution

  1. def push_trail(N,myStack):
        for i in range(-5,-1,1):
           myStack.append(i)
  2. def pop_one(myStack):
    if myStack =[]:
         print("Stack Underflow")
         return None
    else:
        myStack.pор()
  3. def display_all(myStack): 
        if myStack=[]: 
           print("Empty Stack")
       else:
           print(myStack[: :-1],sep="→")
shaalaa.com
  Is there an error in this question or solution?
2024-2025 (March) Set 4

APPEARS IN

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×