From 6607118aa63dfb2a0da7a1b27f8769e2e37ee15e Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 13 Oct 2026 18:39:06 +0100 Subject: [PATCH 1/2] implement O(1) LRU Cache with eviction policy --- Sprint-2/implement_lru_cache/lru_cache.py | 74 +++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index e69de29b..b2d9275e 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -0,0 +1,74 @@ +class Node: + # Create a building block for the cache + def __init__(self, key, value): + self.key = key + self.value = value + self.next = None + self.previous = None + + +class LruCache: + # Initializes the cache with a maximum capacity + def __init__(self, limit): + if limit <= 0: + raise ValueError("Cache limit must be greater than zero.") + self.limit = limit + self.cache = {} # Maps key -> Node for O(1) lookups + self.head = None # Most recently used + self.tail = None # Least recently used + + # Looks up a value and marks it as recently used + def get(self, key): + if key not in self.cache: + return None + + node = self.cache[key] + self._move_to_head(node) + return node.value + + # Add or update an item, evicting the oldest if full + def set(self, key, value): + if key in self.cache: + node = self.cache[key] + node.value = value + self._move_to_head(node) + return + + if len(self.cache) >= self.limit: + if self.tail is not None: + del self.cache[self.tail.key] + + if self.head == self.tail: + self.head = None + self.tail = None + else: + self.tail = self.tail.previous + self.tail.next = None + + new_node = Node(key, value) + self.cache[key] = new_node + + if self.head is None: + self.head = new_node + self.tail = new_node + else: + new_node.next = self.head + self.head.previous = new_node + self.head = new_node + + # Internal helper to reposition a used node to the front + def _move_to_head(self, node): + if self.head == node: + return + + if node == self.tail: + self.tail = node.previous + self.tail.next = None + else: + node.previous.next = node.next + node.next.previous = node.previous + + node.next = self.head + self.head.previous = node + node.previous = None + self.head = node \ No newline at end of file From ed3433d920ae29a44060a0c4dbebd39e2003c166 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Thu, 15 Oct 2026 17:10:23 +0100 Subject: [PATCH 2/2] refactor: separate DoublyLinkedList and LruCache to adhere to SRP --- Sprint-2/implement_lru_cache/lru_cache.py | 96 ++++++++++++++--------- 1 file changed, 58 insertions(+), 38 deletions(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index b2d9275e..504d515b 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -1,4 +1,6 @@ class Node: + __slots__ = ("key", "value", "next", "previous") + # Create a building block for the cache def __init__(self, key, value): self.key = key @@ -7,6 +9,55 @@ def __init__(self, key, value): self.previous = None +class DoublyLinkedList: + # Manages node ordering (head, tail, pushing, removing, and moving to head) + def __init__(self): + self.head = None + self.tail = None + + def push_head(self, node): + if self.head is None: + self.head = node + self.tail = node + else: + node.next = self.head + self.head.previous = node + self.head = node + return node + + def remove(self, node): + if self.head == self.tail and node == self.head: + self.head = None + self.tail = None + elif node == self.head: + self.head = self.head.next + if self.head is not None: + self.head.previous = None + elif node == self.tail: + self.tail = self.tail.previous + if self.tail is not None: + self.tail.next = None + else: + node.previous.next = node.next + node.next.previous = node.previous + + node.next = None + node.previous = None + + def pop_tail(self): + if self.tail is None: + return None + tail_node = self.tail + self.remove(tail_node) + return tail_node + + def move_to_head(self, node): + if self.head == node: + return + self.remove(node) + self.push_head(node) + + class LruCache: # Initializes the cache with a maximum capacity def __init__(self, limit): @@ -14,8 +65,7 @@ def __init__(self, limit): raise ValueError("Cache limit must be greater than zero.") self.limit = limit self.cache = {} # Maps key -> Node for O(1) lookups - self.head = None # Most recently used - self.tail = None # Least recently used + self.order_list = DoublyLinkedList() # Manages LRU order # Looks up a value and marks it as recently used def get(self, key): @@ -23,7 +73,7 @@ def get(self, key): return None node = self.cache[key] - self._move_to_head(node) + self.order_list.move_to_head(node) return node.value # Add or update an item, evicting the oldest if full @@ -31,44 +81,14 @@ def set(self, key, value): if key in self.cache: node = self.cache[key] node.value = value - self._move_to_head(node) + self.order_list.move_to_head(node) return if len(self.cache) >= self.limit: - if self.tail is not None: - del self.cache[self.tail.key] - - if self.head == self.tail: - self.head = None - self.tail = None - else: - self.tail = self.tail.previous - self.tail.next = None + evicted_node = self.order_list.pop_tail() + if evicted_node is not None: + del self.cache[evicted_node.key] new_node = Node(key, value) self.cache[key] = new_node - - if self.head is None: - self.head = new_node - self.tail = new_node - else: - new_node.next = self.head - self.head.previous = new_node - self.head = new_node - - # Internal helper to reposition a used node to the front - def _move_to_head(self, node): - if self.head == node: - return - - if node == self.tail: - self.tail = node.previous - self.tail.next = None - else: - node.previous.next = node.next - node.next.previous = node.previous - - node.next = self.head - self.head.previous = node - node.previous = None - self.head = node \ No newline at end of file + self.order_list.push_head(new_node)