diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5..b1f6bea0 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,38 @@ +# 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() + else: + return None + + def peek(self): + return self.stack[-1] if not self.isEmpty() else None + + 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 b1149221..9af30215 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,32 +1,45 @@ - +# The intution here is like inserting in reverese order +# Time Complexity : O(1) for push, O(n) for pop +# Space Complexity : O(1) 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 = None + def push(self, data): - + new_node = Node(data) + new_node.next = self.head + self.head = new_node + def pop(self): - + 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() 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 a5d466b5..470ab1d6 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")