Skip to content

Finished buffer, reverse and names with answers to time complexities #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions names/name_tree.py
Original file line number Diff line number Diff line change
@@ -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()

34 changes: 29 additions & 5 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from name_tree import NameNode

start_time = time.time()

Expand All @@ -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 -----------
Expand Down
24 changes: 17 additions & 7 deletions reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions reverse/test_reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down
20 changes: 14 additions & 6 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -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
def get(self):
items = [item for item in self.storage if item is not None]
return items