-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabcLab.py
More file actions
53 lines (44 loc) · 1.19 KB
/
abcLab.py
File metadata and controls
53 lines (44 loc) · 1.19 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import abc
class StackInterface(abc.ABC):
"""
>>> a = Stack()
>>> a.push(1)
>>> a.push(2)
>>> a.peek()
2
>>> a.pop()
2
>>> a.pop()
1
>>> a.peek()
>>> a.pop()
"""
@abc.abstractmethod
def push(self, value):
"Push value onto the stack. Return None"
@abc.abstractmethod
def pop(self):
"Pop value from Stack. Return None if nothingon stack"
@abc.abstractmethod
def peek(self):
"Peeak at top of stack. Return None if empty"
class Stack(StackInterface):
def __init__(self,values=None):
self._storage = []
if values:
for v in values: self.push(v)
def push(self, value):
"Push value onto the stack. Return None"
self._storage.append(value)
return None
def pop(self):
"Pop value from Stack. Return None if nothingon stack"
if len(self._storage) == 0: return None
else:
v = self._storage[-1]
self._storage = self._storage[:-1]
return v
def peek(self):
"Peeak at top of stack. Return None if empty"
if len(self._storage) == 0: return None
else: return self._storage[-1]