Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests #15

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
59 changes: 42 additions & 17 deletions 1-lru-cache/python/src/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@ def __init__(self, maxSize) -> None:
self.maxSize = maxSize
self.lookupTable = {}

def move_to_tail(self, key):
node = self.lookupTable[key]
# Remove node from current place
if node == self.tail:
return

if node == self.head:
self.head = node.nextNode
node.prevNode = None
else:
left = node.prevNode
right = node.nextNode
left.nextNode = right
right.prevNode = left

# add node at tail
before_tail = self.tail.prevNode
tail = self.tail
tail.nextNode = node
node.prevNode = tail
node.nextNode = None
self.tail = node

def remove_from_head(self):
# Note: copied from above
key = self.head.key

right = self.head.nextNode
self.head = right
right.prevNode = None

# This part is only difference from above
del self.lookupTable[key]

def get(self, key) -> int:
if None == key:
return None
Expand All @@ -19,26 +53,11 @@ def get(self, key) -> int:
if self.tail is result:
return result.value

if None == result.prevNode:
self.head = result.nextNode
self.head.prevNode = None

self.tail.nextNode = result
result.nextNode = None
result.prevNode = None
self.tail = result
return result.value
self.move_to_tail(key)

def put(self, key, val) -> None:
if None == key:
return None

self.currentSize += 1
if self.currentSize > self.maxSize:
self.lookupTable.pop(self.head.key)
self.head = self.head.nextNode
self.head.prevNode = None
self.currentSize -= 1

newNode = Node(key, val, self.tail, None)
if None == self.tail:
Expand All @@ -48,7 +67,13 @@ def put(self, key, val) -> None:

self.tail = newNode
self.lookupTable[key] = newNode


self.move_to_tail(key)
self.currentSize += 1
if self.currentSize > self.maxSize:
self.remove_from_head()
self.currentSize -= 1

def toArray(self):
a = []
current = self.head
Expand Down
4 changes: 3 additions & 1 deletion 1-lru-cache/python/src/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ def __init__(self, key, value, prevNode, nextNode) -> None:
self.value = value
self.prevNode = prevNode
self.nextNode = nextNode


def __repr__(self):
return f"Node({self.key=},{self.value=})"
9 changes: 8 additions & 1 deletion 1-lru-cache/python/tests/cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from src.cache import Cache
import unittest

"""
most recent is at tail

we kick items at the head when at capacity
"""


class CachTest(unittest.TestCase):

def test_constructs(self):
Expand Down Expand Up @@ -35,7 +42,7 @@ def test_enforces_max_size_on_set(self):
cache.put('bananas', 111)
cache.put('apples', 222)
cache.put('oranges', 333)
cache.put('cucumbers', 444)
cache.put('cucumbers', 444)
self.assertEqual(cache.currentSize, 3)
self.assertEqual(cache.toArray(), [222, 333, 444])

Expand Down