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

Finished SC #432

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
45 changes: 45 additions & 0 deletions names/bst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'''
Binary search trees are a data structure that enforce an ordering over
the data they store. That ordering in turn makes it a lot more efficient
at searching for a particular piece of data in the tree. '''

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):
#compare the current value of the node(self.value)
if value < self.value:
#insert the value in left
if self.left is None:
# insert node value if no node in the left side
self.left = BSTNode(value)
else:
# repeat the process for the next node
self.left.insert(value)
if value >= self.value:
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 target ==self.value:
return True
if target < self.value:
if self.left is None:
return False
else:
return self.left.contains(target)

if target >=self.value:
if self.right is None:
return False
else:
return self.right.contains(target)
52 changes: 48 additions & 4 deletions names/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,63 @@
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)
# 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:
# if name_1 in names_2:
# duplicates.append(name_1)
from bst import BSTNode

# CREATE TREE WITH names_1 LIST
start_time = time.time()

tree = BSTNode(names_1[0])
for name in names_1:
tree.insert(name)

duplicates = [name for name in names_2 if tree.contains(name)]

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"runtime: {end_time - start_time} seconds")



# ---------- Stretch Goal -----------




# 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.

# LIST COMPREHENSION
# ~ 1.5 seconds which is def better than the original

# CREATE TREE WITH names_2 LIST (just in case order is better)
# ... it's not
start_time = time.time()

tree = BSTNode(names_2[0])
for name in names_2:
tree.insert(name)

duplicates = [name for name in names_1 if tree.contains(name)]

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"runtime: {end_time - start_time} seconds")




# USING SETS

13 changes: 12 additions & 1 deletion reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ def contains(self, value):
return False

def reverse_list(self, node, prev):
pass
#Points head to the node and set previous node None
prev = None
node = self.head
# continue till node is not empty
while node is not None:
next_node = node.next_node
node.next_node =prev
prev = node
node = next_node
self.head = prev


32 changes: 27 additions & 5 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
class RingBuffer:
def __init__(self, capacity):
pass

self.capacity = capacity
self.data = []
self.index =0


def append(self, item):
pass

# check if the buffer is full
if len(self.data) == self.capacity:
# overwrite the past value with new item
self.data.pop(self.index)
self.data.insert(self.index,item)
self.index = (self.index +1 ) % self.capacity
else:
self.data.append(item)

def get(self):
pass
return self.data

buffer = RingBuffer(3)
print(buffer.get())
buffer.append('a')
buffer.append('b')
buffer.append('c')
print(buffer.get())
buffer.append('d')
print(buffer.get())

buffer.append('e')
print(buffer.get())