From 0b0bbfc0e926972989d50910424144033b37b93a Mon Sep 17 00:00:00 2001 From: Anthony Date: Sun, 27 Sep 2020 20:19:08 -0500 Subject: [PATCH 1/4] initial commit --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 7468701de..c2a4fdd4d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Sprint Challenge: Data Structures +## Initial commit + 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. ## Instructions From 04daee4d897838655fe6f47ce6b1e384b993cd49 Mon Sep 17 00:00:00 2001 From: Anthony Date: Sun, 27 Sep 2020 21:01:19 -0500 Subject: [PATCH 2/4] Ring Buffer is Complete will work on Names next --- README.md | 5 +++++ ring_buffer/ring_buffer.py | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c2a4fdd4d..8e605920a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,11 @@ ## 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 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. ## Instructions 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:] From 83a6685c7618e3b5d14c97b617ffe9b1bb02922b Mon Sep 17 00:00:00 2001 From: Anthony Date: Sun, 27 Sep 2020 22:13:00 -0500 Subject: [PATCH 3/4] name.py is completed and speed is amazing, on to reverse --- README.md | 2 +- names/bst.py | 38 ++++++++++++++++++++++++++++++++++++++ names/names.py | 21 +++++++++++++++------ 3 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 names/bst.py diff --git a/README.md b/README.md index 8e605920a..9b051225c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ================================================================================================ -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. +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 From 78218c02846e6aadfd8bf900928cc341c706a3ad Mon Sep 17 00:00:00 2001 From: Anthony Date: Sun, 27 Sep 2020 22:35:12 -0500 Subject: [PATCH 4/4] finished reverse.py and have Met MVP --- reverse/reverse.py | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) 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