From e5439714fddc8fe126952aa3ce5ddcf40e6b75b9 Mon Sep 17 00:00:00 2001 From: allurkarsneha Date: Mon, 27 Jul 2026 19:14:38 -0500 Subject: [PATCH] Complete PreCourse-1 exercises in Python --- Exercise_1.py | 24 +++++++++++++- Exercise_2.py | 17 +++++++++- Exercise_3.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 124 insertions(+), 4 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..4975bc093 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,20 +1,42 @@ +# Time Complexity: +# isEmpty: O(1) +# push: O(1) amortized +# pop: O(1) +# peek: O(1) +# size: O(1) +# show: O(n) + +# Space Complexity: O(n) + class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): + self.items = [] def isEmpty(self): + return len(self.items) == 0 def push(self, item): + self.items.append(item) def pop(self): - + if not self.isEmpty(): + return self.items.pop() + else: + return None def peek(self): + if not self.isEmpty(): + return self.items[-1] + else: + return None def size(self): + return len(self.items) def show(self): + return self.items s = myStack() diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..53d1b29f9 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,3 +1,8 @@ +# Time Complexity: +# push: O(1) +# pop: O(1) + +# Space Complexity: O(n) class Node: def __init__(self, data): @@ -6,11 +11,21 @@ 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 + else: + popped = self.head.data + self.head = self.head.next + return popped + a_stack = Stack() while True: #Give input as string if getting an EOF error. Give input like "push 10" or "pop" diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..0264150c1 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,19 @@ +# Time Complexity: +# append: O(n) +# find: O(n) +# remove: O(n) + +# Space Complexity: +# O(n) for storing n nodes +# O(1) extra space for each operation + 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,16 +28,88 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ - + new_node = ListNode(data) + if not self.head: + self.head = new_node + else: + current = self.head + while current.next: + current = current.next + current.next = new_node + def find(self, key): """ Search for the first element with `data` matching `key`. Return the element or `None` if not found. Takes O(n) time. """ - + current = self.head + while current: + if current.data == key: + return current + current = current.next + return None + def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + current = self.head + previous = None + while current: + if current.data == key: + if previous: + previous.next = current.next + else: + self.head = current.next + current.next = None + return current + previous = current + current = current.next + return None + +if __name__ == "__main__": + linked_list = SinglyLinkedList() + + print("Empty list tests:") + print(linked_list.find(10)) + print(linked_list.remove(10)) + linked_list.append(10) + linked_list.append(20) + linked_list.append(30) + linked_list.append(40) + + print("\nFind tests:") + + found = linked_list.find(20) + print(found.data if found else None) + + found = linked_list.find(40) + print(found.data if found else None) + + found = linked_list.find(99) + print(found.data if found else None) + + linked_list.remove(10) + print("\nAfter removing head 10:") + print(linked_list.head.data) + + linked_list.remove(30) + print("\nAfter removing middle value 30:") + print(linked_list.find(30)) + + linked_list.remove(40) + print("\nAfter removing tail 40:") + print(linked_list.find(40)) + + print("\nRemaining head:") + print(linked_list.head.data) + + linked_list.remove(99) + print("\nAfter trying to remove 99:") + print(linked_list.head.data) + + linked_list.remove(20) + print("\nAfter removing final node:") + print(linked_list.head) \ No newline at end of file