From 7ada7dd16f5c3beadc00a10a302058acf4c5d1b7 Mon Sep 17 00:00:00 2001 From: Eric Whitcomb Date: Sat, 29 Jun 2019 11:23:29 -0600 Subject: [PATCH 1/3] Completed ring buffer, tests pass --- ring_buffer/ring_buffer.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 35fd33cac..08cf98398 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -5,7 +5,11 @@ def __init__(self, capacity): self.storage = [None]*capacity def append(self, item): - pass + self.storage[self.current] = item + if self.current >= len(self.storage)-1: + self.current = 0 + else: + self.current += 1 def get(self): - pass \ No newline at end of file + return [v for v in self.storage if v is not None] \ No newline at end of file From 23d4437ea20159ac51f6ed4702b43b394226646d Mon Sep 17 00:00:00 2001 From: Eric Whitcomb Date: Sat, 29 Jun 2019 12:36:31 -0600 Subject: [PATCH 2/3] Completed names, running in under 10th of a second I used sets as that is how I would solve this problem. Not sure if this is what you were looking for however. This is what made sense to me. --- names/names.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/names/names.py b/names/names.py index 586e8393e..58950221c 100644 --- a/names/names.py +++ b/names/names.py @@ -10,13 +10,14 @@ names_2 = f.read().split("\n") # List containing 10000 names f.close() -duplicates = [] -for name_1 in names_1: - for name_2 in names_2: - if name_1 == name_2: - duplicates.append(name_1) +# duplicates = [] +# for name_1 in names_1: +# for name_2 in names_2: +# if name_1 == name_2: +# duplicates.append(name_1) + +duplicates = set(names_1) & set(names_2) end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") print (f"runtime: {end_time - start_time} seconds") - From 341b1c5fa6c32f5cf3cf9086fc5a8faf5a86155e Mon Sep 17 00:00:00 2001 From: Eric Whitcomb Date: Sat, 29 Jun 2019 12:49:36 -0600 Subject: [PATCH 3/3] Questions answered, sprint complete --- Data_Structures_Answers.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Data_Structures_Answers.md b/Data_Structures_Answers.md index e39545492..6cdc277e2 100644 --- a/Data_Structures_Answers.md +++ b/Data_Structures_Answers.md @@ -2,17 +2,34 @@ 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) + 6. What is the space complexity of the provided code in `names.py`? +O(2n) + 7. What is the runtime complexity of your optimized code in `names.py`? +O(logn) + 8. What is the space complexity of your optimized code in `names.py`? + +Not sure if this counts as O(n) or O(2n). Having the two lists is a bit +confusing. Final answer, O(2n) +