Skip to content

Sprint Complete! #436

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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/Sprint-Challenge--Data-Structures-Python.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 147 additions & 0 deletions names/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
""" utilizing binary_search_tree
added stack and queue
also chose to use a doubly linked list for a two way traversal:
BFT(breadth first traversal)
as well as the DFT(depth fist traversal)
"""
from queue import Queue


class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

def insert(self, value):
if (self.left is None) & (self.right is None): # Empty tree use case
if value >= self.value:
self.right = BSTNode(value)
else:
self.left = BSTNode(value)
elif value < self.value: # value goes to left branch of root
if self.left is None:
self.left = BSTNode(value)
else:
self.left.insert(value)
else: # Value goes to the right branch of root
if self.right is None:
self.right = BSTNode(value)
else:
self.right.insert(value)

def contains(self, target):
if self.value == target:
return True
elif 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)

# Return the maximum value found in the tree
def get_max(self):
# go right until you cannot anymore
# return value
if self.right is None:
return self.value
else:
self.right.get_max()

# Call the function `fn` on the value of each node
def for_each(self, fn):
# call the function fn(value)
fn(self.value)
# base case - no children
if self.left is None and self.right is None:
return
# recursive case - 1 or more children
# go left, call fn(value) for each node
if self.left:
self.left.for_each(fn)
# go right, call fn(value) for each node
if self.right:
self.right.for_each(fn)

# Part 2 -----------------------
# Print all the values in order from low to high
# Hint: Use a recursive, depth first traversal
def in_order_print(self):
# if there is a tree
if self:
# if left child
if self.left:
# make left child
self.left.in_order_print()
# print the current root
print(self.value)
# if right child
if self.right:
# make the right child root
return self.right.in_order_print()

# Print the value of every node, starting with the given node,
# in an iterative breadth first traversal
def bft_print(self):
# instantiate a Queue
q = Queue()
# insert the value
q.enqueue(self)

# while length of q is greater than 0
while q.size > 0:
# pop off the top
top = q.dequeue()
# print it
print(top.value)
# if there is a left child
if top.left:
# add left child to queue
q.enqueue(top.left)
# if there is a right child
if top.right:
# add right child to queue
q.enqueue(top.right)

# Print the value of every node, starting with the given node,
# in an iterative depth first traversal
def dft_print(self):
# if a tree exists
if self:
# print the current value (as it's the first traversal)
print(self.value)
# if there is a left child
if self.left:
# re-run function with left child as root of tree
self.left.dft_print()
# if there is a right child
if self.right:
# re-run function with right child as root of tree
self.right.dft_print()

bst = BSTNode(1)

bst.insert(8)
bst.insert(5)
bst.insert(7)
bst.insert(6)
bst.insert(3)
bst.insert(4)
bst.insert(2)

bst.bft_print()
bst.dft_print()

""" Not using these, hope that's ok!
#print("elegant methods")
#print("pre order")
#bst.pre_order_dft()
#print("in order")
#bst.in_order_print()
#print("post order")
#bst.post_order_dft()
"""
18 changes: 14 additions & 4 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from binary_search_tree import BSTNode

start_time = time.time()

Expand All @@ -13,15 +14,24 @@
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)
# label bst as the first name in names_1 txt file
bst = BSTNode(names_1[0])

"""
we are going to insert the name for the name in first
txt file if name isn't equal to first name in name_1 index
"""
[bst.insert(name) for name in names_1 if name != names_1[0]]
# checking for duplicates - returned 64
[duplicates.append(name) for name in names_2 if bst.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")

# my runtime shows from PowerShell:
# runtime: 0.09596920013427734 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
Expand Down
18 changes: 18 additions & 0 deletions names/queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Queue:
def __init__(self):
self.size = 0
self.storage = []

def __len__(self):
return self.size

def enqueue(self, value):
self.storage.insert(0, value)
self.size += 1

def dequeue(self):
if self.size == 0:
return None
else:
self.size -= 1
return self.storage.pop()
25 changes: 18 additions & 7 deletions reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,36 @@ def __init__(self):

def add_to_head(self, value):
node = Node(value)

if self.head is not None:
node.set_next(self.head)

self.head = node

def contains(self, value):
# if not the head, return False
if not self.head:
return False

# set current = to head
current = self.head

while current:
# if the value of head = value
if current.get_value() == value:
return True

# move on to the next
current = current.get_next()

return False

def reverse_list(self, node, prev):
pass
# while current node exists
if node:
# store the next node
next_node = node.next_node
# set the prev node to the next node
node.next_node = prev
# run it all through again, with the new root
self.reverse_list(next_node, node)
# if node is None
else:
# the previous node is the head
self.head = prev

# this didn't push through Git the first time, comment to try again
26 changes: 23 additions & 3 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
class RingBuffer:
counter = 0
def __init__(self, capacity):
pass
self.capacity = capacity
self.storage = []
self.index = 0


def append(self, item):
pass
if len(self.storage) == self.capacity:
self.storage[self.index] = item
else:
self.storage.append(item)
# add item to the index after the one it's at
self.index = (self.index + 1) % self.capacity


def get(self):
pass
''' Returns all elements in the buffer
as a list in their given order
'''
return self.storage

buffer = RingBuffer(3)
buffer.append('g')
buffer.append('a')
buffer.append('s')
buffer.append('k')
print(buffer.get())