Question: from dynamic _ array import * class StackException ( Exception ) : Custom exception to be used by Stack class DO NOT

from dynamic_array import *
class StackException(Exception):
"""
Custom exception to be used by Stack class
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
pass
class Stack:
def __init__(self):
"""
Init new stack based on Dynamic Array
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self._da = DynamicArray()
def __str__(self)-> str:
"""
Return content of stack in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
out = "STACK: "+ str(self._da.length())+" elements. ["
out +=','.join([str(self._da[i]) for i in range(self._da.length())])
return out +']'
def is_empty(self)-> bool:
"""
Return True is the stack is empty, False otherwise
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return self._da.is_empty()
def size(self)-> int:
"""
Return number of elements currently in the stack
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return self._da.length()
# -----------------------------------------------------------------------
def push(self, value: object)-> None:
"""
TODO: Write this implementation
"""
pass
def pop(self)-> object:
"""
TODO: Write this implementation
"""
pass
def top(self)-> object:
"""
TODO: Write this implementation
"""
pass
# ------------------- BASIC TESTING -----------------------------------------
if __name__=="__main__":
print("
# push example 1")
s = Stack()
print(s)
for value in [1,2,3,4,5]:
s.push(value)
print(s)
print("
# pop example 1")
s = Stack()
try:
print(s.pop())
except Exception as e:
print("Exception:", type(e))
for value in [1,2,3,4,5]:
s.push(value)
for i in range(6):
try:
print(s.pop())
except Exception as e:
print("Exception:", type(e))
print("
# top example 1")
s = Stack()
try:
s.top()
except Exception as e:
print("No elements in stack", type(e))
s.push(10)
s.push(20)
print(s)
print(s.top())
print(s.top())
print(s)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!