forked from super30admin/PreCourse-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise_1.py
55 lines (34 loc) · 1.07 KB
/
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
49
50
51
52
53
54
55
# Time complexity = O(1) for push, pop and peek.
# Space complexity = O(n)
class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):
self.length = 0
self.data = {}
def isEmpty(self):
if self.length == 0:
return None
def push(self, item):
self.data[self.length] = item
self.length += 1
return self.length
def pop(self):
if self.length == 0:
return None
popped_item = self.data[self.length -1]
self.data[self.length -1] = None
self.length -= 1
return popped_item
def peek(self, index):
return self.data[index]
def size(self):
return (self.length[self.data])
def show(self):
for element in self.data:
print(element)
s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())