diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..dcf3edd7a 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,43 @@ +# Time Complexity: +# isEmpty: O(1) +# push: O(1) amortized +# pop: O(1) +# peek: O(1) +# size: O(1) +# show: O(n) +# +# Space Complexity: O(n), where n is the number of elements in the 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): - + def __init__(self): + self.stack = [] + + def isEmpty(self): + return len(self.stack) == 0 + + def push(self, item): + self.stack.append(item) + + def pop(self): + if self.isEmpty(): + return None + return self.stack.pop() + + def peek(self): + if self.isEmpty(): + return None + return self.stack[-1] + + def size(self): + return len(self.stack) + + def show(self): + return self.stack + s = myStack() s.push('1') s.push('2') print(s.pop()) print(s.show()) + diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..3fefb3498 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,32 +1,58 @@ +# Time Complexity: +# push: O(1) +# pop: O(1) +# +# Space Complexity: O(n), where n is the number of elements in the 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.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 + + popped_value = self.top.data + self.top = self.top.next + return popped_value + + 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" + + if len(do) == 0: + continue + operation = do[0].strip().lower() + if operation == 'push': a_stack.push(int(do[1])) + elif operation == 'pop': popped = a_stack.pop() + if popped is None: print('Stack is empty.') else: - print('Popped value: ', int(popped)) + print('Popped value:', int(popped)) + elif operation == 'quit': break + diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..ec494c746 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,13 +1,23 @@ +# Time Complexity: +# append: O(n) +# find: O(n) +# remove: O(n) +# Space Complexity: O(n), where n is the number of nodes in the linked list. + class ListNode: """ - A node in a singly-linked list. + 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. + Create a new singly linked list. Takes O(1) time. """ self.head = None @@ -17,16 +27,80 @@ 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): """ - Search for the first element with `data` matching - `key`. Return the element or `None` if not found. + Search for the first element with data matching key. + Return the node 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. + Remove the first occurrence of key in the list. Takes O(n) time. """ + if self.head is None: + return False + + if self.head.data == key: + self.head = self.head.next + return True + + curr = self.head + + while curr.next is not None: + if curr.next.data == key: + curr.next = curr.next.next + return True + + curr = curr.next + + return False + + def show(self): + curr = self.head + result = [] + + while curr is not None: + result.append(curr.data) + curr = curr.next + + return result + + +ll = SinglyLinkedList() +ll.append(10) +ll.append(20) +ll.append(30) + +print(ll.show()) + +node = ll.find(20) +if node is not None: + print("Found:", node.data) +else: + print("Not found") + +ll.remove(20) +print(ll.show())