-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_stack.py
More file actions
61 lines (40 loc) · 1.27 KB
/
sort_stack.py
File metadata and controls
61 lines (40 loc) · 1.27 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
"""
Write a program to sort a stack in ascending order (with biggest item on top). You may use additional stacks to hold items, but you may not copy the elements into
any other data structure (like array). The stack support the following operations push, pop, peek and is_empty.
"""
def sort_stack(stack):
def peek(stack): return stack[-1]
sorted_stack = []
while stack:
number = stack.pop()
if stack and number > peek(stack):
sorted_stack.append(number)
else:
while sorted_stack and peek(sorted_stack) > number:
stack.append(sorted_stack.pop())
sorted_stack.append(number)
return sorted_stack
print(sort_stack([7, 73, 65, 23, 8, 77, 1, 24]))
def sort(A):
# Sort stack without additional space
# basically stack + recursion i.e more stacks. FUN
def insert(A, num):
if not A:
A.append(num)
else:
if num < A[-1]:
last_popped = A.pop()
insert(A, num)
insert(A, last_popped)
else:
A.append(num)
return A
if not A: return A
num = A.pop()
sort(A)
insert(A, num)
return A
# A = [4,5,3,1,2]
# A = [1,2,3,4,5]
A = [1,2,3,6,5,4]
print(sort(A))