forked from super30admin/PreCourse-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise_1.py
48 lines (37 loc) · 917 Bytes
/
Exercise_1.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
# Time complexity for :
# push operation = O(1)
# pop operation = O(1)
# peek operation = O(1)
# Space complexity = O(n)
class myStack:
def __init__(self):
self.stack = []
self.maxSize = 1000
# self.top = -1
def isEmpty(self):
return self.stack == []
def push(self, item):
self.stack.append(item)
# self.top += 1
def pop(self):
if self.isEmpty():
return "Stack is empty!"
else:
last_element = self.stack[-1] # saving last
del self.stack[-1] # removing last
# self.top = self.top - 1
return last_element
def peek(self):
if self.isEmpty():
return "Stack is empty!"
else:
return self.stack[-1]
def size(self):
return len(self.stack)
def show(self):
return self.stack
s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())