From d159dae194cddd93629ceaf891ac7770c391b98d Mon Sep 17 00:00:00 2001 From: Melvin Milkiyas Date: Tue, 11 Nov 2025 23:07:42 -0800 Subject: [PATCH] Precourse 1 - completed --- Exercise_1.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++--- Exercise_2.py | 18 +++++++++++++++- Exercise_3.py | 34 +++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..609e07718 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,78 @@ + +### Stack with array + + class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): + self.stack = [] + self.MAX = 5 + + + def size(self): + return len(self.stack) + #Time complexity O(1) + def isEmpty(self): + if self.size()==0: + return True + else: + return False + #Time complexity O(1) + def push(self, item): + if self.size()!=self.MAX: + self.stack.append(item) + else: + print("Stack is full") + return self.stack + #Time complexity O(1) + def pop(self): + if not self.isEmpty(): + p=self.stack.pop() + return p + else: + print("Stack is empty") + return None + #Time complexity O(1) + def peek(self): - - def size(self): + if not self.isEmpty(): + return self.stack[-1] + else: + return None + #Time complexity O(1) + def show(self): - + return self.stack + #Time complexity O(1) +# Space Complexity: O(n) s = myStack() s.push('1') s.push('2') +s.push('3') +s.push('4') +s.push('5') +s.push('6') + + +print(s.pop()) +print(s.pop()) +print(s.pop()) print(s.pop()) + + + print(s.show()) +print(s.peek()) + + diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..5100245c4 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -6,10 +6,26 @@ def __init__(self, data): class Stack: def __init__(self): + self.top = None + self.count = 0 def push(self, data): - + new_node = Node(data) + new_node.next = self.top + self.top = new_node + self.count+=1 + #Time complexity O(1) + def pop(self): + if self.top == None: + return None + else: + p = self.top.data + self.top = self.top.next + self.count -=1 + return p + #Time complexity O(1) + #Space complexity O(n) a_stack = Stack() while True: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..2de832772 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,6 +3,8 @@ 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,22 +13,54 @@ def __init__(self): Takes O(1) time. """ self.head = None + 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 == None: + self.head = new_node + return + current = self.head + while current.next: + current = current.next + current.next = new_node + 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: + if current.data == key: + return current.data + current = current.next + return None + + def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + + current = self.head + previous_node = None + + while current: + if current.data == key: + if previous_node: + previous_node.next=current.next + return + else: + self.head = current.next + previous_node = current + current=current.next