-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathExercise_1.py
More file actions
47 lines (37 loc) · 1.68 KB
/
Exercise_1.py
File metadata and controls
47 lines (37 loc) · 1.68 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
# Time Complexity : O(1)
# Space Complexity : O(N)
# Did this code successfully run on Leetcode : Ran it locally
# Any problem you faced while coding this : Deciding the max length and adding the sanity checks
# Your code here along with comments explaining your approach
class myStack:
def __init__(self):
self.maxLen = 1000 # Maximum length allowed for the stack
self.stack = [] # Array that will be used as stack
self.length = 0 # length for O(1) access to check the length
def isEmpty(self):
if self.length == 0: # If the stack is empty then the length will be zero
return True
return False
def push(self, item):
if self.length == self.maxLen: # If the length has reached the max allowed length then reject the push
return "Stack is full"
self.stack.append(item)
self.length += 1
def pop(self):
if self.length == 0: # If the stack is empty then reject the pop
return "Stack is empty!"
self.length -= 1
return self.stack.pop()
def peek(self):
if len(self.stack) == 0: # If the stack is empty reject the peek as there is no item to peek
return "Stack is empty!"
return self.stack[-1]
def size(self): # Return the size of the stack
return self.length
def show(self): # Return the entire stack
return self.stack
s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())