
Given main.py, complete the Stack class in Stack.py by writing the push() and pop() methods. The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program.
Output "Can't push, stack is full" when push() is called on a full stack. Output "Can't pop, stack is empty" when pop() is called on an empty stack.
Ex. If the input is:
Push 1
Push 2
Push 3
Push 4
Push 5
Pop -1
Code given
main.py(read-only)
from Stack import Stack
if __name__ == "__main__":
stack = Stack(5)
input_line = input()
while input_line != '-1':
split_input = input_line.split(' ')
command = split_input[0]
if command == 'Push':
number_to_push = int(split_input[1])
stack.push(number_to_push)
print('Stack contents (top to bottom):')
stack.print_stack()
elif command == "Pop":
stack.pop()
print('Stack contents (top to bottom):')
stack.print_stack()
input_line = input()
Stack.py (needs edit)
class Stack:
def __init__(self, allocation_size):
self.array = [None] * allocation_size
self.top_index = -1
# TODO: Write a method to push a value into the stack
# TODO: Write a method to pop the top value from the stack
def print_stack(self):
i = self.top_index
while i >= 0:
print(self.array[i])
i = i - 1
print()

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images

- If the elements “A”, “B”, “C” and “D” are placed in a stack and are removed one at a time, in what order will they be removed?arrow_forwardexamine a very simple task: reversing a word. When you run the program, it asks you to type in a word. When you press Enter, it displays the word with the letters in reverse order. A stack is used to reverse the letters. First the characters are extracted one by one from the input string and pushed onto the stack. Then they’re popped off the stack and displayed. Because of its last-in-first-out characteristic, the stack reverses the order of the characters.arrow_forwardCreate a programme that sorts a stack so that the smallest elements appear on top. You can use a second temporary stack, but you can't transfer the components into another data structure (such an array). The following operations are supported by the stack: push, pop, peek, and is Empty.arrow_forward
- I have implemented a stack using arrays. The array is 5 elements long and is called examstack. The contents are: examstack[0] = 36 examstack[1] = 49 examstack[2] = 7 examstack[3] = 67 examstack[4] = 9 If I did a Pop what would be returned? 9 or 36 09 07 O 67 O 9 or 67 36 or 49 O 36 49arrow_forwardCreate one program that lets the user enter multiple digits in base 10 and the program will display it in all basses From base (2) to Base (36).arrow_forwardWhat is the operation that adds items to a stack? Get Set Pop Pusharrow_forward
- Which variable is appropriate to track the insertion position to a stack?arrow_forwardA pep9 call will push the return address onto the stack True or False?arrow_forwardCreate two function using java for separate tasks: >Delete the middle element of a stack > Create a separate function that takes an arraylist of integers and put all the odds before the evens int he order they occurarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





