diff --git a/names/bst.py b/names/bst.py new file mode 100644 index 000000000..84c36a8dc --- /dev/null +++ b/names/bst.py @@ -0,0 +1,45 @@ +''' +Binary search trees are a data structure that enforce an ordering over +the data they store. That ordering in turn makes it a lot more efficient +at searching for a particular piece of data in the tree. ''' + +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): + #compare the current value of the node(self.value) + if value < self.value: + #insert the value in left + if self.left is None: + # insert node value if no node in the left side + self.left = BSTNode(value) + else: + # repeat the process for the next node + self.left.insert(value) + if value >= self.value: + if self.right is None: + 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 + if target < self.value: + if self.left is None: + return False + else: + return self.left.contains(target) + + if target >=self.value: + if self.right is None: + return False + else: + return self.right.contains(target) \ No newline at end of file diff --git a/names/names.py b/names/names.py index ea158997f..e993a3bf2 100644 --- a/names/names.py +++ b/names/names.py @@ -10,19 +10,63 @@ names_2 = f.read().split("\n") # List containing 10000 names 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) + +# for name_1 in names_1: +# if name_1 in names_2: +# duplicates.append(name_1) +from bst import BSTNode + +# CREATE TREE WITH names_1 LIST +start_time = time.time() + +tree = BSTNode(names_1[0]) +for name in names_1: + tree.insert(name) + +duplicates = [name for name in names_2 if tree.contains(name)] end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") print (f"runtime: {end_time - start_time} seconds") + + # ---------- 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 # structures, but you may not import any additional libraries that you did not write yourself. + +# LIST COMPREHENSION +# ~ 1.5 seconds which is def better than the original + +# CREATE TREE WITH names_2 LIST (just in case order is better) +# ... it's not +start_time = time.time() + +tree = BSTNode(names_2[0]) +for name in names_2: + tree.insert(name) + +duplicates = [name for name in names_1 if tree.contains(name)] + +end_time = time.time() +print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") +print (f"runtime: {end_time - start_time} seconds") + + + + +# USING SETS + diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..9728a066e 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 + #Points head to the node and set previous node None + prev = None + node = self.head + # continue till node is not empty + while node is not None: + next_node = node.next_node + node.next_node =prev + prev = node + node = next_node + self.head = prev + + diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..b626f8a19 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,31 @@ class RingBuffer: def __init__(self, capacity): - pass - + self.capacity = capacity + self.data = [] + self.index =0 + + def append(self, item): - pass - + # check if the buffer is full + if len(self.data) == self.capacity: + # overwrite the past value with new item + self.data.pop(self.index) + self.data.insert(self.index,item) + self.index = (self.index +1 ) % self.capacity + else: + self.data.append(item) + def get(self): - pass \ No newline at end of file + return self.data + +buffer = RingBuffer(3) +print(buffer.get()) +buffer.append('a') +buffer.append('b') +buffer.append('c') +print(buffer.get()) +buffer.append('d') +print(buffer.get()) + +buffer.append('e') +print(buffer.get()) \ No newline at end of file