Question: Write a complete Python program which reads an integer N, creates a new Stack using the class definition below, then pushes 1, 2, ..., N
Write a complete Python program which reads an integer N, creates a new Stack using the class definition below, then pushes 1, 2, ..., N this stack in this order. Finally, repeatedly pop the stack and print the returned value, until the stack is empty.
class Stack: """Stack implementation as a list""" def __init__(self): """Create new stack""" self._items = [] def is_empty(self): """Check if the stack is empty""" return not bool(self._items) def push(self, item): """Add an item to the stack""" self._items.append(item) def pop(self): """Remove an item from the stack""" return self._items.pop() def peek(self): """Get the value of the top item in the stack""" return self._items[-1] def size(self): """Get the number of items in the stack""" return len(self._items) def __str__(self): result = '[' for elt in self._items: result = result + str(elt) + "," return result + "]"
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
