Skip to content

Chris sprint2 - Data Structure Sprint challenge with Stretch Completed. #422

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 9 commits 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
53 changes: 53 additions & 0 deletions names/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -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
30 changes: 27 additions & 3 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 @@ -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")
Expand All @@ -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")
7 changes: 6 additions & 1 deletion reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 16 additions & 4 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -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
return self.storage