diff --git a/Data_Structures_Answers.md b/Data_Structures_Answers.md index e39545492..29983c16b 100644 --- a/Data_Structures_Answers.md +++ b/Data_Structures_Answers.md @@ -1,18 +1,25 @@ 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*m)` -5. What is the runtime complexity of the provided code in `names.py`? +6) What is the space complexity of the provided code in `names.py`? + `O(n)` -6. What is the space complexity of the provided code in `names.py`? - -7. What is the runtime complexity of your optimized code in `names.py`? +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(n)` diff --git a/Pipfile b/Pipfile new file mode 100644 index 000000000..b723d0199 --- /dev/null +++ b/Pipfile @@ -0,0 +1,11 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] + +[requires] +python_version = "3.7" diff --git a/names/names.py b/names/names.py index 586e8393e..e1fdf773c 100644 --- a/names/names.py +++ b/names/names.py @@ -2,21 +2,71 @@ start_time = time.time() -f = open('names_1.txt', 'r') +f = open('names/names_1.txt', 'r') names_1 = f.read().split("\n") # List containing 10000 names f.close() -f = open('names_2.txt', 'r') +f = open('names/names_2.txt', 'r') 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) -end_time = time.time() -print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") -print (f"runtime: {end_time - start_time} seconds") +# # First pass solution provided +# def compare_first(list_1, list_2): +# duplicates = [] +# for item_1 in list_1: +# for item_2 in list_2: +# if item_1 == item_2: +# duplicates.append(item_1) +# return duplicates + +# duplicates = compare_first(names_1, names_2) + + +# # Second iteration +# def compare_second(list_1, list_2): +# duplicates = [] +# for item_1 in list_1: +# if item_1 in list_2: +# duplicates.append(item_1) +# return duplicates + + +# duplicates = compare_second(names_1, names_2) + +# # Third iteration +# def compare_third(list_1, list_2): +# duplicates = [] +# set_2 = set(list_2) +# for item_1 in list_1: +# if item_1 in set_2: +# duplicates.append(item_1) +# return duplicates + + +# duplicates = compare_third(names_1, names_2) + +# Fourth iteration +def compare_fourth(list_1, list_2): + duplicates = [] + list_1_dictionary = {} + + for item_1 in list_1: + list_1_dictionary[item_1] = item_1 + + for item_2 in list_2: + try: + if list_1_dictionary[item_2]: + duplicates.append(item_2) + except: + pass + return duplicates + + +duplicates = compare_fourth(names_1, 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..e5a872475 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,11 +1,15 @@ 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 - def append(self, item): - pass + def append(self, item): + self.storage[self.current] = item + self.current += 1 - def get(self): - pass \ No newline at end of file + if self.current == self.capacity: + self.current = 0 + + def get(self): + return [i for i in self.storage if i != None] diff --git a/ring_buffer/test_ring_buffer.py b/ring_buffer/test_ring_buffer.py index 6508c10af..66dcb1f89 100644 --- a/ring_buffer/test_ring_buffer.py +++ b/ring_buffer/test_ring_buffer.py @@ -1,6 +1,7 @@ import unittest from ring_buffer import RingBuffer + class RingBufferTests(unittest.TestCase): def setUp(self): self.buffer = RingBuffer(5) @@ -31,4 +32,4 @@ def test_ring_buffer(self): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()