From 1e7975a9a78f3e780d82f351b812968813d536ff Mon Sep 17 00:00:00 2001 From: Thierry Joux Date: Fri, 20 Sep 2019 08:48:20 -0700 Subject: [PATCH 1/3] Completed ring_buffer class --- ring_buffer/ring_buffer.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 35fd33cac..5a05c51a4 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -4,8 +4,11 @@ def __init__(self, capacity): self.current = 0 self.storage = [None]*capacity + # when full always append by replacing the oldest information - not full replace the first preset None seen def append(self, item): - pass + self.storage[self.current % self.capacity] = item + self.current = (self.current + 1) % self.capacity + # return everything from buffer, in order, ignoring None values def get(self): - pass \ No newline at end of file + return [item for item in self.storage if item is not None] \ No newline at end of file From d934c26818ce6195fd3204c0b25581f3a2619a1e Mon Sep 17 00:00:00 2001 From: Thierry Joux Date: Fri, 20 Sep 2019 10:17:42 -0700 Subject: [PATCH 2/3] Completed names.py + STRETCH --- names/binary_search_tree.py | 44 +++++++++++++++++++++++++++++++++++++ names/names.py | 38 +++++++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 names/binary_search_tree.py diff --git a/names/binary_search_tree.py b/names/binary_search_tree.py new file mode 100644 index 000000000..0490a9913 --- /dev/null +++ b/names/binary_search_tree.py @@ -0,0 +1,44 @@ +class BinarySearchTree: + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + + def insert(self, value): + + if value == self.value: + return + + if value < self.value: + if not self.left: + self.left = BinarySearchTree(value) + else: + self.left.insert(value) + + elif value > self.value: + if not self.right: + self.right = BinarySearchTree(value) + else: + self.right.insert(value) + + + + + def contains(self, target): + + if self.value == target: + return True + + if target < self.value: + + if self.left: + return self.left.contains(target) + else: + return False + + else: + if self.right: + return self.right.contains(target) + else: + return False \ No newline at end of file diff --git a/names/names.py b/names/names.py index 586e8393e..73c8b31ed 100644 --- a/names/names.py +++ b/names/names.py @@ -1,4 +1,5 @@ import time +from binary_search_tree import BinarySearchTree start_time = time.time() @@ -10,11 +11,38 @@ 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) +########################################################################### +# Binary Tree Search solution (insertion complexity: O(h(eight of tree)), +# comparison: n * O(h(eight of tree)) +########################################################################### + +# Setup binary tree and insert all names of names_1 +bst = BinarySearchTree(names_1[0]) + +for i in range(1,len(names_1)): + bst.insert(names_1[i]) + +# look up for duplicates +duplicates=[] +for name in names_2: + if bst.contains(name): + duplicates.append(name) + +##################################### +# Original solution (runtime: 5.5 s.) +# complexity: O(n**2) +###################################### +# duplicates = [] +# for name_1 in names_1: +# for name_2 in names_2: +# if name_1 == name_2: +# duplicates.append(name_1) + +######## ############################## STRETCH ################################### +# STRETCH - only Python lists - No extra storage for binary tree (runtime: 1.11 s.) +#################################################################################### +# +# duplicates = [name for name in names_1 if name in names_2] end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") From b8cfbad5476da653fcd86f6998b7b086dffc8238 Mon Sep 17 00:00:00 2001 From: Thierry Joux Date: Fri, 20 Sep 2019 10:47:55 -0700 Subject: [PATCH 3/3] Completed reverse_list function --- reverse/reverse.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/reverse/reverse.py b/reverse/reverse.py index 95ed9d1d4..c23f79b88 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -43,5 +43,20 @@ def contains(self, value): return False def reverse_list(self): - # TO BE COMPLETED - pass \ No newline at end of file + + if self.head is None or self.head.next_node is None: + return + + #reverting direction for all linked list pointers + previous = None + current = self.head + after = current.next_node + + while after is not None: + current.next_node = previous + previous = current + current = after + after = current.next_node + + current.next_node = previous + self.head = current \ No newline at end of file