-
Notifications
You must be signed in to change notification settings - Fork 1
/
two-stacks2.py
96 lines (73 loc) · 2.14 KB
/
two-stacks2.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
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
"""
Implement two stacks in one array
Given a fixed number of elements, add elements of the first stack to
the front of the array but add elements of the second stack to the back
Pushing new elements to indexed positions for constant time operations
>>> ts = TwoStacks(5)
>>> ts.push1(1)
>>> ts.push1(2)
>>> ts.push1(3)
>>> print(ts)
array([1, 2, 3, 0, 0])
>>> ts.push2(4)
>>> ts.push2(5)
>>> print(ts)
array([1, 2, 3, 5, 4])
>>> ts.push2(6)
'Stack Overflow'
>>> print(ts.pop1())
3
>>> print(ts.pop2())
5
>>> print(ts.pop2())
4
>>> print(ts.pop2())
Stack Underflow
>>> print(ts)
array([1, 2, 3, 5, 4])
"""
import numpy as np
class TwoStacks():
def __init__(self, n):
self.size = n
self.arr = np.zeros([n], dtype=int)
self.top1 = -1 # first stack will start at the front of the array
self.top2 = self.size # second stack will start at the back af the array
def _has_empty_space(self):
# Check if there is a space for new element
return self.top1 < self.top2 - 1
def push1(self, value):
# First stack starts at the front of the array, so move top pointer forwards
if self._has_empty_space():
self.top1 += 1
self.arr[self.top1] = value
else:
return 'Stack Overflow'
def push2(self, value):
# Second stack starts at the end of the array, so move top pointer backwards
if self._has_empty_space():
self.top2 -= 1
self.arr[self.top2] = value
else:
return 'Stack Overflow'
def pop1(self):
# Move top pointer towards the front of array
if self.top1 >= 0:
front = self.arr[self.top1]
self.top1 -= 1
return front
else:
return 'Stack Underflow'
def pop2(self):
# Move top pointer backwards to end of array
if self.top2 < self.size:
back = self.arr[self.top2]
self.top2 += 1
return back
else:
return 'Stack Underflow'
def __repr__(self):
return repr(self.arr)
if __name__ == '__main__':
import doctest
doctest.testmod()