diff --git a/README.md b/README.md index 7468701de..9b051225c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,13 @@ # Sprint Challenge: Data Structures -In this week's Sprint you implemented some classic and fundamental data structures and learned about how to go about evaluating their respective runtimes and performance. This Sprint Challenge aims to assess your comfort with these topics through exercises that build on the data structures you implemented and the algorithmic intuition you've started to build up. +## Initial commit + +================================================================================================ +======https://github.com/LambdaSchool/Sprint-Challenge--Data-Structures-Python/pull/424========= +================================================================================================ + + +In this week's Sprint you implemented some classic and fundamental data structures and learned about how to go about evaluating their respective run times and performance. This Sprint Challenge aims to assess your comfort with these topics through exercises that build on the data structures you implemented and the algorithmic intuition you've started to build up. ## Instructions diff --git a/names/bst.py b/names/bst.py new file mode 100644 index 000000000..a76afa586 --- /dev/null +++ b/names/bst.py @@ -0,0 +1,38 @@ +class BSTNode: + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + ########## RETURNING INSERT VALUES ########## + + def insert(self, value): + if value < self.value: + if self.left is None: + self.left = BSTNode(value) + else: + self.left.insert(value) + elif value >= self.value: + if self.right is None: + self.right = BSTNode(value) + else: + self.right.insert(value) + + +########## RETURNING TRUE VALUE ########## +########## IF DOES NOT ITS FALSE ######### + + + def contains(self, target): + if self.value == target: + return True + if target < self.value: + if not self.left: + return False + else: + return self.left.contains(target) + else: + if not self.right: + return False + else: + return self.right.contains(target) diff --git a/names/names.py b/names/names.py index ea158997f..211154ab0 100644 --- a/names/names.py +++ b/names/names.py @@ -1,4 +1,5 @@ import time +from bst import BSTNode start_time = time.time() @@ -11,16 +12,24 @@ f.close() duplicates = [] # Return the list of duplicates in this data structure - +bst = BSTNode("Mike") # 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) + bst.insert(name_1) + +for name_2 in names_2: + if name_2 not in duplicates: + if bst.contains(name_2): + duplicates.append(name_2) end_time = time.time() -print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") -print (f"runtime: {end_time - start_time} seconds") +print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") +print(f"runtime: {end_time - start_time} seconds") + +########## SPEED TIMES ########## +########## BEFORE runtime: 6.172563314437866 seconds ########## +########## AFTER runtime: 0.1124579906463623 seconds ########## +########## SHAVED NEARLY 6 SECONDS OFF TIME ########## # ---------- Stretch Goal ----------- # Python has built-in tools that allow for a very efficient approach to this problem diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..4f9ded2b1 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -1,4 +1,5 @@ class Node: + ########## INITIALIZE CONSTRUCTOR ########## def __init__(self, value=None, next_node=None): self.value = value self.next_node = next_node @@ -13,9 +14,12 @@ def set_next(self, new_next): self.next_node = new_next class LinkedList: + ########## INITIALIZING THE HEAD ########## def __init__(self): self.head = None - + + + ########## NODE INSERTED IN FRONT ########## def add_to_head(self, value): node = Node(value) @@ -24,19 +28,49 @@ def add_to_head(self, value): self.head = node + + + ########## TRUE OR FALSE ########## def contains(self, value): if not self.head: return False - + + + ########## LOOPING THROUGH NODES ########## current = self.head while current: + ########## MAKING SURE THIS IS THE NODE WE LOOKED FOR ########## if current.get_value() == value: return True current = current.get_next() return False + + + ########## 3 POINTERS == PREV/NULL == HEAD & NEXT ########## + def reverse_list(self, x=None, y=None): + if self.head == None: + return None + current = self.head + prev = None + + while current != None: + next = current.next_node + + current.next_node = prev + + prev = current + current = next + + self.head = prev + + + ########## PRINTING LINKEDLIST ########## + def print_list(self): + current = self.head + while(current): + print(current.value) + current = current.next_node - def reverse_list(self, node, prev): - pass diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..f89060ed2 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,20 @@ class RingBuffer: def __init__(self, capacity): - pass + self.capacity = capacity + self.buffer = [] # SIZE/COUNT ########## + self.current = 0 # EMPTY ARRAY - OBJECT ########## def append(self, item): - pass + ########## APPENDING AN ELEMENT ########## + if len(self.buffer) < self.capacity: + self.buffer.append(item) + elif len(self.buffer) == self.capacity: + self.buffer[self.current] = item + # OVERWRITE OLDEST ELEMENT ########## + self.current = (self.current + 1) % self.capacity def get(self): - pass \ No newline at end of file + ########## RETURNING A LIST OF ELEMENTS [OLDEST-NEWEST] ########## + if self.buffer is not None: + # SLICED WITH THE COLON ON THE END IS FOR TESTING AND IF IT IS IN THE FRONT IT WILL FAIL ########## + return self.buffer[:self.current]+self.buffer[self.current:]