Skip to content

Chawnchristian #420

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

# Insert the given value into the tree
def insert(self, value):
node = BST(value)
if self.value is None:
self.value = value
return

elif value < self.value:
if self.left is None:
self.left = node
else:
self.left.insert(value)


elif value >= self.value:
if self.right is None:
self.right = node
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 and self.left:
return self.left.contains(target)

elif target > self.value and self.right:
return self.right.contains(target)

else:
return False
14 changes: 12 additions & 2 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import time
from binary_search_tree import BST



start_time = time.time()

Expand All @@ -12,11 +15,18 @@

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

search = BST('dup_name')
for name in names_1:
search.insert(name)
for name in names_2:
if search.contains(name):
duplicates.append(name)
# Replace the nested for loops below with your improvements
for name_1 in names_1:
'''for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)
duplicates.append(name_1)'''


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

def reverse_list(self, node, prev):
pass
if self.head is None:
return
if node.next_node is None:
self.head = node
node.next_node = prev
return
node_next = node.next_node
node.next_node = prev

self.reverse_list(node_next, node)
13 changes: 10 additions & 3 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
class RingBuffer:
def __init__(self, capacity):
pass
self.capacity = capacity
self.arr = []
self.index = 0


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

def get(self):
pass
return self.arr