From 802e600d66af80857328d77b044713b8435a07e0 Mon Sep 17 00:00:00 2001 From: Thasleem Date: Tue, 22 Oct 2024 13:19:15 -0700 Subject: [PATCH 1/2] Precourse-1 complete --- Exercise_1.py | 50 +++++++++++++++++++++++++++++------------------ Exercise_2.py | 54 ++++++++++++++++++++++++++++++++++----------------- Exercise_3.py | 40 ++++++++++++++++++++++++++++++++++---- 3 files changed, 103 insertions(+), 41 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..3c0b40965 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,36 @@ +# Time Complexity : O(1) for isEmpty, push, pop, peek, size +# Space Complexity : O(n) for storing elements in stack 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.stack = [] + self.count = 0 + + def isEmpty(self): + return self.count == 0 + + def push(self, item): + self.stack.append(item) + self.count += 1 + + def pop(self): + if self.count >= 1: + self.count -= 1 + return self.stack.pop() + + def peek(self): + return self.stack[-1] + + def size(self): + return self.count + + def show(self): + return self.stack + 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..17c1c95e9 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,32 +1,50 @@ - +# Time Complexity : O(1) for push, O(n) for pop +# Space Complexity : O(n) for storing n nodes in stack class Node: def __init__(self, data): - self.data = data - self.next = None - + self.data = data + self.next = None + + class Stack: def __init__(self): - + self.head = self.tail = Node(-1) + self.count = 0 + def push(self, data): - + self.tail.next = Node(data) + self.tail = self.tail.next + self.count+=1 + def pop(self): - + if self.count >= 1: + curr = self.head.next + while curr.next and curr.next.next: + curr = curr.next + + res = curr.next.data if curr.next else curr.data + curr.next = None + self.tail = curr + self.count-=1 + return res + + 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" 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..470ab1d6a 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -2,31 +2,63 @@ 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): """ Create a new singly-linked list. Takes O(1) time. """ - self.head = None + self.head = ListNode() def append(self, data): """ Insert a new element at the end of the list. Takes O(n) time. """ - + curr = self.head + while curr.next: + curr = curr.next + curr.next = 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. """ - + curr = self.head.next + while curr: + if curr.data == key: + return curr.data + curr = curr.next + return None + def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + + curr = self.head.next + while curr.next: + if curr.next.data == key: + curr.next = curr.next.next + return + curr = curr.next + + +sl = SinglyLinkedList() +for i in range(5): + sl.append(i) +sl.remove(3) +sl.append(7) +print("Found key {}".format(sl.find(2))) +sl.remove(2) +if sl.find(2) is None: + print("Not found") From 66cba52a5db67595d3e3e2616eaa561a0781375b Mon Sep 17 00:00:00 2001 From: Thasleem Date: Mon, 28 Oct 2024 16:16:58 -0700 Subject: [PATCH 2/2] fix review comments --- Exercise_1.py | 4 +++- Exercise_2.py | 27 +++++++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 3c0b40965..b1f6bea08 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -18,9 +18,11 @@ def pop(self): if self.count >= 1: self.count -= 1 return self.stack.pop() + else: + return None def peek(self): - return self.stack[-1] + return self.stack[-1] if not self.isEmpty() else None def size(self): return self.count diff --git a/Exercise_2.py b/Exercise_2.py index 17c1c95e9..9af30215b 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,5 +1,6 @@ +# The intution here is like inserting in reverese order # Time Complexity : O(1) for push, O(n) for pop -# Space Complexity : O(n) for storing n nodes in stack +# Space Complexity : O(1) for storing n nodes in stack class Node: def __init__(self, data): self.data = data @@ -8,25 +9,19 @@ def __init__(self, data): class Stack: def __init__(self): - self.head = self.tail = Node(-1) - self.count = 0 + self.head = None def push(self, data): - self.tail.next = Node(data) - self.tail = self.tail.next - self.count+=1 + new_node = Node(data) + new_node.next = self.head + self.head = new_node def pop(self): - if self.count >= 1: - curr = self.head.next - while curr.next and curr.next.next: - curr = curr.next - - res = curr.next.data if curr.next else curr.data - curr.next = None - self.tail = curr - self.count-=1 - return res + if self.head is None: + return None # Stack is empty + pop_value = self.head.data # Store the value to return + self.head = self.head.next # Move the top pointer down + return pop_value a_stack = Stack()