diff --git a/names/binary_search_tree.py b/names/binary_search_tree.py new file mode 100644 index 000000000..908289f40 --- /dev/null +++ b/names/binary_search_tree.py @@ -0,0 +1,53 @@ + + +class BinarySearchTree: + def __init__(self): + self.root_node = None + + def isEmpty(self): + return self.root_node is None + + def insert(self, value): + if self.isEmpty(): + self.root_node = BSTNode(value) + else: + self.root_node.insert(value) + + def contains(self, target): + if self.isEmpty(): + return False + else: + return self.root_node.contains(target) + + +# BSTNode class + +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 value < self.value: + if self.left is None: + self.left = BSTNode(value) + else: + self.left.insert(value) + elif value >= self.value: + if not self.right: + self.right = BSTNode(value) + else: + self.right.insert(value) + + + # Return True if the tree contains the value + # False if it does not + def contains(self, target): + if target == self.value: + return True + elif target < self.value: + return self.left.contains(target) if self.left else False + else: + return self.right.contains(target) if self.right else False \ No newline at end of file diff --git a/names/names.py b/names/names.py index ea158997f..b087cecd0 100644 --- a/names/names.py +++ b/names/names.py @@ -1,4 +1,5 @@ import time +from binary_search_tree import BinarySearchTree start_time = time.time() @@ -13,10 +14,14 @@ duplicates = [] # Return the list of duplicates in this data structure # Replace the nested for loops below with your improvements +bst = BinarySearchTree() + for name_1 in names_1: - for name_2 in names_2: - if name_1 == name_2: - duplicates.append(name_1) + bst.insert(name_1) + +for name_2 in names_2: + if bst.contains(name_2): + duplicates.append(name_2) end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") @@ -26,3 +31,22 @@ # 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_1 = f.read().split("\n") # List containing 10000 names +f.close() + +f = open('names_2.txt', 'r') +names_2 = f.read().split("\n") # List containing 10000 names +f.close() + +duplicates = [] # Returns the duplicates in this data structure + +# Replace the nested for loops below with your improvements +duplicates = list(set(names_1) & set(names_2)) + +end_time = time.time() +print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") +print (f"runtime: {end_time - start_time} seconds") diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..13cfc6758 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -39,4 +39,9 @@ def contains(self, value): return False def reverse_list(self, node, prev): - pass + if node is None: + self.head = prev + return + + self.reverse_list(node.get_next(), node) + node.set_next(prev) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..9467efd98 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,21 @@ class RingBuffer: - def __init__(self, capacity): - pass + def __init__(self, capacity = 10): + self.capacity = capacity + self.size = 0 + self.write_index = 0 + self.storage = [] def append(self, item): - pass + if self.size >= self.capacity: + self.storage[self.write_index] = item + else: + self.storage.append(item) + self.size += 1 + + if self.write_index < (self.capacity - 1): + self.write_index += 1 + else: + self.write_index = 0 def get(self): - pass \ No newline at end of file + return self.storage \ No newline at end of file