From e29ea2a871efc6fee9b67d5fafcd6c980f2e15a1 Mon Sep 17 00:00:00 2001 From: RITIKA CHAUBE Date: Tue, 2 Jun 2026 17:22:03 -0400 Subject: [PATCH] Done Precourse 1 --- Exercise_1.py | 42 +++++++++++++++++++++++++++++++++++++++--- Exercise_2.py | 16 +++++++++++++++- Exercise_3.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..d3fbf7d5a 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,21 +1,57 @@ class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file + + #Time and Space Complexity + # Overall Space Complexity = O(N) where N is the MAX_SIZE of 1000 + # __init__ function has the time and space complexity of O(N) + # isEmpty function has the time and space complexity of O(1) + # push function has the time and space complexity of O(1) + # pop function has the time and space complexity of O(1) + # peek function has the time and space complexity of O(1) + # size function has the time and space complexity of O(1) + # show function has the time and space complexity of O(M) where M dentoes the number of elements in the stack + + def __init__(self): + self.MAX_SIZE=1000 + self.array=[None]*self.MAX_SIZE + self.top=-1 def isEmpty(self): + if self.top==-1: + return True + return False def push(self, item): + if self.top-1: + value=self.array[self.top] + self.top-=1 + return value + else: + print("Stack Underflow") + return False + def peek(self): + if self.top==-1: + return False + else: + return self.array[self.top] def size(self): + return self.top+1 def show(self): - + return self.array[0: self.top+1][::-1] + s = myStack() s.push('1') diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..e84df0c59 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,8 @@ - +# Time and Space Complexity +# Overall Space Complexity is O(N) where N is the number of elements in stack +# __init__ function has time and space complexity of O(1) +# push function has time and space complexity of O(1) +# pop function has time and space complexity of O(1) class Node: def __init__(self, data): self.data = data @@ -6,10 +10,20 @@ def __init__(self, data): class Stack: def __init__(self): + self.top=None def push(self, data): + newNode=Node(data) + newNode.next=self.top + self.top=newNode def pop(self): + if self.top==None: + return None + else: + value=self.top.data + self.top=self.top.next + return value a_stack = Stack() while True: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..30b18526a 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,16 @@ +# Time and Space Complexity +# Overall Space Complexity is O(N) where N is total number of elements in the list +# append function takes O(N) time complexity and O(1) space complexity +# find function takes O(N) time complexity and O(1) space complexity +# remove function takes O(N) time complexity and O(1) space complexity + 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 +25,14 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + newNode=ListNode(data) + if self.head==None: + self.head=newNode + else: + a=self.head + while a.next!=None: + a=a.next + a.next=newNode def find(self, key): """ @@ -24,9 +40,35 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + a=self.head + while a!=None: + if a.data==key: + return a + a=a.next + return None def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + a=self.head + if self.head==None: + return None + if self.head.data==key: + self.head=self.head.next + return None + while a.next!=None: + if a.next.data==key: + a.next=a.next.next + return None + a=a.next + +ll = SinglyLinkedList() + +ll.append(10) +ll.append(20) +ll.append(30) +found = ll.find(20) +print(found.data) +ll.remove(20) \ No newline at end of file