-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathExercise_1.py
More file actions
69 lines (54 loc) · 1.46 KB
/
Exercise_1.py
File metadata and controls
69 lines (54 loc) · 1.46 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 8 16:20:59 2026
@author: rishigoswamy
# Time Complexity:
# isEmpty : O(1)
# push : O(1)
# pop : O(1)
# peek : O(1)
# size : O(1)
# show : O(n) # iterates through stack
# Space Complexity:
# O(n), where n is the number of elements in the stack (max 1000)
"""
class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):
self.maxSize = 1000
self.currentSize = 0
self.stack = []
def isEmpty(self):
return not self.stack
def push(self, item):
if self.currentSize < self.maxSize:
self.stack.append(item)
self.currentSize+=1
else:
print("Stack Overflow")
def pop(self):
if self.stack:
self.currentSize-=1
return self.stack.pop()
else:
print("Stack Underflow")
return 0
def peek(self):
if self.stack:
return self.stack[-1]
else:
print("Stack Underflow")
return 0
def size(self):
return len(self.stack)
def show(self):
for item in self.stack:
print(item)
return self.stack
s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())