forked from lanqiao-courses/python-100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path020-set_of_stacks.py
48 lines (36 loc) · 1.31 KB
/
020-set_of_stacks.py
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
from stack import Stack
class StackWithCapacity(Stack):
def __init__(self, top=None, capacity=10):
super(StackWithCapacity, self).__init__(top)
self.capacity = capacity
self.num_items = 0
def push(self, data):
if self.is_full():
raise Exception('Stack full')
super(StackWithCapacity, self).push(data)
self.num_items += 1
def pop(self):
self.num_items -= 1
return super(StackWithCapacity, self).pop()
def is_full(self):
return self.num_items == self.capacity
def is_empty(self):
return self.num_items == 0
class SetOfStacks(object):
def __init__(self, indiv_stack_capacity):
self.indiv_stack_capacity = indiv_stack_capacity
self.stacks = []
self.last_stack = None
def push(self, data):
if self.last_stack is None or self.last_stack.is_full():
self.last_stack = StackWithCapacity(None, self.indiv_stack_capacity)
self.stacks.append(self.last_stack)
self.last_stack.push(data)
def pop(self):
if self.last_stack is None:
return None
data = self.last_stack.pop()
if self.last_stack.is_empty():
self.stacks.pop()
self.last_stack = self.stacks[-1] if self.stacks else None
return data