Advertisements
Advertisements
Question
Write the following user-defined functions in Python:
push_trail(N,myStack):HereNandmystackare lists, andmyStackrepresents a stack. The function should push the last 5 elements from the listNonto the stackmyStack. For example, if the listNis[1,2,3,4,5,6,7], then the functionpush_trail()Should push the elements3, 4, 5, 6, 7onto the stack. Therefore the value of stack will be[3, 4, 5, 6, 7]. Assume thatNcontains at least 5 elements.- 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 returnNone. display_all(myStack):The function should display all the elements of the stackmyStack, without deleting them. If the stack is empty, the function should display the message'Empty Stack'.
Code Writing
Advertisements
Solution
-
def push_trail(N,myStack): for i in range(-5,-1,1): myStack.append(i) -
def pop_one(myStack): if myStack =[]: print("Stack Underflow") return None else: myStack.pор() -
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?
