Skip to content

Oliver Abreu | Python Data Structures - Sprint Challenge #418

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 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ While credit will be given for a functional solution, only optimal solutions wil

#### Passing the Sprint
Score ranges for a 1, 2, and 3 are shown in the rubric above. For a student to have _passed_ a sprint challenge, they need to earn an **at least 2** for all items on the rubric.
<!-- Initial Commit -->
31 changes: 31 additions & 0 deletions names/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

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)

def contains(self, value):
if self.value == value:
return True
if value < self.value:
if not self.left:
return False
else:
return self.left.contains(value)
else:
if not self.right:
return False
else:
return self.right.contains(value)
15 changes: 9 additions & 6 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import time
from binary_search_tree import BSTNode

start_time = time.time()

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

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

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)
search_tree = BSTNode("None")
for name in names_1:
search_tree.insert(name)
for name in names_2:
if search_tree.contains(name):
duplicates.append(name)

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

def reverse_list(self, node, prev):
pass
self.prev = None
current = self.head

while current is not None:
next_node = current.next_node
current.next_node = prev
prev = current
current = next_node
self.head = prev
16 changes: 13 additions & 3 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
class RingBuffer:
def __init__(self, capacity):
pass
self.capacity = capacity
self.current_index = 0
self.storage = []

def append(self, item):
pass
"""
When buffer is full, place the value of `item` at the current index;
otherwise, append the item to the end of the buffer
"""
if len(self.storage) == self.capacity:
self.storage[self.current_index] = item
self.current_index = (self.current_index + 1) % self.capacity
else:
self.storage.append(item)

def get(self):
pass
return self.storage