From 37817dadeeeefd327043aa259380dd822204c0b5 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Mon, 6 Apr 2026 18:25:06 -0400 Subject: [PATCH] chore: Completed precourse-1 --- Exercise_1.py | 55 ++++++++++++++++++++++++++-------------- Exercise_2.py | 50 +++++++++++++++++++++++------------- Exercise_3.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 136 insertions(+), 39 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..f5121be53 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,41 @@ +# Time Complexity : everything is O(1) +# Space Complexity : O(n) +# 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): - - def isEmpty(self): - - def push(self, item): - - def pop(self): - - - def peek(self): - - def size(self): - - def show(self): - + # Please read sample.java file before starting. + # Kindly include Time and Space complexity at top of each file + def __init__(self): + self.arr = [] + + def isEmpty(self): + if len(self.arr) == 0: + return True + return False + + def push(self, item): + self.arr.append(item) + + def pop(self): + return self.arr.pop() + + def peek(self): + return self.arr[-1] + + def size(self): + return len(self.arr) + + def show(self): + return self.arr + s = myStack() -s.push('1') -s.push('2') +s.push("1") +s.push("2") print(s.pop()) print(s.show()) diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..9421fceb3 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,32 +1,48 @@ +# Time Complexity : O(1) +# Space Complexity : O(n) +# 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 Node: def __init__(self, data): - self.data = data - self.next = None - + self.data = data + self.next = None + + class Stack: def __init__(self): - + self.arr = [] + def push(self, data): - + self.arr.append(data) + def pop(self): - + if len(self.arr) == 0: + return None + return self.arr.pop() + + a_stack = Stack() while True: - #Give input as string if getting an EOF error. Give input like "push 10" or "pop" - print('push ') - print('pop') - 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" + # Give input as string if getting an EOF error. Give input like "push 10" or "pop" + print("push ") + print("pop") + 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" + print(do) operation = do[0].strip().lower() - if operation == 'push': + if operation == "push": a_stack.push(int(do[1])) - elif operation == 'pop': + elif operation == "pop": popped = a_stack.pop() if popped is None: - print('Stack is empty.') + print("Stack is empty.") else: - print('Popped value: ', int(popped)) - elif operation == 'quit': + print("Popped value: ", int(popped)) + elif operation == "quit": break diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..743cddac9 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,9 +1,22 @@ +# Time Complexity : O(n) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : yes +# Any problem you faced while coding this : took me a little bit to warp my head around singly linked list but i think i get it now + + +# 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,16 +30,67 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ - + newNode = ListNode(data) + if self.head is None: + self.head = newNode + else: + current = self.head + while current.next is not None: + current = current.next + current.next = newNode + return newNode + 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. """ - + current = self.head + while current is not None: + 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.data == key: + self.head = self.head.next + return + + current = prev = self.head + while current is not None: + if current.data == key: + prev.next = current.next + return + else: + prev = current + current = current.next + + +ll = SinglyLinkedList() +while True: + print("append ") + print("find ") + print("remove ") + print("quit") + do = input("What would you like to do? ").split() + operation = do[0].strip().lower() + if operation == "append": + ll.append(int(do[1])) + print("Appended", do[1]) + elif operation == "find": + result = ll.find(int(do[1])) + if result is None: + print("Not found.") + else: + print("Found:", result.data) + elif operation == "remove": + ll.remove(int(do[1])) + print("Removed", do[1]) + elif operation == "quit": + break