-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsort_stack.py
34 lines (29 loc) · 1019 Bytes
/
sort_stack.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
from .stack import Stack
class SortedStack:
def __init__(self):
self.stack = Stack()
self._temp = Stack()
def push(self, value):
# Pop out all the smaller values and store it into temp stack
while not self.stack.isempty() and self.stack.peek() > value:
top_value = self.stack.pop()
self._temp.push(top_value)
# Push the current value
self.stack.push(value)
# Move all the values from temp stack to current stack
while not self._temp.isempty():
self.stack.push(self._temp.pop())
sorted_stack = SortedStack()
sorted_stack.push(1)
assert sorted_stack.stack.peek() == 1
sorted_stack.push(2)
assert sorted_stack.stack.peek() == 2
sorted_stack.push(10)
assert sorted_stack.stack.peek() == 10
sorted_stack.push(5)
assert sorted_stack.stack.peek() != 5
assert sorted_stack.stack.data[-2] == 5
sorted_stack.push(0)
assert sorted_stack.stack.data[0] == 0
sorted_stack.push(3)
assert sorted_stack.stack.data[3] == 3