forked from careercup/CtCI-6th-Edition-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33StackOfPlates.py
More file actions
123 lines (103 loc) · 2.96 KB
/
33StackOfPlates.py
File metadata and controls
123 lines (103 loc) · 2.96 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
## 3.3 Stack of Plates
import unittest
class Node(object):
def __init__(self, value):
self.value = value
self.above = None
self.below = None
class Stack(object):
def __init__(self, capacity):
self.capacity = capacity
self.size = 0
self.top = None
self.bottom = None
def is_full(self):
return self.size == self.capacity
def is_empty(self):
return self.size == 0
def join(self, above, below):
if below:
below.above = above
if above:
above.below = below
def push(self, v):
if self.size >= self.capacity:
return False
self.size += 1
n = Node(v)
if self.size == 1:
self.bottom = n
self.join(n, self.top)
self.top = n
return True
def pop(self):
if not self.top:
return None
t = self.top
self.top = self.top.below
self.size -= 1
return t.value
def remove_bottom(self):
b = self.bottom
self.bottom = self.bottom.above
if self.bottom:
self.bottom.below = None
self.size -= 1
return b.value
class SetOfStacks(object):
def __init__(self, capacity):
self.capacity = capacity
self.stacks = []
def get_last_stack(self):
if not self.stacks:
return None
return self.stacks[-1]
def is_empty(self):
last = self.get_last_stack()
return not last or last.is_empty()
def push(self, v):
last = self.get_last_stack()
if last and not last.is_full():
last.push(v)
else:
stack = Stack(self.capacity)
stack.push(v)
self.stacks.append(stack)
def pop(self):
last = self.get_last_stack()
if not last:
return None
v = last.pop()
if last.size == 0:
del self.stacks[-1]
return v
def pop_at(self, index):
return self.left_shift(index, True)
def left_shift(self, index, remove_top):
stack = self.stacks[index]
removed_item = stack.pop() if remove_top else stack.remove_bottom()
if stack.is_empty():
del self.stacks[index]
elif len(self.stacks) > index + 1:
v = self.left_shift(index + 1, False)
stack.push(v)
return removed_item
class Tests(unittest.TestCase):
def test_stacks(self):
stacks = SetOfStacks(5)
for i in range(35):
stacks.push(i)
lst = []
for _ in range(35):
lst.append(stacks.pop())
self.assertEqual(lst, list(reversed(range(35))))
def test_pop_at(self):
stacks = SetOfStacks(5)
for i in range(35):
stacks.push(i)
lst = []
for _ in range(31):
lst.append(stacks.pop_at(0))
self.assertEqual(lst, list(range(4, 35)))
if __name__ == '__main__':
unittest.main()