classstack(object):"""stack implementation using a shallow embedding"""def__init__(self):"""creates an empty stack in O(1)"""self.__stack=list()def__len__(self):"""return the number of elements in the stack in O(1)"""returnlen(self.__stack)def__str__(self):"""return the string representation of the stack in O(N)"""returnstr(self.__stack)defpush(self,x):"""adds x to the top of the stack in O(1)"""self.__stack.append(x)deftop(self):"""return the element last inserted in the stack in O(1)"""assertlen(self)>0returnself.__stack[-1]defpop(self):"""remove the element last inserted in the stack in O(1)"""assertlen(self)>0self.__stack.pop()
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.