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) + 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") - 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