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/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. 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 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