diff --git a/Data_Structures_Answers.md b/Data_Structures_Answers.md index e39545492..67f081770 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(c) 2. What is the space complexity of your ring buffer's `append` function? - + O(c) 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) 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(logn) 8. What is the space complexity of your optimized code in `names.py`? + O(n) \ No newline at end of file diff --git a/names/names.py b/names/names.py index 586e8393e..c508a5b56 100644 --- a/names/names.py +++ b/names/names.py @@ -10,11 +10,17 @@ 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 = [] 7-8 secs +# for name_1 in names_1: +# for name_2 in names_2: +# if name_1 == name_2: +# duplicates.append(name_1) +# for i in names_1: 1.35 secs +# if i in names_2: +# duplicates.append(i) +# duplicates = [i for i in names_1 if i in names_2] 1.29 secs possible stretch solution using Lists for storing of the names + +duplicates = set(names_1).intersection(names_2) # .00399 secs sets use dictonaries so this solution can't be used for stretch end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 35fd33cac..c83dd6f2f 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -5,7 +5,24 @@ def __init__(self, capacity): self.storage = [None]*capacity def append(self, item): - pass + if self.current < self.capacity: + self.storage[self.current] = item + self.current += 1 + else: + self.current = 0 + self.storage[self.current] = item + self.current += 1 def get(self): - pass \ No newline at end of file + storage = [i for i in self.storage if i] + + return storage + +rb = RingBuffer(5) + +rb.append(5) +rb.append(6) +rb.append(3) + +print(rb.storage) +print(rb.get()) \ No newline at end of file