From 957c22e0385942f0d3e81ce5d245499ab15097cf Mon Sep 17 00:00:00 2001 From: john Date: Sun, 2 Aug 2020 18:31:25 -0700 Subject: [PATCH 1/3] names.py - sub second fastest --- names/names.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/names/names.py b/names/names.py index ea158997f..775b75452 100644 --- a/names/names.py +++ b/names/names.py @@ -12,15 +12,30 @@ duplicates = [] # Return the list of duplicates in this data structure +# Polynomial Runtime - O(n^2) # 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) + +# Linear Time O(n) - Runtime less than 2 seconds +for name in names_1: + if name in names_2: + duplicates.append(name) + +# List Comprehension - Runtime less than 2 seconds +# duplicates = [name for name in names_1 if name in names_2] + +# Set Functions to list - Runtime less than 1 second +# duplicates = list(set(names_1) & set(names_2)) + +# set().intersection() - Runtime less than 1 second +# duplicates = set(names_1).intersection(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") +print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") +print(f"runtime: {end_time - start_time} seconds") # ---------- Stretch Goal ----------- # Python has built-in tools that allow for a very efficient approach to this problem From 8b488ba3cea0c2d8ad288070760f3384036ba768 Mon Sep 17 00:00:00 2001 From: john Date: Sun, 2 Aug 2020 18:59:21 -0700 Subject: [PATCH 2/3] ring_buffer.py - test pass --- ring_buffer/ring_buffer.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..55d61ce3a 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,40 @@ class RingBuffer: def __init__(self, capacity): - pass + self.index = 0 + self.capacity = capacity + self.data = [] def append(self, item): - pass + + # check if full, if so replace oldest index + if len(self.data) == self.capacity: + self.data[self.index] = item + + # if not full, append item to list + else: + self.data.append(item) + + # keep track of oldest append + # index increment +1 up to capacity, then restarts to 0 + self.index = (self.index + 1) % self.capacity def get(self): - pass \ No newline at end of file + return self.data + + +buffer = RingBuffer(3) + +buffer.append('a') +buffer.append('b') +buffer.append('c') + +print(buffer.get()) + +buffer.append('d') + +print(buffer.get()) + +buffer.append('e') +buffer.append('f') + +print(buffer.get()) From 9b7a509c59fd0cf0d0040c3dd5f3f3d03aa21a91 Mon Sep 17 00:00:00 2001 From: john Date: Sun, 2 Aug 2020 20:10:23 -0700 Subject: [PATCH 3/3] reverse.py - test passed --- reverse/reverse.py | 48 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..1c3b04f29 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -12,6 +12,10 @@ def get_next(self): def set_next(self, new_next): self.next_node = new_next + def __str__(self): + return f"Value: {self.value}, Next_Node: {self.next_node}" + + class LinkedList: def __init__(self): self.head = None @@ -24,6 +28,9 @@ def add_to_head(self, value): self.head = node + print("ADD TO HEAD") + print(f"Head: {self.head}") + def contains(self, value): if not self.head: return False @@ -39,4 +46,43 @@ def contains(self, value): return False def reverse_list(self, node, prev): - pass + + # check if LL is empty + if self.head is None: + return + + # store current node + cur_node = node + # store next node + next_node = cur_node.next_node + # make next node the prev + cur_node.set_next(prev) + + # loop through all nodes until None + while next_node is not None: + + # store previous node + prev_node = cur_node + + # store current node + cur_node = next_node + + # store next node of current node + next_node = cur_node.get_next() + + # reverse cur_node pointer to previous node + cur_node.set_next(prev_node) + + # set head to new current node + self.head = cur_node + + print("REVERSE LIST") + print(f"Head: {self.head}") + + +linked_list = LinkedList() + +linked_list.add_to_head(3) +linked_list.add_to_head(2) +linked_list.add_to_head(1) +linked_list.reverse_list(linked_list.head, None)