From fa43c4f83a0f3877b785bb7f438308e4994ab312 Mon Sep 17 00:00:00 2001 From: Cristina Altreche Date: Sun, 27 Sep 2020 21:20:29 -0400 Subject: [PATCH 1/6] setup max capacity and storage for ringbuffer --- ring_buffer/ring_buffer.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..afe1cc94d 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,6 +1,8 @@ class RingBuffer: def __init__(self, capacity): - pass + # setup max capacity and storage + self.capacity = capacity + self.storage = [] def append(self, item): pass From d45041b03e5c9f3fda36b3484ad876106f37c3e7 Mon Sep 17 00:00:00 2001 From: Cristina Altreche Date: Sun, 27 Sep 2020 21:25:44 -0400 Subject: [PATCH 2/6] initialized size, head, and tail for RingBuffer --- ring_buffer/ring_buffer.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index afe1cc94d..13dcd1f98 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -3,6 +3,12 @@ def __init__(self, capacity): # setup max capacity and storage self.capacity = capacity self.storage = [] + # setup size to zero + self.size = 0 + # instantiate the tail and head to insert and remove oldest and newest + self.tail = -1 # to equal newest element + self.head = 0 # to equal oldest element + def append(self, item): pass From 5214bb6e2195d88956b649596f73dc50ecb92a5c Mon Sep 17 00:00:00 2001 From: Cristina Altreche Date: Sun, 27 Sep 2020 21:44:38 -0400 Subject: [PATCH 3/6] def get complete --- ring_buffer/ring_buffer.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 13dcd1f98..c5aa486bc 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -11,7 +11,14 @@ def __init__(self, capacity): def append(self, item): - pass + # first check if the storage is full + if self.size == self.capacity + # if full enqueue the item to the tail + + # decrease the size + + # otherwise enqueue the item def get(self): - pass \ No newline at end of file + # returns all of the elements in the buffer in a list + return self.storage \ No newline at end of file From f9e8356417dea2172734e6ff04bf4eaaed5b854a Mon Sep 17 00:00:00 2001 From: Cristina Altreche Date: Sun, 27 Sep 2020 22:05:15 -0400 Subject: [PATCH 4/6] ring buffer completed --- ring_buffer/ring_buffer.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index c5aa486bc..12f9bfec4 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -3,22 +3,41 @@ def __init__(self, capacity): # setup max capacity and storage self.capacity = capacity self.storage = [] - # setup size to zero + # initialize ring buffer size to zero self.size = 0 # instantiate the tail and head to insert and remove oldest and newest - self.tail = -1 # to equal newest element - self.head = 0 # to equal oldest element + self.tail = -1 # to equal newest element + self.head = 0 # to equal oldest element +# https://www.oreilly.com/library/view/python-cookbook/0596001673/ch05s19.html +# class _ _Full: +# """ class that implements a full buffer """ +# def append(self, x): +# """ Append an element overwriting the oldest one. """ +# self.data[self.cur] = x +# self.cur = (self.cur+1) % self.max def append(self, item): # first check if the storage is full - if self.size == self.capacity - # if full enqueue the item to the tail + if self.size == self.capacity: + # replace index at self.head + self.storage[self.head] = item - # decrease the size + # update head to be oldest item in storage. When this leaves no remainders start back at 0 + self.head = (self.head + 1) % self.capacity - # otherwise enqueue the item + # otherwise if storage is not full enqueue item + else: + self.enqueue(item) def get(self): # returns all of the elements in the buffer in a list - return self.storage \ No newline at end of file + return self.storage + + # added enqueue to insert values at end of RingBuffer. + + def enqueue(self, value): + # inserts the value at the end of the storage + self.storage.append(value) + # increases size by 1 + self.size += 1 From edf191d25632d588ab85cf47b2c14b6f28cee901 Mon Sep 17 00:00:00 2001 From: Cristina Altreche Date: Sun, 27 Sep 2020 22:49:18 -0400 Subject: [PATCH 5/6] names finished --- names/names.py | 51 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/names/names.py b/names/names.py index ea158997f..d8855cc98 100644 --- a/names/names.py +++ b/names/names.py @@ -13,14 +13,53 @@ duplicates = [] # Return the list of duplicates in this data structure # 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) + +# runtime: 3.0849990844726562 seconds +# duplicates = [ name for name in names_1 if name in names_2 ] # Return the list of duplicates in this data structure + + +# # 12.839998483657837 seconds +# for name_1 in names_1: +# for name_2 in names_2: +# if name_1 == name_2: +# duplicates.append(name_1) + + +# runtime: 3.0630006790161133 seconds +class Stack: + def __init__(self): + self.size = 0 + self.storage = [] + + def __len__(self): + return self.size + + def push(self, value): + self.size += 1 + self.storage.append(value) + + def pop(self): + if self.size == 0: + return None + self.size -= 1 + return self.storage.pop() + + +# Create a new Stack +search = Stack() + +# for every name in the FIRST array (name_1), add that to stack +for name in names_1: + search.push(name) + +# for each item in the stack, if it is in the SECOND array(name_2) append to duplicates +for item in search.storage: + if item in names_2: + duplicates.append(item) 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 a9adb538408046cc36e8eee320e8e896ad5da33f Mon Sep 17 00:00:00 2001 From: Cristina Altreche Date: Sun, 27 Sep 2020 23:22:28 -0400 Subject: [PATCH 6/6] MVP met --- reverse/reverse.py | 47 +++++++++++++++++++++++++++++++++++++- ring_buffer/ring_buffer.py | 6 ++--- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..1a01ccb66 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -12,6 +12,7 @@ def get_next(self): def set_next(self, new_next): self.next_node = new_next + class LinkedList: def __init__(self): self.head = None @@ -38,5 +39,49 @@ def contains(self, value): return False + +# # Function to reverse the linked list +# def reverse(self): +# prev = None +# current = self.head +# while(current is not None): +# next = current.next +# current.next = prev +# prev = current +# current = next +# self.head = prev + def reverse_list(self, node, prev): - pass + prev = None + node = self.head + while(node is not None): + next_node = node.next_node + node.next_node = prev + prev = node + node = next_node + self.head = prev + + def push(self, new_data): + new_node = Node(new_data) + new_node.next_node = self.head + self.head = new_node + + def printList(self): + temp = self.head + while(temp): + print(temp.value), + temp = temp.next_node + + +# Print test +llist = LinkedList() +llist.push(20) +llist.push(4) +llist.push(15) +llist.push(85) + +print("Given Linked List") +llist.printList() +# llist.reverse_list() +# print("\nReversed Linked List") +# llist.printList() diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 12f9bfec4..4f3293090 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -5,8 +5,7 @@ def __init__(self, capacity): self.storage = [] # initialize ring buffer size to zero self.size = 0 - # instantiate the tail and head to insert and remove oldest and newest - self.tail = -1 # to equal newest element + # instantiate head to update self.head = 0 # to equal oldest element # https://www.oreilly.com/library/view/python-cookbook/0596001673/ch05s19.html @@ -34,8 +33,7 @@ def get(self): # returns all of the elements in the buffer in a list return self.storage - # added enqueue to insert values at end of RingBuffer. - + # Added enqueue to insert values at end of RingBuffer. def enqueue(self, value): # inserts the value at the end of the storage self.storage.append(value)