Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
day two
  • Loading branch information
amoito committed Mar 24, 2020
commit 04dd4442b5842d850add33fd05821bb3ee8fcf0f
65 changes: 65 additions & 0 deletions linked_middle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Node:
def __init__(self, value):
self.value = value
self.next = None

def add(self, value):
self.next = Node(value)

def findMiddle(head):
middle = head
end = head
print(head)
while end.next!= None and end.next.next != None:
middle = middle.next
end = end.next.next
print(middle.value)

# print(head.value)
# print(head.next.value)
# print(head.next.next.value)
# print(head.next.next.next.value)
# print(head.next.next.next.next.value)
# print(head.next.next.next.next.next.value)
n1 = Node(1)
n1.add(2)
n1.next.add(3)
n1.next.next.add(4)
n1.next.next.next.add(5)
n1.next.next.next.next.add(6)

# reverse(n1)
# print("hello")
findMiddle(n1)























# def reverse(head):
# before = head
# worker = head
# while(worker != None):
# after = worker.next
# worker.next = before
# before = worker
# worker = after
# head = before
105 changes: 90 additions & 15 deletions lru_cache/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, value, prev=None, next=None):

"""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."""
have a next node it is pointed to."""
def insert_after(self, value):
current_next = self.next
self.next = ListNode(value, self, current_next)
Expand Down Expand Up @@ -40,34 +40,109 @@ def delete(self):


class DoublyLinkedList:
def __init__(self, node=None):
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):
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
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):
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
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):
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

#TODO: Catch errors if list is empty or node is not in list
#For now assume node is in list
self.length -= 1
#if head and tail
if self.head is self.tail:
self.head = None
self.tail = None
# if head
elif node is self.head:
self.head = self.head.next
node.delete()
# if tail
elif node is self.tail:
self.tail = self.tail.prev
node.delete()
#if regular
else:
node.delete()

"""Returns the highest value currently in the list"""
def get_max(self):
pass
# Loop through all nodes, looking for this value
#TODO: Error checking
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

34 changes: 30 additions & 4 deletions lru_cache/lru_cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import sys
sys.path.append('./doubly_linked_list')
from doubly_linked_list import DoublyLinkedList
from doubly_linked_list import ListNode

class LRUCache:
"""
Our LRUCache class keeps track of the max number of nodes it
Expand All @@ -7,7 +12,10 @@ class LRUCache:
to every node stored in the cache.
"""
def __init__(self, limit=10):
pass
self.hash_map = {}
self.orderList = DoublyLinkedList()
self.limit = limit
self.size = 0

"""
Retrieves the value associated with the given key. Also
Expand All @@ -17,8 +25,13 @@ def __init__(self, limit=10):
key-value pair doesn't exist in the cache.
"""
def get(self, key):
pass

if key not in self.hash_map.keys():
return None
else:
self.orderList.move_to_front(self.hash_map[key][1])
self.hash_map[key][1] = self.orderList.head
return self.hash_map[key][0]

"""
Adds the given key-value pair to the cache. The newly-
added pair should be considered the most-recently used
Expand All @@ -30,4 +43,17 @@ def get(self, key):
the newly-specified value.
"""
def set(self, key, value):
pass
if key in self.hash_map.keys():
self.orderList.move_to_front(self.hash_map[key][1])

else:
if self.size == self.limit:
del self.hash_map[self.orderList.tail.value]
self.orderList.remove_from_tail()
else:
self.size += 1
self.orderList.add_to_head(key)
self.hash_map[key] = [None, None]
self.hash_map[key][0] = value
self.hash_map[key][1] = self.orderList.head

2 changes: 2 additions & 0 deletions queue_and_stack/dll_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ def dequeue(self):

def len(self):
return self.size
#or return len(self.storage) if you comment out self.size = 0
# on line 8