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
70 changes: 62 additions & 8 deletions doubly_linked_list/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,95 @@ def __len__(self):
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):
pass
new_node = ListNode(value)
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):
pass
if self.head:
value = self.head.value
self.delete(self.head)
return value
return None

"""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):
pass
new_node = ListNode(value)
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):
pass
if self.tail:
value = self.tail.value
self.delete(self.tail)
return value
else:
return None

"""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):
pass
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):
pass
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):
pass
self.length -= 1

if self.head is self.tail:
self.head = None
self.tail = None
elif node is self.head:
self.head = self.head.next
node.delete()
elif node is self.tail:
self.tail = self.tail.prev
node.delete()
else:
node.delete()

"""Returns the highest value currently in the list"""
def get_max(self):
pass
if not self.head:
return None
max_value = self.head.value
current = self.head
while current:
if current.value > max_value:
max_value = current.value
current = current.next

return max_value
70 changes: 62 additions & 8 deletions lru_cache/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,79 @@ def __len__(self):
return self.length

def add_to_head(self, value):
pass
new_node = ListNode(value)
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

def remove_from_head(self):
pass
if self.head:
value = self.head.value
self.delete(self.head)
return value
return None

def add_to_tail(self, value):
pass
new_node = ListNode(value)
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

def remove_from_tail(self):
pass
if self.tail:
value = self.tail.value
self.delete(self.tail)
return value
else:
return None

def move_to_front(self, node):
pass
if node is self.head:
return
value = node.value
self.delete(node)
self.add_to_head(value)

def move_to_end(self, node):
pass
if node is self.tail:
return
value = node.value
self.delete(node)
self.add_to_tail(value)

def delete(self, node):
pass
self.length -= 1

if self.head is self.tail:
self.head = None
self.tail = None
elif node is self.head:
self.head = self.head.next
node.delete()
elif node is self.tail:
self.tail = self.tail.prev
node.delete()
else:
node.delete()

def get_max(self):
pass
if not self.head:
return None
max_value = self.head.value
current = self.head
while current:
if current.value > max_value:
max_value = current.value
current = current.next

return max_value
27 changes: 23 additions & 4 deletions lru_cache/lru_cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import sys
sys.path.append('./doubly_linked_list')
from doubly_linked_list import DoublyLinkedList

class LRUCache:
"""
Our LRUCache class keeps track of the max number of nodes it
Expand All @@ -6,8 +10,10 @@ class LRUCache:
order, as well as a storage dict that provides fast access
to every node stored in the cache.
"""
def __init__(self, limit=10):
pass
def __init__(self, limit=10, total=0):
self.limit = limit
self.storage = {}
self.order = DoublyLinkedList()

"""
Retrieves the value associated with the given key. Also
Expand All @@ -17,7 +23,12 @@ def __init__(self, limit=10):
key-value pair doesn't exist in the cache.
"""
def get(self, key):
pass
if key in self.storage:
node = self.storage[key]
self.order.move_to_front(node)
return node.value[1]
else:
return None

"""
Adds the given key-value pair to the cache. The newly-
Expand All @@ -30,4 +41,12 @@ def get(self, key):
the newly-specified value.
"""
def set(self, key, value):
pass
if key in self.storage:
self.storage[key].value = (key, value)
self.order.move_to_front(self.storage[key])
else:
self.order.add_to_head((key, value))
self.storage[key] = self.order.head
if self.order.length > self.limit:
del self.storage[self.order.tail.value[0]]
self.order.remove_from_tail()
10 changes: 5 additions & 5 deletions queue_and_stack/dll_queue.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import sys
sys.path.append('../doubly_linked_list')
sys.path.append('./doubly_linked_list')
from doubly_linked_list import DoublyLinkedList


class Queue:
def __init__(self):
self.size = 0
# Why is our DLL a good choice to store our elements?
# self.storage = ?
self.storage = DoublyLinkedList()

def enqueue(self, value):
pass
return self.storage.add_to_tail(value)

def dequeue(self):
pass
return self.storage.remove_from_head()

def len(self):
pass
return len(self.storage)
11 changes: 6 additions & 5 deletions queue_and_stack/dll_stack.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import sys
sys.path.append('../doubly_linked_list')
sys.path.append('./doubly_linked_list')
from doubly_linked_list import DoublyLinkedList

class Stack:
def __init__(self):
self.size = 0
# Why is our DLL a good choice to store our elements?
# self.storage = ?
self.storage = DoublyLinkedList()

def push(self, value):
pass
return self.storage.add_to_tail(value)

def pop(self):
pass
return self.storage.remove_from_tail()

def len(self):
pass
return len(self.storage)