From d2d02891c165d0daaefabbd4db9599deb98bf767 Mon Sep 17 00:00:00 2001 From: ofega Date: Fri, 12 Jun 2020 08:51:45 +0100 Subject: [PATCH] done with my sprint challenge :) --- names/names.py | 8 +- reverse/reverse.py | 18 +++- ring_buffer/doubly_linked_list.py | 138 ++++++++++++++++++++++++++++++ ring_buffer/ring_buffer.py | 27 +++++- 4 files changed, 184 insertions(+), 7 deletions(-) create mode 100644 ring_buffer/doubly_linked_list.py diff --git a/names/names.py b/names/names.py index ea158997f..9794201b1 100644 --- a/names/names.py +++ b/names/names.py @@ -12,11 +12,13 @@ duplicates = [] # Return the list of duplicates in this data structure +""" +Starter Code Runtime Complexity = 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) + if name_1 in names_2: + duplicates.append(name_1) end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..c771317be 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -39,4 +39,20 @@ def contains(self, value): return False def reverse_list(self, node, prev): - pass + if self.head == None: + return + + previous_node = None + current_node = self.head + next_node = current_node.next_node + + + while current_node: + current_node.next_node = previous_node + previous_node = current_node + current_node = next_node + + if next_node: + next_node = next_node.next_node + + self.head = previous_node diff --git a/ring_buffer/doubly_linked_list.py b/ring_buffer/doubly_linked_list.py new file mode 100644 index 000000000..906630972 --- /dev/null +++ b/ring_buffer/doubly_linked_list.py @@ -0,0 +1,138 @@ +"""Each ListNode holds a reference to its previous node +as well as its next node in the List.""" + + +class ListNode: + def __init__(self, value, prev=None, next=None): + self.value = value + self.prev = prev + self.next = next + + """Wrap the given value in a ListNode and insert it + after this node. Note that this node could already + have a next node it is point to.""" + def insert_after(self, value): + current_next = self.next + self.next = ListNode(value, self, current_next) + if current_next: + current_next.prev = self.next + + """Wrap the given value in a ListNode and insert it + before this node. Note that this node could already + have a previous node it is point to.""" + def insert_before(self, value): + current_prev = self.prev + self.prev = ListNode(value, current_prev, self) + if current_prev: + current_prev.next = self.prev + + """Rearranges this ListNode's previous and next pointers + accordingly, effectively deleting this ListNode.""" + def delete(self): + if self.prev: + self.prev.next = self.next + if self.next: + self.next.prev = self.prev + + +"""Our doubly-linked list class. It holds references to +the list's head and tail nodes.""" + + +class DoublyLinkedList: + def __init__(self, node=None): + self.head = node + self.tail = node + self.length = 1 if node is not None else 0 + + def __len__(self): + return self.length + + """Wraps the given value in a ListNode and inserts it + as the new head of the list. Don't forget to handle + the old head node's previous pointer accordingly.""" + def add_to_head(self, value): + new_node = ListNode(value, None, None) + self.length += 1 + if not self.head and not self.tail: + self.head = new_node + self.tail = new_node + else: + new_node.next = self.head + self.head.prev = new_node + self.head = new_node + + """Removes the List's current head node, making the + current head's next node the new head of the List. + Returns the value of the removed Node.""" + def remove_from_head(self): + value = self.head.value + self.delete(self.head) + return value + + """Wraps the given value in a ListNode and inserts it + as the new tail of the list. Don't forget to handle + the old tail node's next pointer accordingly.""" + def add_to_tail(self, value): + new_node = ListNode(value, None, None) + self.length += 1 + if not self.head and not self.tail: + self.head = new_node + self.tail = new_node + else: + new_node.prev = self.tail + self.tail.next = new_node + self.tail = new_node + + """Removes the List's current tail node, making the + current tail's previous node the new tail of the List. + Returns the value of the removed Node.""" + def remove_from_tail(self): + value = self.tail.value + self.delete(self.tail) + return value + + """Removes the input node from its current spot in the + List and inserts it as the new head node of the List.""" + def move_to_front(self, node): + if node is self.head: + return + value = node.value + self.delete(node) + self.add_to_head(value) + + """Removes the input node from its current spot in the + List and inserts it as the new tail node of the List.""" + def move_to_end(self, node): + if node is self.tail: + return + value = node.value + self.delete(node) + self.add_to_tail(value) + + """Removes a node from the list and handles cases where + the node was the head or the tail""" + def delete(self, node): + self.length -= 1 + if self.head is self.tail: + self.head = None + self.tail = None + elif self.head is node: + self.head = node.next + node.delete() + elif self.tail is node: + self.tail = node.prev + node.delete() + else: + node.delete() + + """Returns the highest value currently in the list""" + def get_max(self): + max_value = self.head.value + current = self.head + while current is not None: + if current.value > max_value: + max_value = current.value + current = current.next + + return max_value \ No newline at end of file diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..67a8b080d 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,30 @@ +from doubly_linked_list import DoublyLinkedList + class RingBuffer: def __init__(self, capacity): - pass + self.capacity = capacity + self.current = None + self.storage = DoublyLinkedList() def append(self, item): - pass + if self.storage.length < self.capacity: + self.storage.add_to_tail(item) + self.current = self.storage.tail + + if self.storage.length == self.capacity: + self.current.value = item + + if self.current is self.storage.tail: + self.current = self.storage.head + else: + self.current = self.current.next def get(self): - pass \ No newline at end of file + buffer_list = [] + + node = self.storage.head + while node is not None: + buffer_list.append(node.value) + node = node.next + + return buffer_list \ No newline at end of file