From f74bd4980646bb3e9b784aedf7179ac8288cad00 Mon Sep 17 00:00:00 2001 From: Kevin Stewart Date: Fri, 25 Sep 2020 21:18:22 -0400 Subject: [PATCH 1/5] Ring_buffer test complete --- ring_buffer/ring_buffer.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..c0962c0e8 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,15 @@ class RingBuffer: def __init__(self, capacity): - pass + self.capacity = capacity + self.current = -1 + self.buffer = [] def append(self, item): - pass + if len(self.buffer) == self.capacity: + self.current = (self.current + 1) % self.capacity + self.buffer[self.current] = item + else: + self.buffer.append(item) def get(self): - pass \ No newline at end of file + return self.buffer \ No newline at end of file From 9f16049629c63b2b8f6f371c603bb07466ef145c Mon Sep 17 00:00:00 2001 From: Kevin Stewart Date: Fri, 25 Sep 2020 22:30:02 -0400 Subject: [PATCH 2/5] Name error fixed: Names found from text files --- names/names.py | 8 ++++++-- ring_buffer/ring_buffer.py | 10 +++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/names/names.py b/names/names.py index ea158997f..c05795f84 100644 --- a/names/names.py +++ b/names/names.py @@ -1,12 +1,16 @@ import time +import os + +file_names_1 = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'names_1.txt') +file_names_2 = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'names_2.txt') start_time = time.time() -f = open('names_1.txt', 'r') +f = open(file_names_1, 'r') names_1 = f.read().split("\n") # List containing 10000 names f.close() -f = open('names_2.txt', 'r') +f = open(file_names_2, 'r') names_2 = f.read().split("\n") # List containing 10000 names f.close() diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index c0962c0e8..94b7b259b 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -2,14 +2,14 @@ class RingBuffer: def __init__(self, capacity): self.capacity = capacity self.current = -1 - self.buffer = [] + self.buffer_array = [] def append(self, item): - if len(self.buffer) == self.capacity: + if len(self.buffer_array) == self.capacity: self.current = (self.current + 1) % self.capacity - self.buffer[self.current] = item + self.buffer_array[self.current] = item else: - self.buffer.append(item) + self.buffer_array.append(item) def get(self): - return self.buffer \ No newline at end of file + return self.buffer_array From 674c2d93c29af76fb85c6338a6d71e868fa3d70e Mon Sep 17 00:00:00 2001 From: Kevin Stewart Date: Fri, 25 Sep 2020 22:51:59 -0400 Subject: [PATCH 3/5] Names loop returning duplicates under one second --- names/names.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/names/names.py b/names/names.py index c05795f84..e1d899286 100644 --- a/names/names.py +++ b/names/names.py @@ -16,11 +16,54 @@ duplicates = [] # Return the list of duplicates in this data structure +class BSTNode: + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + # Insert the given value into the tree + def insert(self, value): + if value < self.value: + if self.left is None: + self.left = BSTNode(value) + else: + self.left.insert(value) + else: + if self.right is None: + self.right = BSTNode(value) + else: + self.right.insert(value) + + # Return True if the tree contains the value + # False if it does not + def contains(self, target): + if self.value == target: + return True + + if target < self.value: + if self.left is None: + return False + else: + return self.left.contains(target) + else: + if self.right is None: + return False + else: + return self.right.contains(target) # Replace the nested for loops below with your improvements -for name_1 in names_1: - for name_2 in names_2: - if name_1 == name_2: - duplicates.append(name_1) +# for name_1 in names_1: +# for name_2 in names_2: +# if name_1 == name_2: +# duplicates.append(name_1) + +bst = BSTNode(names_1[0]) +for names1 in names_1: + bst.insert(names1) +for names2 in names_2: + if bst.contains(names2): + duplicates.append(names2) + end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") From 9c700a30eb3d9f20a9935d5bfac65e5923a1e5db Mon Sep 17 00:00:00 2001 From: Kevin Stewart Date: Fri, 25 Sep 2020 23:54:14 -0400 Subject: [PATCH 4/5] Reverse test complete --- reverse/reverse.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..4d4d09392 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -39,4 +39,10 @@ def contains(self, value): return False def reverse_list(self, node, prev): - pass + while node: + reverse_node = node.next_node + node.next_node = prev + prev = node + node = reverse_node + self.head = prev + return node From 9a505f665b2094b8abb23a25ac5bbb7e1eca7d15 Mon Sep 17 00:00:00 2001 From: Kevin Stewart Date: Mon, 28 Sep 2020 21:27:02 -0400 Subject: [PATCH 5/5] MVP --- reverse/reverse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reverse/reverse.py b/reverse/reverse.py index 4d4d09392..f4f2bfbd1 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -40,7 +40,7 @@ def contains(self, value): def reverse_list(self, node, prev): while node: - reverse_node = node.next_node + reverse_node = node.get_next() node.next_node = prev prev = node node = reverse_node