-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q05_sort_stack.py
50 lines (36 loc) · 1.09 KB
/
Q05_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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import unittest
class Stack:
def __init__(self):
self.stack = []
def push(self, num):
self.stack.append(num)
def pop(self):
return self.stack.pop()
def is_empty(self):
return len(self.stack) == 0
def peek(self):
if not self.stack:
return
return self.stack[-1]
def sort_stack(stack):
sorted_stack = Stack()
temp = None
while not stack.is_empty():
temp = stack.pop()
while not sorted_stack.is_empty() and sorted_stack.peek() > temp:
stack.push(sorted_stack.pop())
sorted_stack.push(temp)
return sorted_stack
class SetOfStacksTest(unittest.TestCase):
def test_sort_stack(self):
stack = Stack()
self.assertEqual(None, stack.peek())
stack.push(2)
stack.push(3)
stack.push(1)
stack.push(4)
sorted_stack = sort_stack(stack)
self.assertEqual(4, sorted_stack.pop())
self.assertEqual(3, sorted_stack.pop())
self.assertEqual(2, sorted_stack.pop())
self.assertEqual(1, sorted_stack.pop())