forked from super30admin/PreCourse-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_1.py
More file actions
47 lines (37 loc) · 1.26 KB
/
Exercise_1.py
File metadata and controls
47 lines (37 loc) · 1.26 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(1)
#Did this code successfully run on Leetcode : Question not avaliable in Leetcode
#Any problem you faced while coding this : None
class myStack:
def __init__(self): #Initialization of an list as Stack
self.Stack=[] #time---O(1) space--O(1)
def isEmpty(self):
if len(self.Stack): #time---O(1)
return False
return True
def push(self, item):
self.Stack.append(item) #time---O(1)
def pop(self):
if s.isEmpty(): #time---O(1)
return 'Stack is Empty'
else:
ele=self.Stack[-1] #time---O(1) Space--O(1)
self.Stack=self.Stack[0:len(self.Stack)-1]
return ele
def peek(self): #time-O(1)
if s.isEmpty():
return 'Stack is Empty'
else:
return self.Stack[len(self.Stack)-1]
def size(self): #time-O(1)
if s.isEmpty():
return 0
else:
return len(self.Stack)
def show(self): #time-O(1)
print(self.Stack)
s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())