From 4186fba7518c475c023fc8be1c0d2450e21ee15c Mon Sep 17 00:00:00 2001 From: kmk028 Date: Sun, 27 Sep 2020 18:17:19 -0700 Subject: [PATCH 1/3] RingBuffer completed --- ring_buffer/ring_buffer.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..d5a37934d 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,14 @@ class RingBuffer: def __init__(self, capacity): - pass + self.capacity = capacity + self.storage = [None]*capacity + self.curr = 0 def append(self, item): - pass + self.storage[self.curr] = item + self.curr += 1 + if self.curr == self.capacity: + self.curr=0 def get(self): - pass \ No newline at end of file + return [x for x in self.storage if x is not None] \ No newline at end of file From 753b0cc6a7925cf5d622c9f2656f3850e944c6db Mon Sep 17 00:00:00 2001 From: kmk028 Date: Sun, 27 Sep 2020 18:45:56 -0700 Subject: [PATCH 2/3] RunTime optimization using BST --- names/BST.py | 38 ++++++++++++++++++++++++++++++++++++++ names/names.py | 28 ++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 names/BST.py diff --git a/names/BST.py b/names/BST.py new file mode 100644 index 000000000..3393f403a --- /dev/null +++ b/names/BST.py @@ -0,0 +1,38 @@ +class BSTNode: + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + # Insert the given value into the tree + def insert(self, value): + # if root is not empty and value is greater than root, place value to right of root + if (value >= self.value) and (self.right != None): + self.right.insert(value) + # if root is empty and value is greater than root, create a node and place value to right of root + if (value >= self.value) and (self.right == None): + new_node = BSTNode(value) + self.right = new_node + # if root is not empty and value is lesser than root, place value to right of root + if (value < self.value) and (self.left != None): + self.left.insert(value) + # if root is empty and value is lesser than root, create a node and place value to left of root + if (value < self.value) and (self.left == None): + new_node = BSTNode(value) + self.left = new_node + + # Return True if the tree contains the value + # False if it does not + def contains(self, target): + if target == self.value: + return True + if (target>self.value): + if self.right is not None: + return self.right.contains(target) + else: + return False + if (target Date: Sun, 27 Sep 2020 19:28:06 -0700 Subject: [PATCH 3/3] reverse Linked list and Stretch goal completed --- names/names.py | 25 +++++++++++++++++++++++++ reverse/reverse.py | 15 ++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/names/names.py b/names/names.py index a806ba07e..502a7d934 100644 --- a/names/names.py +++ b/names/names.py @@ -42,3 +42,28 @@ # Python has built-in tools that allow for a very efficient approach to this problem # What's the best time you can accomplish? Thare are no restrictions on techniques or data # structures, but you may not import any additional libraries that you did not write yourself. + +start_time = time.time() + +f = open('names_1.txt', 'r') +names_3 = f.read().split("\n") # List containing 10000 names +f.close() + +f = open('names_2.txt', 'r') +names_4 = f.read().split("\n") # List containing 10000 names +f.close() + +duplicates1 = [] # Return the list of duplicates in this data structure + +def intersection(lst1, lst2): + temp = set(lst2) + lst3 = [value for value in lst1 if value in temp] + return lst3 + +duplicates1 = intersection(names_3,names_4) + +end_time = time.time() +print (f"{len(duplicates1)} duplicates:\n\n{', '.join(duplicates1)}\n\n") +print (f"runtime: {end_time - start_time} seconds") + +##Runtime reduced to 0.003970146179199219 seconds diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..21273d259 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -39,4 +39,17 @@ def contains(self, value): return False def reverse_list(self, node, prev): - pass + # Iterative solution + # initialize 3 pointers. Prev = Null, curr = head, next is null + prev = None + current = self.head + # store the next node before changeing it + # change the next pointer to previous. this is where it does reverse + # move prev and current 1 step ahead + # repeat process till current node is not null. + while current: + next = current.next_node + current.next_node = prev + prev=current + current=next + self.head=prev