From e79c3224b0294e65c39b6d881d41761b829ea18b Mon Sep 17 00:00:00 2001 From: rishigoswamy Date: Sun, 8 Feb 2026 16:56:10 -0800 Subject: [PATCH 1/3] Add implementation for PreCourse-1 --- Exercise_1.py | 33 +++++++++++++++++++++++++++++++++ Exercise_2.py | 20 ++++++++++++++++++++ Exercise_3.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..ca55f63c1 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,20 +1,53 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 8 16:20:59 2026 + +@author: rishigoswamy +""" + class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): + self.maxSize = 1000 + self.currentSize = 0 + self.stack = [] def isEmpty(self): + return not self.stack def push(self, item): + if self.currentSize < self.maxSize: + self.stack.append(item) + self.currentSize+=1 + else: + print("Stack Overflow") + def pop(self): + if self.stack: + self.currentSize-=1 + return self.stack.pop() + else: + print("Stack Underflow") + return 0 def peek(self): + if self.stack: + return self.stack[-1] + else: + print("Stack Underflow") + return 0 def size(self): + return len(self.stack) def show(self): + for item in self.stack: + print(item) + return self.stack s = myStack() diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..9039e5080 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,3 +1,10 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 8 16:20:59 2026 + +@author: rishigoswamy +""" class Node: def __init__(self, data): @@ -6,10 +13,23 @@ def __init__(self, data): class Stack: def __init__(self): + self.head = Node(None) def push(self, data): + newNode = Node(data) + if self.head.next: + newNode.next = self.head.next + self.head.next = newNode + def pop(self): + if self.head.next: + val = self.head.next.data + self.head.next = self.head.next.next + return val + else: + return None + a_stack = Stack() while True: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..3b055d018 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,17 @@ +""" +Created on Sun Feb 8 16:20:59 2026 + +@author: rishigoswamy + +""" + 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): @@ -18,6 +27,18 @@ def append(self, data): Takes O(n) time. """ + if not self.head: + self.head = ListNode(data=data) + return + + ptr = self.head + while ptr.next: + ptr = ptr.next + + ptr.next = ListNode(data=data) + + + def find(self, key): """ Search for the first element with `data` matching @@ -25,8 +46,33 @@ def find(self, key): Takes O(n) time. """ + ptr = self.head + while ptr: + if ptr.data == key: + return ptr + ptr = ptr.next + + return None + def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + + dummy = ListNode(None, None) + dummy.next = self.head + + prev = dummy + curr = prev.next + while curr: + if curr.data == key: + prev.next = curr.next + self.head = dummy.next + return + prev = curr + curr = curr.next + + return + + From 8b5de94d3b0e2b61abc0e051dc2b970960633fdc Mon Sep 17 00:00:00 2001 From: rishigoswamy Date: Sun, 8 Feb 2026 16:59:03 -0800 Subject: [PATCH 2/3] Add implementation for PreCourse-1 --- Exercise_1.py | 12 ++++++++++++ Exercise_2.py | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/Exercise_1.py b/Exercise_1.py index ca55f63c1..e1e05a96e 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -4,6 +4,18 @@ Created on Sun Feb 8 16:20:59 2026 @author: rishigoswamy + +# Time Complexity: +# isEmpty : O(1) +# push : O(1) +# pop : O(1) +# peek : O(1) +# size : O(1) +# show : O(n) # iterates through stack + +# Space Complexity: +# O(n), where n is the number of elements in the stack (max 1000) + """ class myStack: diff --git a/Exercise_2.py b/Exercise_2.py index 9039e5080..a280c7011 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -4,6 +4,14 @@ Created on Sun Feb 8 16:20:59 2026 @author: rishigoswamy + +# Time Complexity: +# push : O(1) (insert at head) +# pop : O(1) (remove from head) +# +# Space Complexity: +# O(n), where n is the number of elements currently in the stack. + """ class Node: From 0a98b7856effe142f27acfbe6c199a24d680f47c Mon Sep 17 00:00:00 2001 From: rishigoswamy Date: Sun, 8 Feb 2026 16:59:24 -0800 Subject: [PATCH 3/3] Add implementation for PreCourse-1 --- Exercise_3.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Exercise_3.py b/Exercise_3.py index 3b055d018..d7adf7a03 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,6 +3,14 @@ @author: rishigoswamy +# Time Complexity: +# append : O(n) (traverses to the end of the list) +# find : O(n) (linear search) +# remove : O(n) (linear traversal to find the key) +# +# Space Complexity: +# O(n), where n is the number of nodes in the linked list. + """ class ListNode: