From b92512464d6901eb4fa907c5e356cc7c662deef4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 14 Jun 2019 08:02:26 -0700 Subject: [PATCH 1/5] initial commit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 30881a7fb..700d77e83 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # 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. ## Instructions From 3d2ae390f92590a47baeebb943bd65f466a00eee Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 14 Jun 2019 08:28:56 -0700 Subject: [PATCH 2/5] finished ring_buffer --- ring_buffer/ring_buffer.py | 39 ++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 35fd33cac..8a17f71c6 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,11 +1,34 @@ +# buffer does not grow +# last element is replaced by new element + + class RingBuffer: - def __init__(self, capacity): - self.capacity = capacity - self.current = 0 - self.storage = [None]*capacity + def __init__(self, capacity): + self.capacity = capacity + self.current = 0 + self.storage = [None]*capacity + + # adds a new element and deletes the last element + def append(self, item): + # print(self.current) + # print(self.storage) + # replaces the item for the "current" index + self.storage[self.current] = item + # check that our current position is not beyond the lists length + if self.current < self.capacity - 1: + # increment our position + self.current += 1 + # if we are past the length of the array set to index[0] + else: + self.current = 0 - def append(self, item): - pass + # returns all of the elements in the buffer + # does not return None values - def get(self): - pass \ No newline at end of file + def get(self): + returnArr = [] + for i in self.storage: + if i is not None: + returnArr.append(i) + # print(returnArr) + return returnArr From 963eedcbaf59fe4284572863a74544470208125f Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 14 Jun 2019 09:21:55 -0700 Subject: [PATCH 3/5] finished names --- names/names.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/names/names.py b/names/names.py index 586e8393e..ace6b77d6 100644 --- a/names/names.py +++ b/names/names.py @@ -11,12 +11,29 @@ f.close() duplicates = [] -for name_1 in names_1: - for name_2 in names_2: - if name_1 == name_2: - duplicates.append(name_1) -end_time = time.time() -print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") -print (f"runtime: {end_time - start_time} seconds") +# loops through names 2 for every name in name_1 +# loops through 10,000 names 10,000 times + +# for name_1 in names_1: +# for name_2 in names_2: +# if name_1 == name_2: +# duplicates.append(name_1) + +# runtime: 6.5317909717559814 seconds + +# loops through names_2 and checks if it is in names_1_set +# sets have no index or slicing and can hold any type of data +# membership tests are faster in a set because they use a hash table +# so a set will reduce one of our for loops to a O(1) operation +names_1_set = set(names_1) +for name in names_2: + if name in names_1_set: + duplicates.append(name) + +# runtime: 0.003815174102783203 seconds + +end_time = time.time() +print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") +print(f"runtime: {end_time - start_time} seconds") From d423fd0d8813c99efa533318a9bf554969dbef7d Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 14 Jun 2019 09:22:16 -0700 Subject: [PATCH 4/5] finished questions --- Data_Structures_Answers.md | 15 ++++++++------- ring_buffer/ring_buffer.py | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Data_Structures_Answers.md b/Data_Structures_Answers.md index e39545492..05b64e319 100644 --- a/Data_Structures_Answers.md +++ b/Data_Structures_Answers.md @@ -1,18 +1,19 @@ Add your answers to the questions below. 1. What is the runtime complexity of your ring buffer's `append` method? - +O(1) 2. What is the space complexity of your ring buffer's `append` function? - +O(1) 3. What is the runtime complexity of your ring buffer's `get` method? - +O(n) 4. What is the space complexity of your ring buffer's `get` method? - +O(n) 5. What is the runtime complexity of the provided code in `names.py`? - +O(n^2) - nested for loop 6. What is the space complexity of the provided code in `names.py`? - +O(n) 7. What is the runtime complexity of your optimized code in `names.py`? - +O(n) 8. What is the space complexity of your optimized code in `names.py`? +O(1) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 8a17f71c6..7a68a5499 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -12,6 +12,7 @@ def __init__(self, capacity): def append(self, item): # print(self.current) # print(self.storage) + # replaces the item for the "current" index self.storage[self.current] = item # check that our current position is not beyond the lists length From e6015f2b881bcf6ed0725cc858bb9bcfbc0241a6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 14 Jun 2019 11:06:21 -0700 Subject: [PATCH 5/5] finished MVP --- README.md | 2 +- ring_buffer/ring_buffer.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 700d77e83..7c77ad7c4 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Open up the `Data_Structures_Answers.md` file. This is where you'll jot down you ### Stretch Problems -1. Say your code from `names.py` is to run on an embedded computer with very limited RAM. Because of this, memory is extremely constrained and you are only allowed to store names in arrays (i.e. Python lists). How would you go about optimizing the code under these conditions? Try it out and compare your solution to the original runtime. (If this solution is less efficient than your original solution, include both and label the strech solution with a comment) +1. Say your code from `names.py` is to run on an embedded computer with very limited RAM. Because of this, memory is extremely constrained and you are only allowed to store names in arrays (i.e. Python lists). How would you go about optimizing the code under these conditions? Try it out and compare your solution to the original runtime. (If this solution is less efficient than your original solution, include both and label the stretch solution with a comment) ### Rubric diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 7a68a5499..6a5719b85 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -8,10 +8,10 @@ def __init__(self, capacity): self.current = 0 self.storage = [None]*capacity - # adds a new element and deletes the last element + # adds a new element and deletes the last element if length == capacity def append(self, item): - # print(self.current) - # print(self.storage) + print(self.current) + print(self.storage) # replaces the item for the "current" index self.storage[self.current] = item @@ -31,5 +31,5 @@ def get(self): for i in self.storage: if i is not None: returnArr.append(i) - # print(returnArr) + print(returnArr) return returnArr