-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q_stacks.py
60 lines (46 loc) · 1.63 KB
/
Q_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
49
50
51
52
53
54
55
56
57
58
59
60
# Use a single list to implement 3 stacks
# Idea is to do [0, n/3) , [n/3, 2n/3), [2n/3, n)
class ThreeStack:
def __init__(self, stack_size):
self.number_stacks = 3
# fixed size list
self.custom_list = [0] * (stack_size * self.number_stacks)
# size of stacks
self.sizes = [0] * self.number_stacks
self.stack_size = stack_size
def isFull(self, stack_num):
if self.sizes[stack_num] == self.stack_size:
return True
else:
return False
def isEmpty(self, stack_num):
if self.sizes[stack_num] == 0:
return True
else:
return False
# top elements index - to use in push and pop
def indexOfTop(self, stack_num):
offset = stack_num * self.stack_size
return offset + self.sizes[stack_num]- 1
def push(self, item, stack_num):
if self.isFull(stack_num):
return "Stack is full"
else:
self.sizes[stack_num] += 1
# add to the end of the given stack
self.custom_list[self.indexOfTop(stack_num)] = item
def pop(self, stack_num):
if self.isEmpty(stack_num):
return "Stack is empty"
else:
# top element
value = self.custom_list[self.indexOfTop(stack_num)]
self.custom_list[self.indexOfTop(stack_num)] = 0
self.sizes[stack_num] -= 1
return value
def peek(self, stack_num):
if self.isEmpty(stack_num):
return "Stack is empty"
else:
value = self.custom_list[self.indexOfTop(stack_num)]
return value