From 68e3736ccdc66315865e370612a688221179f760 Mon Sep 17 00:00:00 2001 From: lex-marie790 Date: Sun, 27 Sep 2020 18:58:01 -0700 Subject: [PATCH 1/3] ring_buffer completed --- ring_buffer/ring_buffer.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..4438f4471 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,15 @@ class RingBuffer: def __init__(self, capacity): - pass + self.capacity = capacity + self.index = -1 + self.storage = [] def append(self, item): - pass - + if len(self.storage) == self.capacity: + self.index = (self.index + 1) % self.capacity + self.storage[self.index] = item + else: + self.storage.append(item) + def get(self): - pass \ No newline at end of file + return self.storage \ No newline at end of file From e312b61dd1e3654c9429662b8fda1e5c76fb7b35 Mon Sep 17 00:00:00 2001 From: lex-marie790 Date: Sun, 27 Sep 2020 19:30:54 -0700 Subject: [PATCH 2/3] names completed --- names/names.py | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/names/names.py b/names/names.py index ea158997f..534fa892c 100644 --- a/names/names.py +++ b/names/names.py @@ -13,10 +13,44 @@ 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) +class BSTNode: + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + def insert(self, value): + if value < self.value: + if not self.left: + self.left = BSTNode(value) + else: + self.left.insert(value) + else: + if not self.right: + self.right = BSTNode(value) + else: + self.right.insert(value) + + def contains(self, target): + if self.value == target: + return True + if target < self.value: + if not self.left: + return False + else: + return self.left.contains(target) + else: + if not self.right: + return False + else: + return self.right.contains(target) + +bst = BSTNode(names_1[0]) +for names1 in names_1: + bst.insert(names1) +for names2 in names_2: + if bst.contains(names2): + duplicates.append(names2) end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") @@ -24,5 +58,5 @@ # ---------- Stretch Goal ----------- # 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 +# What's the best time you can accomplish? There are no restrictions on techniques or data # structures, but you may not import any additional libraries that you did not write yourself. From 8f61f7a09b049bd719d81635f797f316fe7f48d0 Mon Sep 17 00:00:00 2001 From: lex-marie790 Date: Sun, 27 Sep 2020 20:07:35 -0700 Subject: [PATCH 3/3] reverse completed --- README.md | 2 +- reverse/reverse.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7468701de..3aad64697 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ While credit will be given for a functional solution, only optimal solutions wil #### Stretch -* Say your code from `names.py` is to run on an embedded computer with very limited RAM. Because of this, memory is extremely constrained and you are only allowed to store names in arrays (i.e. Python lists). How would you go about optimizing the code under these conditions? Try it out and compare your solution to the original runtime. (If this solution is less efficient than your original solution, include both and label the strech solution with a comment) +* Say your code from `names.py` is to run on an embedded computer with very limited RAM. Because of this, memory is extremely constrained and you are only allowed to store names in arrays (i.e. Python lists). How would you go about optimizing the code under these conditions? Try it out and compare your solution to the original runtime. (If this solution is less efficient than your original solution, include both and label the stretch solution with a comment) ### Rubric diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..3179f5e97 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -39,4 +39,10 @@ def contains(self, value): return False def reverse_list(self, node, prev): - pass + while node: + reverse_node = node.next_node + node.next_node = prev + prev = node + node = reverse_node + self.head = prev + return node \ No newline at end of file