Skip to content
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
117 changes: 117 additions & 0 deletions names/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
class BinarySearchTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

def insert(self, value):
#handles case where leaf node is found and value is smaller
if self.left is None and value <= self.value:
self.left = BinarySearchTree(value)
#handles case where leaf node is found and right is smaller
elif self.right is None and value > self.value:
self.right = BinarySearchTree(value)
#repeats first two checks for left node
elif value <= self.value:
self.left.insert(value)
#repeats first two checks for right node
elif value > self.value:
self.right.insert(value)


def contains(self, target):
if self.value == target:
return True
if target > self.value and self.right is not None:
return self.right.contains(target)
elif target <= self.value and self.left is not None:
return self.left.contains(target)
else:
return False


def get_max(self):
#simply goes to far right node and returns value
if self.right == None:
return self.value
else:
return self.right.get_max()

def for_each(self, cb):
cb(self.value)

if self.left:
self.left.for_each(cb)
if self.right:
self.right.for_each(cb)

# Print all the values in order from low to high
# Hint: Use a recursive, depth first traversal
def in_order_print(self, node):

if not node.left:
print(node.value)

if node.left and node.right:
node.in_order_print(node.left)
print(node.value)
node.in_order_print(node.right)
elif node.left:
node.in_order_print(node.left)
print(node.value)
elif node.right:
node.in_order_print(node.right)
print(node.value)


# Print the value of every node, starting with the given node,
# in an iterative depth first traversal
def dft_print(self, node):
stack = Stack()
stack.push(self)

current_node = self

while stack.len() > 0:

current_node = stack.pop()
print(current_node.value)
if current_node.left:
stack.push(current_node.left)
if current_node.right:
stack.push(current_node.right)

# Print the value of every node, starting with the given node,
# in an iterative breadth first traversal
def bft_print(self, node):
queue = Queue()
queue.enqueue(self)

current_node = None

while queue.len() > 0:

current_node = queue.dequeue()

if current_node.left:
queue.enqueue(current_node.left)
if current_node.right:
queue.enqueue(current_node.right)

print(current_node.value)


def pre_order_dft(self, node):
pass

def post_order_dft(self, node):
pass

bst = BinarySearchTree(1)
bst.insert(8)
bst.insert(5)
bst.insert(7)
bst.insert(6)
bst.insert(3)
bst.insert(4)
bst.insert(2)
23 changes: 18 additions & 5 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from binary_search_tree import BinarySearchTree

start_time = time.time()

Expand All @@ -11,12 +12,24 @@
f.close()

duplicates = []
for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)

#new solution would be at worst O(n), depending on balance of tree
names_bst = BinarySearchTree(names_1[0])
for name in names_1:
names_bst.insert(name)


for name in names_2:
if names_bst.contains(name):
duplicates.append(name)


#original solution was big O of n squared
# for name_1 in names_1:
# for name_2 in names_2:
# if name_1 == name_2:
# duplicates.append(name_1)

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"runtime: {end_time - start_time} seconds")

2 changes: 1 addition & 1 deletion names/names_2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9997,4 +9997,4 @@ Gerald Evans
Larry Tyler
Bennett Meza
Delaney Villarreal
Paola James
Paola James
29 changes: 25 additions & 4 deletions reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ def __init__(self, value=None, next_node=None):
# reference to the next node in the list
self.next_node = next_node

def __str__(self):
return f"{self.value}"
def get_value(self):
return self.value

Expand All @@ -24,15 +26,15 @@ 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
# get a reference to the node we're currently at; update this as we traverse the list
current = self.head
# check to see if we're at a valid node
# check to see if we're at a valid node
while current:
# return True if the current value we're looking at matches our target value
if current.get_value() == value:
Expand All @@ -43,5 +45,24 @@ def contains(self, value):
return False

def reverse_list(self):
# TO BE COMPLETED
pass

current_node = self.head
next = self.head
previous = None

while current_node:
#handles end of list by replacing the head with new 1 value
if not current_node.next_node:
self.head = current_node
#set next var to be next_node
next = next.next_node

#set current_nodes next_node to be previous
#starts with a none value
current_node.next_node = previous

#set previous var to be current_node
previous = current_node

#set current node to next_node
current_node = next
11 changes: 7 additions & 4 deletions reverse/test_reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_contains(self):
def test_empty_reverse(self):
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()
Expand All @@ -39,9 +39,12 @@ def test_longer_reverse(self):
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)

self.assertEqual(self.list.head.get_next().get_next().get_next().value, 4)
self.assertEqual(self.list.head.get_next().get_next().get_next().get_next().value, 5)






if __name__ == '__main__':
unittest.main()
unittest.main()
31 changes: 29 additions & 2 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,36 @@ def __init__(self, capacity):
self.capacity = capacity
self.current = 0
self.storage = [None]*capacity
self.length = 0

def append(self, item):
pass
#when capacity is met or exceeded by length
if self.length == self.capacity:
self.storage[self.current] = item
#handles case where current exceeds storage index by resetting current back to 0 which is the oldest item in storage
if self.current + 1 == self.capacity:
self.current = 0
else:
self.current += 1
self.increment_length()
else:
for index, value in enumerate(self.storage):
if value is None:
self.storage[index] = item
self.increment_length()
return

def increment_length(self):
if self.length == self.capacity:
return
else:
self.length += 1

def get(self):
pass
#if else statement optimizes print to only run O(n) list comprehension if the last item is None
#else it just returns O(1) by returning self.storage
if self.storage[self.capacity - 1] is None:
print_array = [item for item in self.storage if item]
return print_array
else:
return self.storage
2 changes: 1 addition & 1 deletion ring_buffer/test_ring_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ def test_ring_buffer(self):


if __name__ == '__main__':
unittest.main()
unittest.main()