Skip to content
Open
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
89 changes: 89 additions & 0 deletions Sprint-2/implement_lru_cache/lru_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.next = None
self.previous = None


class LruCache:
def __init__(self, limit):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To better adhere to the Single-Responsibility Principle (SRP) from SOLID design principles,
it's preferable to implement the "doubly linked list" and the "LRU Cache" as separate classes, with the linked list used inside LruCache to manage ordering. In fact, you could just import the linked list which you implemented in the other exercise.

Alternatively, OrderedDict can be used directly within LruCache to maintain order.

Could you update your code using one of these approaches?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To better adhere to the Single-Responsibility Principle (SRP) from SOLID design principles, it's preferable to implement the "doubly linked list" and the "LRU Cache" as separate classes, with the linked list used inside LruCache to manage ordering. In fact, you could just import the linked list which you implemented in the other exercise.

Alternatively, OrderedDict can be used directly within LruCache to maintain order.

Could you update your code using one of these approaches?

I agree with the suggestion. Since the LinkedList implementation is in a separate branch, I couldn't import it directly here. Instead, I separated the linked-list logic into its own class and used it within LruCache to keep the responsibilities separate.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To by pass the validation bot check, you could copy the linked_list.py from the other folder to the implement_lru_cache folder, and then import the class. It would be a good practice for code reuse.

if limit <= 0:
raise ValueError("Limit must be greater than 0")

self.limit = limit
self.cache = {}
self.head = None
self.tail = None

def _add_to_head(self, node):
node.previous = None
node.next = self.head

if self.head is not None:
self.head.previous = node

self.head = node

if self.tail is None:
self.tail = node

def _move_to_head(self, node):
if node == self.head:
return

if node.previous is not None:
node.previous.next = node.next

if node.next is not None:
node.next.previous = node.previous

if node == self.tail:
self.tail = node.previous

node.previous = None
node.next = self.head

if self.head is not None:
self.head.previous = node

self.head = node

def _remove_tail(self):
if self.tail is None:
return

old_tail = self.tail

if self.head == self.tail:
self.head = None
self.tail = None
else:
self.tail = old_tail.previous
self.tail.next = None

del self.cache[old_tail.key]

def get(self, key):
node = self.cache.get(key)

if node is None:
return None

self._move_to_head(node)
return node.value

def set(self, key, value):
if key in self.cache:
node = self.cache[key]
node.value = value
self._move_to_head(node)
return

node = Node(key, value)

self.cache[key] = node
self._add_to_head(node)

if len(self.cache) > self.limit:
self._remove_tail()
Loading