diff --git a/names/name_tree.py b/names/name_tree.py new file mode 100644 index 000000000..c5d1c276a --- /dev/null +++ b/names/name_tree.py @@ -0,0 +1,39 @@ +class NameNode: + 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 = NameNode(value) + else: + self.left.insert(value) + elif value >= self.value: + if not self.right: + self.right = NameNode(value) + else: + self.right.insert(value) + + def contains(self, target): + if target == self.value: + return True + elif target < self.value: + if not self.left: + return False + else: + return self.left.contains(target) + elif target >= self.value: + if not self.right: + return False + else: + return self.right.contains(target) + + def in_order_print(self): + if self.left: + self.left.in_order_print() + print(self.value) + if self.right: + self.right.in_order_print() + \ No newline at end of file diff --git a/names/names.py b/names/names.py index ea158997f..56715affa 100644 --- a/names/names.py +++ b/names/names.py @@ -1,4 +1,5 @@ import time +from name_tree import NameNode start_time = time.time() @@ -13,13 +14,36 @@ 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) + +# -------- OLD TIME COMPLEXITY: O(n^3) ------------ +# O(n) time complexity +# for name_1 in names_1: + # O(n) time complexity +# for name_2 in names_2: + # O(n) time complexity +# if name_1 == name_2: +# duplicates.append(name_1) +# O(n) * O(n) * O(n) ---> O(n^3) +# Average time: 5.8 seconds + + +# -------- NEW TIME COMPLEXITY: O(n) ---------- +root = NameNode(names_1[0]) + +# O(n) time complexity +for index in range(1, (len(names_1) - 1), 1): + root.insert(names_1[index]) + +# O(n) time complexity +for name in names_2: + # O(log n) time complexity + if root.contains(name): + duplicates.append(name) +# O(n) + O(n) + O(log n) ---> O(2n) + O(log n) ---> O(2n) ---> O(n) +# Average time: 0.13 seconds end_time = time.time() -print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") +print (f"{len(duplicates)} duplicates:\n{', '.join(duplicates)}\n") print (f"runtime: {end_time - start_time} seconds") # ---------- Stretch Goal ----------- diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..b4358a632 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -18,25 +18,35 @@ def __init__(self): def add_to_head(self, value): node = Node(value) - if self.head is not None: node.set_next(self.head) - self.head = node def contains(self, value): if not self.head: return False - current = self.head while current: if current.get_value() == value: return True - current = current.get_next() - return False - def reverse_list(self, node, prev): - pass + def reverse_list(self): + prev = None + current = self.head + while current: + next = current.next_node + current.next_node = prev + prev = current + current = next + self.head = prev + + def __str__(self): + str = "" + current = self.head + while current: + str += f"{current.value.value}" + current = current.next_node + return str diff --git a/reverse/test_reverse.py b/reverse/test_reverse.py index 0199b1a20..9f0a03508 100644 --- a/reverse/test_reverse.py +++ b/reverse/test_reverse.py @@ -20,12 +20,12 @@ def test_contains(self): self.assertFalse(self.list.contains(1000)) def test_empty_reverse(self): - self.list.reverse_list(self.list.head, None) + self.list.reverse_list() self.assertEqual(self.list.head, None) def test_single_reverse(self): self.list.add_to_head(1) - self.list.reverse_list(self.list.head, None) + self.list.reverse_list() self.assertEqual(self.list.head.value, 1) def test_longer_reverse(self): @@ -35,7 +35,7 @@ def test_longer_reverse(self): self.list.add_to_head(4) self.list.add_to_head(5) self.assertEqual(self.list.head.value, 5) - self.list.reverse_list(self.list.head, None) + self.list.reverse_list() self.assertEqual(self.list.head.value, 1) self.assertEqual(self.list.head.get_next().value, 2) self.assertEqual(self.list.head.get_next().get_next().value, 3) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..9e7b403e4 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,17 @@ class RingBuffer: - def __init__(self, capacity): - pass + def __init__(self, capacity): + self.capacity = capacity + self.storage = [] + self.count = 0 - def append(self, item): - pass + def append(self, item): + if not self.storage: + [self.storage.append(None) for _ in range(self.capacity)] + if self.count + 1 > self.capacity: + self.count = 0 + self.storage[self.count] = item + self.count += 1 - def get(self): - pass \ No newline at end of file + def get(self): + items = [item for item in self.storage if item is not None] + return items \ No newline at end of file