From 66bb5ce8624c71ab40743385c51cbaaf8a5fdeb8 Mon Sep 17 00:00:00 2001 From: Prerana T H M Date: Tue, 16 Jun 2026 03:00:34 -0700 Subject: [PATCH 1/3] Completed s30 PreCourse-1 --- Exercise_1.py | 37 +++++++++++++++++++++++++------------ Exercise_2.py | 10 ++++++++++ Exercise_3.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 12 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..bab6467d2 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,20 +1,33 @@ 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 __init__(self): + self.stack = [] + + def isEmpty(self): + return len(self.stack) == 0 + + def push(self, item): + self.stack.append(item) + + def pop(self): + if self.isEmpty(): + return "Stack is empty" + else: + return self.stack.pop() + + def peek(self): + if self.isEmpty(): + return "Stack is empty" + else: + return self.stack[-1] + - def size(self): + def size(self): + return len(self.stack) - def show(self): + def show(self): + return self.stack s = myStack() diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..f75f394eb 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -6,10 +6,20 @@ def __init__(self, data): class Stack: def __init__(self): + self.top = None def push(self, data): + new_node = Node(data) + new_node.next = self.top + self.top = new_node def pop(self): + if self.top is None: + return None + + popped_node = self.top + self.top = self.top.next + return popped_node.data a_stack = Stack() while True: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..0f069d459 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,6 +3,9 @@ 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 +20,16 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + new_node = ListNode(data) + + if self.head is None: + self.head = new_node + return + + current = self.head + while current.next: + current = current.next + current.next = new_node def find(self, key): """ @@ -24,9 +37,43 @@ def find(self, key): `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. """ + if self.head is None: + return + + if self.head.data == key: + self.head = self.head.next + return + + prev = self.head + current = self.head.next + + while current: + if current.data == key: + prev.next = current.next + return + prev = current + current = current.next + +''' +Time Complexities - +append O(n) +find O(n) +remove O(n) +constructor O(1) + +''' \ No newline at end of file From c97c38cd55306b4c75f5f15796d47fd50975d98f Mon Sep 17 00:00:00 2001 From: Prerana T H M Date: Tue, 16 Jun 2026 03:22:11 -0700 Subject: [PATCH 2/3] Completed s30 PreCourse-1 --- Exercise_1.py | 12 ++++++++++++ Exercise_2.py | 9 +++++++++ Exercise_3.py | 11 ++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index bab6467d2..99eaa59c7 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -35,3 +35,15 @@ def show(self): s.push('2') print(s.pop()) print(s.show()) + +''' +Time Complexity : O(1) +push() : O(1) +pop() : O(1) +peek() : O(1) +isEmpty() : O(1) +size() : O(1) +show() : O(1) + +Space Completxity : O(n) +''' \ No newline at end of file diff --git a/Exercise_2.py b/Exercise_2.py index f75f394eb..9cba25c50 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -40,3 +40,12 @@ def pop(self): print('Popped value: ', int(popped)) elif operation == 'quit': break + +''' +Time Complexity : O(1) +Push : O(1) +Pop : O(1) +Peek : O(1) + +Space Complexity : O(n) +''' \ No newline at end of file diff --git a/Exercise_3.py b/Exercise_3.py index 0f069d459..90f0fbc58 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -70,10 +70,11 @@ def remove(self, key): current = current.next ''' -Time Complexities - -append O(n) -find O(n) -remove O(n) -constructor O(1) +Time Complexity : O(n) +append : O(n) +find : O(n) +remove : O(n) + +Space Complexity : ''' \ No newline at end of file From f7d468ab776eacdc9f93556a048c0a8e74eca271 Mon Sep 17 00:00:00 2001 From: Preranathm Date: Tue, 16 Jun 2026 03:23:37 -0700 Subject: [PATCH 3/3] Update Exercise_3.py Added space complexity --- Exercise_3.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Exercise_3.py b/Exercise_3.py index 90f0fbc58..4175897ce 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -75,6 +75,6 @@ def remove(self, key): find : O(n) remove : O(n) -Space Complexity : +Space Complexity : O(n) -''' \ No newline at end of file +'''