From 99ad6df52703a6eb93a29ceb53f40169e0c0151c Mon Sep 17 00:00:00 2001 From: Harika Andugula Date: Thu, 3 Sep 2026 14:44:46 -0700 Subject: [PATCH] design stacks using array solution --- Exercise_1.py | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..6ecfe9fa0 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,50 @@ + +#Time Complexity : +#Space Complexity : +#Did this code successfully run on Leetcode : +#Any problem you faced while coding this : + +#Your code here along with comments explaining your approach + +#Exercise_1 : Implement Stack using Array. class myStack: - #Please read sample.java file before starting. - #Kindly include Time and Space complexity at top of each file - def __init__(self): + #initializing the stack and top variable + def __init__(self): + self.stack = [] + self.top = -1 def isEmpty(self): - + return self.top == -1 + def push(self, item): + self.top += 1 + self.stack.append(item) def pop(self): - + if self.top == -1: + print("stack is empty") + return None + self.top -= 1 + return self.stack.pop() def peek(self): - + return self.stack[self.top] + def size(self): - + count = 0 + for i in range(self.top, -1, -1): + if self.top == -1: + count = 0 + else: + count += 1 + return count + def show(self): - + for i in range(self.top, -1, -1): + print(self.stack[i]) s = myStack() s.push('1') s.push('2') -print(s.pop()) -print(s.show()) +s.pop() +s.show()