From d7a4d0eba044756637f886644f8bb00d79bb9d73 Mon Sep 17 00:00:00 2001 From: CHINMAY-PRAJAPATI Date: Thu, 9 Apr 2026 23:23:04 -0400 Subject: [PATCH] PreCourse 1 Done --- Exercise_1.py | 40 ++++++++++++++++++++++++++++++++++------ Exercise_2.py | 35 +++++++++++++++++++++++++++++++++++ Exercise_3.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 6 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..4cfcf5663 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,52 @@ +# Time Complexity : O(1) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : yes +# Any problem you faced while coding this : No + + +# Your code here along with comments explaining your approach + class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): + self.arr = list() def isEmpty(self): - + return self.size() == 0 + def push(self, item): + self.arr.append(item) def pop(self): - + if self.isEmpty(): + return "Stack is Empty" + + return self.arr.pop() def peek(self): - - def size(self): + if self.isEmpty(): + return "Stack is Empty" + + return self.arr[-1] + def size(self): + return len(self.arr) + def show(self): + return self.arr s = myStack() +print("Push: 1") s.push('1') +print("Push: 2") s.push('2') -print(s.pop()) -print(s.show()) +print("Show: ",s.show()) +print("isEmpty: ",s.isEmpty()) +print("Pop: ",s.pop()) +print("Show: ",s.show()) +print("Pop: ",s.pop()) +print("Show: ",s.show()) +print("isEmpty: ",s.isEmpty()) +print("Pop: ",s.pop()) \ No newline at end of file diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..84e800b2c 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,3 +1,10 @@ +# Time Complexity : O(1) with show() it will be O(n) +# push: O(1) +# pop: O(1) +# show: O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : yes +# Any problem you faced while coding this : No class Node: def __init__(self, data): @@ -6,10 +13,35 @@ def __init__(self, data): class Stack: def __init__(self): + self.head = None def push(self, data): + newNode = Node(data) + current = self.head + self.head = newNode + self.head.next = current def pop(self): + if self.head is None: + return None + + tmp = self.head.data + self.head = self.head.next + return tmp + + def show(self): + current = self.head + if current is None: + print("Stack is empty") + return + print("\n--- Stack (top to bottom) ---") + while current is not None: + addr = id(current) + next_addr = id(current.next) if current.next else None + print(f"Data: {current.data} | Address: {addr} | Next: {next_addr}") + current = current.next + print("-----------------------------\n") + a_stack = Stack() while True: @@ -17,6 +49,7 @@ def pop(self): print('push ') print('pop') print('quit') + print('show') do = input('What would you like to do? ').split() #Give input as string if getting an EOF error. Give input like "push 10" or "pop" operation = do[0].strip().lower() @@ -28,5 +61,7 @@ def pop(self): print('Stack is empty.') else: print('Popped value: ', int(popped)) + elif operation == 'show': + a_stack.show() elif operation == 'quit': break diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..e63a9e641 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,18 @@ +# Time Complexity : O(n) +# append: O(n) +# find: O(n) +# remove: O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : yes +# Any problem you faced while coding this : No + 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,6 +21,7 @@ def __init__(self): Takes O(1) time. """ self.head = None + def append(self, data): """ @@ -18,15 +29,54 @@ def append(self, data): Takes O(n) time. """ + if self.head is not None: + current = self.head + while current.next is not None: + current = current.next + + current.next = ListNode(data) + else: + self.head = ListNode(data) + 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. """ + if self.head is not None: + current = self.head + while current is not None: + if current.data == key: + return "Found the matching key" + current = current.next + return None + return None def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + if self.head is not None: + current = self.head + pointer = self.head + while current is not None: + if current.data == key: + if current == self.head: + self.head = self.head.next + return "key removed" + pointer.next = current.next + return "key removed" + pointer = current + current = current.next + + return None + return None + + + + + + +