-
Notifications
You must be signed in to change notification settings - Fork 0
/
Implement_stack_using_array.py
44 lines (40 loc) · 1.25 KB
/
Implement_stack_using_array.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
"""
Your Task:
You are required to complete two methods push() and pop().
The push() method takes one argument, an integer 'x' to be pushed into the stack
and pop() which returns an integer present at the top and popped out from the stack.
If the stack is empty then return -1 from the pop() method.
Expected Time Complexity : O(1) for both push() and pop().
Expected Auixilliary Space : O(1) for both push() and pop().
Constraints:
1 <= Q <= 100
1 <= x <= 100
"""
# without considering the constraint for array size
class MyStack:
def __init__(self):
self.arr = []
def push(self, data):
self.arr.append(data)
def pop(self):
if self.arr == None:
return -1
data = self.arr[-1]
return data
# with regard of constraint of array size <- accepted by Geek for Geeks
class MyStack:
def __init__(self):
self.array_size = 100
self.arr = [0] * self.array_size
self.top = -1
def push(self, data):
if self.top == self.array_size - 1:
self.arr += [0] * self.array_size
self.top += 1
self.arr[self.top] = data
def pop(self):
data = -1
if self.top > -1:
data = self.arr[self.top]
self.top -= 1
return data