diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..4ec4cc46d 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,21 +1,33 @@ +# Time Complexity: push/pop/peek/isEmpty/size -> O(1); show -> O(n) +# Space Complexity: O(n), where n is the number of items stored + 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): - + def __init__(self): + self._data = [] + + def isEmpty(self): + return len(self._data) == 0 + + def push(self, item): + self._data.append(item) + + def pop(self): + if not self._data: + return None + return self._data.pop() + + def peek(self): + if not self._data: + return None + return self._data[-1] + + def size(self): + return len(self._data) + + def show(self): + return list(self._data) s = myStack() s.push('1') diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..bcc153d42 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,6 @@ +# Time Complexity: push/pop -> O(1) each +# Space Complexity: O(n), where n is the number of nodes in the stack class Node: def __init__(self, data): self.data = data @@ -6,11 +8,19 @@ def __init__(self, data): class Stack: def __init__(self): - + self.top = None + def push(self, data): - + new_node = Node(data) + new_node.next = self.top + self.top = new_node + def pop(self): - + if self.top is None: + return None + data = self.top.data + self.top = self.top.next + return data a_stack = Stack() while True: #Give input as string if getting an EOF error. Give input like "push 10" or "pop" diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..ee1578c7c 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,12 @@ +# Time Complexity: append/find/remove -> O(n) +# Space Complexity: O(1) per operation; O(n) for n nodes stored 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 +21,14 @@ 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 is None: + self.head = new_node + return + curr = self.head + while curr.next is not None: + curr = curr.next + curr.next = new_node def find(self, key): """ @@ -24,9 +36,36 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + curr = self.head + while curr is not None: + 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. """ + prev = None + curr = self.head + while curr is not None: + if curr.data == key: + if prev is None: + self.head = curr.next + else: + prev.next = curr.next + return + prev = curr + curr = curr.next + +if __name__ == "__main__": + sll = SinglyLinkedList() + sll.append(1) + sll.append(2) + sll.append(3) + found = sll.find(2) + print(found.data if found else None) + sll.remove(2) + print("Found" if sll.find(2) else "Not found")