From 3571bdc5d8120740c9a2837f505b79d811b9092d Mon Sep 17 00:00:00 2001 From: rameshavinash94 <87649563+rameshavinash94@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:46:03 -0600 Subject: [PATCH 1/3] PreCourse-1: completed exercises 1,2,3. --- Exercise_1.py | 51 +++++++++++++++++++++---------- Exercise_2.py | 27 +++++++++++++---- Exercise_3.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 140 insertions(+), 21 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..aec8ee349 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,45 @@ +''' +Overall: +Time Complexity O(1) +Space Complexity O(n) +// Did this code successfully run on Leetcode : Not sure if it exist in LeetCode +// Any problem you faced while coding this : No, as per as i can think. +Optimizations: +can we optimized by introducing a maxsize attribute to avoid overflow. +''' 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 __init__(self): + self.stk = [] + self.count = 0 + + def isEmpty(self): + return self.count==0 + + def push(self, item): + self.stk.append(item) + self.count+=1 + + def pop(self): + if self.isEmpty(): + raise Exception("Cannot pop from an empty stack") + self.count-=1 + return self.stk.pop() - - def peek(self): - - def size(self): - - def show(self): + def peek(self): + if not self.isEmpty(): + return self.stk[-1] + raise Exception("Cannot peek from an empty stack") + + def size(self): + return self.count + + def show(self): + return self.stk - s = myStack() s.push('1') s.push('2') print(s.pop()) -print(s.show()) +print(s.show()) \ No newline at end of file diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..3465092ab 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,12 @@ - +''' +Overall: +Time Complexity O(1) +Space Complexity O(n) +// Did this code successfully run on Leetcode : Not sure if it exist in LeetCode +// Any problem you faced while coding this : No. +Approach: +Have considered adding elements at the head of the linked list to optimized the inserts and removal using a single pointer. +''' class Node: def __init__(self, data): self.data = data @@ -6,11 +14,20 @@ def __init__(self, data): class Stack: def __init__(self): - - def push(self, data): - + self.head = None + + def push(self, data): + newNode = Node(data) + prevNode = self.head + self.head, self.head.next = newNode, prevNode # adding node to the head of the linked of the linked list + def pop(self): - + if self.head == None: + return None + poppedVal = self.head.data + self.head = self.head.next + return poppedVal + 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..0e561edf3 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,9 +1,23 @@ +''' +Overall: +Time Complexity O(n) [append and find are 0(n)] +Space Complexity O(n) +// Did this code successfully run on Leetcode : Not sure if it exist in LeetCode +// Any problem you faced while coding this : No. +Approach: +straight forward linked list implementation. +have created a _traverseLinkedList funcitont to avoid code duplications. +Optimizations: +we can optimized the appends by including a new tail pointer. +''' 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 +31,37 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + newNode = ListNode(data) + if self.head: + tmpNode = self.head + while tmpNode.next: + tmpNode = tmpNode.next + tmpNode.next = newNode + else: + self.head = newNode + + def _traverseLinkedList(self, findVal): + """ + Traverse the linked list to find a node with the given data. + Args: + findVal: The data to search for in the linked list. + Returns: + A tuple containing the previous node and the current node. + If the target_data is found in the head node, it returns (None, head). + If the target_data is not found, it returns (None, None). + """ + tmpNode = self.head + + if tmpNode.data == findVal: + return None, tmpNode + + while tmpNode: + if tmpNode.data == findVal: + return prev, tmpNode + prev = tmpNode + tmpNode = tmpNode.next + + return None, None def find(self, key): """ @@ -24,9 +69,45 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + if not self.head: + return + prev, curr = self._traverseLinkedList(key) + if curr: + print( f"element {curr.data} found @ {curr}") + return curr + print('element {curr.data} not found') def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + prev, curr = self._traverseLinkedList(key) + if curr!=None: + if prev: + prev.next = curr.next + else: + self.head = curr.next + else: + print('element not found to remove') + + def printLinkedList(self): + tmpNode = self.head + while tmpNode: + print(tmpNode.data,end='->') + tmpNode = tmpNode.next + print(None) + +# linked list operations +newList = SinglyLinkedList() +newList.append(10) +newList.append(20) +newList.append(30) +newList.remove(20) +newList.append(40) +newList.append(30) +newList.append(15) +newList.find(30) +newList.find(90) +newList.remove(10) +newList.printLinkedList() \ No newline at end of file From e4ac5f6d2eba1a265abc66e8b90c7ef1dd72febc Mon Sep 17 00:00:00 2001 From: rameshavinash94 <87649563+rameshavinash94@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:59:48 -0600 Subject: [PATCH 2/3] PreCourse-1: completed exercises 1,2,3. --- Exercise_3.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Exercise_3.py b/Exercise_3.py index 0e561edf3..b1c71fa2d 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -51,10 +51,7 @@ def _traverseLinkedList(self, findVal): If the target_data is not found, it returns (None, None). """ tmpNode = self.head - - if tmpNode.data == findVal: - return None, tmpNode - + prev = None while tmpNode: if tmpNode.data == findVal: return prev, tmpNode @@ -73,9 +70,9 @@ def find(self, key): return prev, curr = self._traverseLinkedList(key) if curr: - print( f"element {curr.data} found @ {curr}") + print( f"element {key} found @ {curr}") return curr - print('element {curr.data} not found') + print( f'element {key} not found') def remove(self, key): """ @@ -109,5 +106,7 @@ def printLinkedList(self): newList.append(15) newList.find(30) newList.find(90) +newList.find(10) newList.remove(10) -newList.printLinkedList() \ No newline at end of file +newList.printLinkedList() +newList.find(10) From 4771b1cdd492ec1cf930a58a5f08ae1833b87060 Mon Sep 17 00:00:00 2001 From: rameshavinash94 <87649563+rameshavinash94@users.noreply.github.com> Date: Sat, 18 Jan 2025 20:03:35 -0600 Subject: [PATCH 3/3] PreCourse-1: completed exercises 1,2,3. --- Exercise_3.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Exercise_3.py b/Exercise_3.py index b1c71fa2d..5a32116c5 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -47,8 +47,8 @@ def _traverseLinkedList(self, findVal): findVal: The data to search for in the linked list. Returns: A tuple containing the previous node and the current node. - If the target_data is found in the head node, it returns (None, head). - If the target_data is not found, it returns (None, None). + If the findVal is found in the head node, it returns (None, head). + If the findVal is not found, it returns (None, None). """ tmpNode = self.head prev = None