Skip to content
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

Data Structures Sprint - MVP #415

Open
wants to merge 5 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
59 changes: 53 additions & 6 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,69 @@
import time
import os

file_names_1 = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'names_1.txt')
file_names_2 = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'names_2.txt')

start_time = time.time()

f = open('names_1.txt', 'r')
f = open(file_names_1, 'r')
names_1 = f.read().split("\n") # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
f = open(file_names_2, 'r')
names_2 = f.read().split("\n") # List containing 10000 names
f.close()

duplicates = [] # Return the list of duplicates in this data structure

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)
else:
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 self.value == target:
return True

if target < self.value:
if self.left is None:
return False
else:
return self.left.contains(target)
else:
if self.right is None:
return False
else:
return self.right.contains(target)
# 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)

bst = BSTNode(names_1[0])
for names1 in names_1:
bst.insert(names1)
for names2 in names_2:
if bst.contains(names2):
duplicates.append(names2)


end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
Expand Down
8 changes: 7 additions & 1 deletion reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ def contains(self, value):
return False

def reverse_list(self, node, prev):
pass
while node:
reverse_node = node.get_next()
node.next_node = prev
prev = node
node = reverse_node
self.head = prev
return node
12 changes: 9 additions & 3 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
class RingBuffer:
def __init__(self, capacity):
pass
self.capacity = capacity
self.current = -1
self.buffer_array = []

def append(self, item):
pass
if len(self.buffer_array) == self.capacity:
self.current = (self.current + 1) % self.capacity
self.buffer_array[self.current] = item
else:
self.buffer_array.append(item)

def get(self):
pass
return self.buffer_array