From 3bae9c01297b6425042ce2845e35b9fc598a9bab Mon Sep 17 00:00:00 2001 From: Carlos Alvarado Date: Fri, 14 Aug 2020 11:11:51 -0500 Subject: [PATCH 1/2] finsihed sprint --- names/names.py | 39 ++++++++++++++++++++--- reverse/reverse.py | 13 +++++++- ring_buffer/ring_buffer.py | 63 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 106 insertions(+), 9 deletions(-) diff --git a/names/names.py b/names/names.py index ea15899..32cdaf1 100644 --- a/names/names.py +++ b/names/names.py @@ -1,4 +1,5 @@ import time +from collections import deque start_time = time.time() @@ -11,12 +12,40 @@ f.close() duplicates = [] # Return the list of duplicates in this data structure - # Replace the nested for loops below with your improvements -for name_1 in names_1: - for name_2 in names_2: - if name_1 == name_2: - duplicates.append(name_1) +# for name_1 in names_1: +# for name_2 in names_2: +# if name_1 == name_2: +# duplicates.append(name_1) +counter = 0 + +q = deque() + +q.append(names_1[counter]) + +while len(q) > 0: + # we will pop the name from the left + popped_name = q.popleft() + # we will see if the name is in names_2 + # if it is, then we will append it to duplicates, + # we will increase the counter + # else, we will increase the counter and append the next element in + # names_1 to our q and repeat the process again + if popped_name in names_2: + + duplicates.append(popped_name) + counter += 1 + q.append(names_1[counter]) + + if counter == len(names_1) -1:# last case to worry about since index is out of range + pass + + else: + counter += 1 + q.append(names_1[counter]) + + + end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252..aabd7ea 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -39,4 +39,15 @@ def contains(self, value): return False def reverse_list(self, node, prev): - pass + if node is None: + return + + elif node.next_node is None: + self.head = node + self.head.next_node = prev + return self.head + else: + reversed_list = self.reverse_list(node.next_node, prev) + node.next_node.next_node = node + node.next_node = prev + return reversed_list diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0..4f7831d 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,66 @@ class RingBuffer: def __init__(self, capacity): - pass + self.capacity = capacity + self.list = [] # empty list + self.current = 0 # the current length of our list/where we are at + # we want it + # to be a certain size, if the size is passed the capacity + # then we will start back at the beginnig and then overwrite the value at that index + def append(self, item): - pass + # can only be a certain length + # if we reach the ending index or + # if self.current === self.capacity, we want to start back at the beginning + # to overwrite the current info + + + if len(self.list) < self.capacity: + self.list.append(item) + self.current += 1 + else: + if self.current == self.capacity: + + self.current = 0 + self.list[self.current] = item + self.current += 1 + + else: + self.list[self.current] = item + self.current += 1 # lol i forgot to increment here + + + + + + + + # if self.current == self.capacity: + # # make the current equal to the first one + # self.current = 0 + # self.list[self.current] = item + # self.current += 1 + + # else: + # self.list[self.current] = item + # self.current += 1 def get(self): - pass \ No newline at end of file + + return self.list + + +ring = RingBuffer(5) + +ring.append(1) +ring.append(2) +ring.append(3) +ring.append(4) +ring.append(5) +ring.append(6) +ring.append(7) +ring.append(8) +ring.append(9) +ring.append(10) + +print(ring.get(), 'the list with all 5 elements appended') \ No newline at end of file From 4ec9740e5efe17a88f7d0dda3002bebfd8dc20aa Mon Sep 17 00:00:00 2001 From: Carlos Alvarado Date: Fri, 14 Aug 2020 17:49:43 -0500 Subject: [PATCH 2/2] finished and actually optimized runtime of names, without in operator --- names/names.py | 126 +++++++++++++++++++++++++++++++------ reverse/reverse.py | 1 + ring_buffer/ring_buffer.py | 18 +++--- 3 files changed, 116 insertions(+), 29 deletions(-) diff --git a/names/names.py b/names/names.py index 32cdaf1..07cbe40 100644 --- a/names/names.py +++ b/names/names.py @@ -11,38 +11,124 @@ names_2 = f.read().split("\n") # List containing 10000 names f.close() -duplicates = [] # Return the list of duplicates in this data structure +duplicates = [] + +# implementation with cache +# cache = {} + +# for name in names_1: + +# if name not in cache: +# cache[name] = name + + +# for name2 in names_2: +# if name2 in cache: +# duplicates.append(name2) + +# print(duplicates,'duplicates array') + +# Return the list of duplicates in this data structure # Replace the nested for loops below with your improvements # for name_1 in names_1: # for name_2 in names_2: # if name_1 == name_2: # duplicates.append(name_1) -counter = 0 -q = deque() -q.append(names_1[counter]) -while len(q) > 0: - # we will pop the name from the left - popped_name = q.popleft() - # we will see if the name is in names_2 - # if it is, then we will append it to duplicates, - # we will increase the counter - # else, we will increase the counter and append the next element in - # names_1 to our q and repeat the process again - if popped_name in names_2: +class BSTNode: + def __init__(self, value): + self.value = value + self.left = None + self.right = None - duplicates.append(popped_name) - counter += 1 - q.append(names_1[counter]) + def insert(self, value): + if value < self.value: + if self.left is None: + self.left = BSTNode(value) - if counter == len(names_1) -1:# last case to worry about since index is out of range - pass + else: + self.left.insert(value) + else: + if self.right is None: + self.right = BSTNode(value) + + else: + self.right.insert(value) + def contains(self, target): + # base case? + # we find the target in a tree node + if self.value == target: + duplicates.append(target) + # figure out which direction we need to go in + if target < self.value: + # we go left + if not self.left: + pass + else: + return self.left.contains(target) + # or, we get to a spot where the node should be, but nothing is there + # how do we move towards the base case? + else: + # we go right + if not self.right: + pass + else: + return self.right.contains(target) + +first_node = BSTNode(names_1[0]) + + +for i in range(len(names_1)): + + if i == 0: + # first_node = BSTNode(i) + pass else: - counter += 1 - q.append(names_1[counter]) + first_node.insert(names_1[i]) + + +for i in range(len(names_2)): + + first_node.contains(names_2[i]) + + + + + + + + + + +# counter = 0 + +# q = deque() + +# q.append(names_1[counter]) + +# while len(q) > 0: +# # we will pop the name from the left +# popped_name = q.popleft() +# # we will see if the name is in names_2 +# # if it is, then we will append it to duplicates, +# # we will increase the counter +# # else, we will increase the counter and append the next element in +# # names_1 to our q and repeat the process again +# if popped_name in names_2: + +# duplicates.append(popped_name) +# counter += 1 +# q.append(names_1[counter]) + +# if counter == len(names_1) -1:# last case to worry about since index is out of range +# pass + +# else: +# counter += 1 +# q.append(names_1[counter]) diff --git a/reverse/reverse.py b/reverse/reverse.py index aabd7ea..7d5cf62 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -39,6 +39,7 @@ def contains(self, value): return False def reverse_list(self, node, prev): + if node is None: return diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 4f7831d..4cd8f7e 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -17,17 +17,17 @@ def append(self, item): if len(self.list) < self.capacity: self.list.append(item) - self.current += 1 - else: - if self.current == self.capacity: + # self.current += 1 + + elif self.current == self.capacity: - self.current = 0 - self.list[self.current] = item - self.current += 1 + self.current = 0 + self.list[self.current] = item + # self.current += 1 - else: - self.list[self.current] = item - self.current += 1 # lol i forgot to increment here + else: + self.list[self.current] = item + self.current += 1 # lol i forgot to increment here