mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
53 lines
1.1 KiB
Python
53 lines
1.1 KiB
Python
#!/usr/bin/python
|
|
|
|
__author__ = "Mari Wahl"
|
|
__email__ = "marina.w4hl@gmail.com"
|
|
|
|
|
|
''' define the stack class '''
|
|
|
|
|
|
class Stack(object):
|
|
def __init__(self):
|
|
self.items = []
|
|
|
|
def isEmpty(self):
|
|
return not bool(self.items)
|
|
|
|
def push(self, value):
|
|
self.items.append(value)
|
|
|
|
def pop(self):
|
|
value = self.items.pop()
|
|
if value:
|
|
return value
|
|
else:
|
|
print("Stack is empty.")
|
|
|
|
def size(self):
|
|
return len(self.items)
|
|
|
|
def peek(self):
|
|
if self.items:
|
|
return self.items[-1]
|
|
else:
|
|
print('Stack is empty.')
|
|
|
|
def __repr__(self):
|
|
return '{}'.format(self.items)
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
stack = Stack()
|
|
print("Is the stack empty? ", stack.isEmpty())
|
|
print("Adding 0 to 10 in the stack...")
|
|
for i in range(10):
|
|
stack.push(i)
|
|
print("Stack size: ", stack.size())
|
|
print("Stack peek : ", stack.peek())
|
|
print("Pop...", stack.pop())
|
|
print("Stack peek: ", stack.peek())
|
|
print("Is the stack empty? ", stack.isEmpty())
|
|
print(stack) |