diff --git a/names/names_stretch_binary.py b/names/names_stretch_binary.py new file mode 100644 index 000000000..96dfc0b66 --- /dev/null +++ b/names/names_stretch_binary.py @@ -0,0 +1,49 @@ +import time + +def binary_search(arr, target): + if len(arr) == 0: + return -1 + if arr[0] > target: + return -1 + if arr[len(arr) - 1] < target: + return -1 + low = 0 + high = len(arr)-1 + middle = int((high-low)/2 + low) + while high > low: + middle = int((high + low)/2) + if target == arr[middle]: + return middle + elif target < arr[middle]: + high = middle + else: + low = middle + 1 + return -1 # not found + +start_time = time.perf_counter() + +f = open('names_1.txt', 'r') +names_1 = f.read().split("\n") # List containing 10000 names +f.close() + +f = open('names_2.txt', 'r') +names_2 = f.read().split("\n") # List containing 10000 names +f.close() + +duplicates = [] # Return the list of duplicates in this data structure + +# Replace the nested for loops below with your improvements +names_1.sort() +for name_2 in names_2: + if binary_search(names_1, name_2): + duplicates.append(name_2) + +end_time = time.perf_counter() +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 +# What's the best time you can accomplish? There are no restrictions on techniques or data + +# structures, but you may not import any additional libraries that you did not write yourself. \ No newline at end of file diff --git a/names/names_stretch_intersection.py b/names/names_stretch_intersection.py new file mode 100644 index 000000000..cced00b90 --- /dev/null +++ b/names/names_stretch_intersection.py @@ -0,0 +1,25 @@ +import time + +start_time = time.perf_counter() + +f = open('names_1.txt', 'r') +names_1 = f.read().split("\n") # List containing 10000 names +f.close() + +f = open('names_2.txt', 'r') +names_2 = f.read().split("\n") # List containing 10000 names +f.close() + +names_1 = set(names_1) +names_2 = set(names_2) +duplicates = list(names_1.intersection(names_2)) + +end_time = time.perf_counter() +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 +# What's the best time you can accomplish? There are no restrictions on techniques or data + +# structures, but you may not import any additional libraries that you did not write yourself. \ No newline at end of file diff --git a/reverse/reverse.py b/reverse/reverse.py index 95ed9d1d4..a53647c2a 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -43,5 +43,18 @@ 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: + return [None] + + prevNode = None + current = self.head + nextNode = current.get_next() + + while current: + current.set_next(prevNode) + prevNode = current + current = nextNode + if nextNode: + nextNode = nextNode.get_next() + + self.head = prevNode \ No newline at end of file diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index ef88f0d6b..740f74bb0 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -2,31 +2,52 @@ class RingBuffer: - def __init__(self, capacity): - self.capacity = capacity - self.current = None - self.storage = DoublyLinkedList() - - def append(self, item): - pass - - def get(self): - # Note: This is the only [] allowed - list_buffer_contents = [] - - # TODO: Your code here - - return list_buffer_contents + def __init__(self, capacity): + self.capacity = capacity + self.current = None + self.storage = DoublyLinkedList() + + def __len__(self): + return self.storage.length + + def append(self, item): + # while there space is available, add to tail, and update current + if self.storage.length < self.capacity: + self.storage.add_to_tail(item) + self.current = self.storage.tail + # if theres no more space + if self.storage.length == self.capacity: + # update current value + self.current.value = item + # if we're currently at the tail + if self.current is self.storage.tail: + # go back to head + self.current = self.storage.head + else: + # else, go to the next + self.current = self.current.next + + def get(self): + # Note: This is the only [] allowed + list_buffer_contents = [] + + # TODO: Your code here + node = self.storage.head + while node != None: + if node.value is not None: + list_buffer_contents.append(node.value) + node = node.next + return list_buffer_contents # ----------------Stretch Goal------------------- - - class ArrayRingBuffer: - def __init__(self, capacity): - pass - - def append(self, item): - pass - - def get(self): - pass + def __init__(self, capacity): + self.storage = RingBuffer(capacity) + for _ in range(0, capacity): + self.storage.append(None) + + def append(self, item): + return self.storage.append(item) + + def get(self): + return self.storage.get() \ No newline at end of file