Skip to content

Completed SC #441

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 2 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
29 changes: 29 additions & 0 deletions names/bst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

def insert(self, value):
if self.value <= value:
if self.right:
self.right.insert(value)
else:
new_node = BSTNode(value)
self.right = new_node
else:
if self.left:
self.left.insert(value)
else:
new_node = BSTNode(value)
self.left = new_node

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


12 changes: 7 additions & 5 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from bst import BSTNode

start_time = time.time()

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

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

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

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

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

def append(self, item):
pass
if len(self.list) == self.capacity:
self.list[self.counter] = item
if self.counter + 1 == self.capacity:
self.counter = 0
else:
self.counter +=1
else:
self.list.append(item)

def get(self):
pass
return self.list