forked from kkazimierska/python-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
35 lines (28 loc) · 673 Bytes
/
stack.py
File metadata and controls
35 lines (28 loc) · 673 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
if self.items:
return self.items[-1]
return None
def __len__(self):
# in python the `len` function is preferred to `size` methods
return len(self.items)
def __bool__(self):
# lets us use the stack as a conditional
return bool(self.items)
s = Stack()
print(s.is_empty())
s.push(1)
s.push('two')
s.is_empty()
s.size()
s.pop()
s.pop()
s.is_empty()