Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions names/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
18 changes: 17 additions & 1 deletion reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
138 changes: 138 additions & 0 deletions ring_buffer/doubly_linked_list.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 24 additions & 3 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -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
buffer_list = []

node = self.storage.head
while node is not None:
buffer_list.append(node.value)
node = node.next

return buffer_list