From eae5392e5f241dd0fd0ac47c1e8bfbfa4c18ba28 Mon Sep 17 00:00:00 2001 From: Purva Date: Mon, 10 Nov 2025 01:04:33 -0600 Subject: [PATCH] done precourse 1 --- Exercise_1.py | 18 ++++++++++++++++++ Exercise_2.py | 23 ++++++++++++++++++++--- Exercise_3.py | 23 +++++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..3cdd4e677 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,20 +1,37 @@ +#TC = isempty, pusing, popping, peeking, size would be O(1) and the showing/printing the stack would be o(n) +#Sc = o(n) since we have to store all elements even if we pop then, so number of elements pushed +# solved minstack on leetcode, in the append function, we append the minimum value of the two (one we currently are processing VS one that is already on top of min stack) +#if min stack is already empty, add the current one to it +#similarly, when we want to pop an element, we pop it from the minstack as well, since we build both the arrays simultaneously + + class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): + self.stack = [] #created an array to store def isEmpty(self): + return len(self.stack) == 0 #if len is 0, then it is empty def push(self, item): + self.stack.append(item) #just append + def pop(self): + if self.isEmpty(): + return "Empty stack" #check if its already empyt before popping + return self.stack.pop() def peek(self): + return self.stack[-1] #return top element def size(self): + return len(self.stack) #return len def show(self): + return self.stack s = myStack() @@ -22,3 +39,4 @@ def show(self): s.push('2') print(s.pop()) print(s.show()) + diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..09e031d14 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,19 +1,36 @@ +#TC o(1) for push pop elements +#SC o(n) for storing all the elements pushed +# + + class Node: def __init__(self, data): self.data = data - self.next = None + self.next = None class Stack: def __init__(self): + self.top = None #set top to none initially + def push(self, data): - + newNode = Node(data) #create a new node that will tka ein your val + newNode.next = self.top #assing the next of this new node as top val (for easy popping) + self.top = newNode #the new top is the latest node added + def pop(self): + if self.top is None: + return None #if already empty, then return none + poppedval = self.top.data #create a variable to store popped val + self.top = self.top.next #shift the top to next of the current top element + return poppedval + + a_stack = Stack() while True: - #Give input as string if getting an EOF error. Give input like "push 10" or "pop" + #Give input as string if getting an EOF error. Give inppoppoput like "push 10" or "pop" print('push ') print('pop') print('quit') diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..82cc95263 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,6 +3,8 @@ class ListNode: A node in a singly-linked list. """ def __init__(self, data=None, next=None): + self.data = data + self.next = next class SinglyLinkedList: def __init__(self): @@ -17,6 +19,12 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + newNode = ListNode(data) + curr = self.head + while curr.next: + curr = curr.next + curr.next = newNode + def find(self, key): """ @@ -24,9 +32,24 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + curr = self.head + while curr: + if curr.data == key: + return curr + curr = curr.next #check further elements + return None #if not found yet + def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + prev = None + curr = self.head + while curr: + if curr.data == key: + prev.next = curr.next #bypass the current one + return + prev = curr + curr = curr.next