From 09332b7ee8416ae2bbb569b29857af7d8f8989dc Mon Sep 17 00:00:00 2001 From: Kartavya Bhatt Date: Tue, 20 Jan 2026 09:33:18 -0500 Subject: [PATCH] Pre-course - 1 complete --- Exercise_1.py | 37 +++++++++++++++++----- Exercise_2.py | 26 +++++++++++++++- Exercise_3.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 8 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..119c2b6ca 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,21 +1,44 @@ +# Time Complexity : O(1) +# Space Complexity : O(N) +# Did this code successfully run on Leetcode : Ran it locally +# Any problem you faced while coding this : Deciding the max length and adding the sanity checks + + +# 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.maxLen = 1000 # Maximum length allowed for the stack + self.stack = [] # Array that will be used as stack + self.length = 0 # length for O(1) access to check the length + def isEmpty(self): + if self.length == 0: # If the stack is empty then the length will be zero + return True + return False def push(self, item): + if self.length == self.maxLen: # If the length has reached the max allowed length then reject the push + return "Stack is full" + self.stack.append(item) + self.length += 1 def pop(self): + if self.length == 0: # If the stack is empty then reject the pop + return "Stack is empty!" + self.length -= 1 + return self.stack.pop() def peek(self): - - def size(self): - - def show(self): + if len(self.stack) == 0: # If the stack is empty reject the peek as there is no item to peek + return "Stack is empty!" + return self.stack[-1] + + def size(self): # Return the size of the stack + return self.length + def show(self): # Return the entire stack + return self.stack s = myStack() s.push('1') diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..71e9c9d8b 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,12 @@ +# Time Complexity : O(1) +# Space Complexity : O(N) +# Did this code successfully run on Leetcode : Ran it locally +# Any problem you faced while coding this : Forgot what actually stack means and build a queue using linkedlist +# Staying focussed and aware is what I need to improve on. +# Learned to write comments using '''''' + +# Your code here along with comments explaining your approach class Node: def __init__(self, data): self.data = data @@ -6,10 +14,26 @@ def __init__(self, data): class Stack: def __init__(self): + '''Create an empty pointer to store the top of the linked list/stack''' + self.linkedListTop = None def push(self, data): - + '''Creating a node with the data to add to the top of the linked list''' + dataNode = Node(data) + if self.linkedListTop is None: + self.linkedListTop = dataNode + else: + '''Adding datanode to the top of the linked list and setting the linkedListTop to it''' + dataNode.next = self.linkedListTop + self.linkedListTop = dataNode + def pop(self): + if self.linkedListTop is None: + return None + '''Removing the top node and setting linkedListTop to the next node in the list''' + poppedData = self.linkedListTop.data + self.linkedListTop = self.linkedListTop.next + return poppedData a_stack = Stack() while True: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..5e2ca79db 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,17 @@ +# Time Complexity : O(N) for all operations +# Space Complexity : O(N) +# Did this code successfully run on Leetcode : Ran it locally +# Any problem you faced while coding this : None + + +# Your code here along with comments explaining your approach 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 +26,21 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + node = ListNode(data=data) + '''If the linkedlist is empty set the head to the node''' + '''Else iterate the linkedlist to reach the end of the list and attach the node''' + + if self.head is None: + self.head = node + + else: + curr = self.head + + while curr.next: + curr = curr.next + + curr.next = node + def find(self, key): """ @@ -24,9 +48,70 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + curr = self.head + '''Iterate the linkedlist to find the key''' + while curr: + if curr.data == key: + return curr + curr = curr.next + + return None def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + + '''If the key is the first node then update the head to head.next''' + if self.head.data == key: + curr = self.head + self.head = self.head.next + return curr + + '''Iterate the linkedlist and link the node before the key with the node after the key''' + curr = self.head + prev = self.head + while curr: + if curr.data == key: + prev.next = curr.next + return curr + prev = curr + curr = curr.next + + def show(self): + curr = self.head + result = [] + while curr: + result.append(curr.data) + curr = curr.next + return result + + +a_stack = SinglyLinkedList() +while True: + print('Linked List: ', a_stack.show()) + #Give input as string if getting an EOF error. Give input like "push 10" or "find 10" + print('push ') + print('find ') + print('remove ') + print('quit') + 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() + if operation == 'push': + a_stack.append(int(do[1])) + elif operation == 'find': + popped = a_stack.find(int(do[1])) + if popped is None: + print('Element not found') + else: + print('Element found: ', popped.__str__) + elif operation == 'remove': + popped = a_stack.remove(int(do[1])) + if popped is None: + print('Element not found') + else: + print('Element removed successfully') + elif operation == 'quit': + break \ No newline at end of file