From c508169ca3b17db8b4ad4668822190467e6c388f Mon Sep 17 00:00:00 2001 From: Divya Date: Thu, 13 Nov 2025 09:29:19 -0500 Subject: [PATCH] Completed Precourse-1 --- Exercise_1.py | 62 ++++++++++++++++++++++++++++++++++++--------------- Exercise_2.py | 18 +++++++++++++++ Exercise_3.py | 48 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 18 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..76da02dbd 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,50 @@ +# Time Complexity: +# push() -> O(1) +# pop() -> O(1) +# peek() -> O(1) +# isEmpty() -> O(1) +# size() -> O(1) +# show() -> O(n) +# Space Complexity: O(n) where n is the number of elements in the stack + +# Any problem you faced while coding this : no + class myStack: - #Please read sample.java file before starting. - #Kindly include Time and Space complexity at top of each file - def __init__(self): - - def isEmpty(self): - - def push(self, item): - - def pop(self): - - - def peek(self): - - def size(self): - - def show(self): + def __init__(self): + # Initialize an empty stack using a list + self.stack = [] + + def isEmpty(self): + # Returns True if stack is empty, False otherwise + return len(self.stack) == 0 + + def push(self, item): + # Adds an item to the top of the stack + self.stack.append(item) + + def pop(self): + # Removes and returns the top item of the stack + if self.isEmpty(): + return "Stack is empty" + return self.stack.pop() + + def peek(self): + # Returns the top item of the stack without removing it + if self.isEmpty(): + return "Stack is empty" + return self.stack[-1] + + def size(self): + # Returns the number of elements in the stack + return len(self.stack) + + def show(self): + # Returns a list of all stack elements + return self.stack.copy() s = myStack() s.push('1') s.push('2') -print(s.pop()) -print(s.show()) +print(s.pop()) # 2 +print(s.show()) # ['1'] diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..29184aecf 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -6,10 +6,27 @@ def __init__(self, data): class Stack: def __init__(self): + self.head = None + def push(self, data): + new_node= Node(data) + new_node.next= self.head + self.head= new_node + + def pop(self): + if self.head is None: + return None + popped_item= self.head.data + self.head=self.head.next + return popped_item + + + + + a_stack = Stack() while True: @@ -22,6 +39,7 @@ def pop(self): operation = do[0].strip().lower() if operation == 'push': a_stack.push(int(do[1])) + elif operation == 'pop': popped = a_stack.pop() if popped is None: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..5fb0994f4 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,12 @@ + + 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): @@ -11,12 +15,25 @@ def __init__(self): Takes O(1) time. """ self.head = None + def append(self, data): """ Insert a new element at the end of the list. Takes O(n) time. """ + n_node=ListNode(data) + + if self.head ==None: + self.head=n_node + return + + cur=self.head + while cur.next!=None: + cur=cur.next + cur.next=n_node + + def find(self, key): """ @@ -24,9 +41,40 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + temp=self.head + + while temp: + + if temp.data==key: + return temp + temp=temp.next + + return None + + + + + + def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + + temp=self.head + prev=None + while temp: + if temp.data==key: + if prev is None: + self.head = temp.next + else: + prev.next=temp.next + break + prev=temp + temp=temp.next + + + +