diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..eaf91e2ac --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Sprint-Challenge--Data-Structures-Python.iml b/.idea/Sprint-Challenge--Data-Structures-Python.iml new file mode 100644 index 000000000..951c92867 --- /dev/null +++ b/.idea/Sprint-Challenge--Data-Structures-Python.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 000000000..105ce2da2 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..abf7b3965 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..2e14baebe --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..9661ac713 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/names/binary_search_tree.py b/names/binary_search_tree.py new file mode 100644 index 000000000..cb49da570 --- /dev/null +++ b/names/binary_search_tree.py @@ -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() +""" \ No newline at end of file diff --git a/names/names.py b/names/names.py index ea158997f..eb703a6af 100644 --- a/names/names.py +++ b/names/names.py @@ -1,4 +1,5 @@ import time +from binary_search_tree import BSTNode start_time = time.time() @@ -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 diff --git a/names/queue.py b/names/queue.py new file mode 100644 index 000000000..d70594ed2 --- /dev/null +++ b/names/queue.py @@ -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() diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..f14c4fbb2 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -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 \ No newline at end of file diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..2b5e92cb5 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -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 \ No newline at end of file + ''' 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()) \ No newline at end of file