From 4e8b57681344c060fb5849a9b48b2f00c268f2b2 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Tue, 24 Feb 2026 15:19:12 +0000 Subject: [PATCH 1/4] lru cache --- Sprint-2/implement_lru_cache/lru_cache.py | 82 +++++++++++++++++++ .../implement_lru_cache/lru_cache_test.py | 1 - 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index e69de29..fcad074 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -0,0 +1,82 @@ +class Node: + def __init__(self, key, value): + self.key = key + self.value = value + self.previous = None + self.next = None + + +class LruCache: + def __init__(self, limit): + if limit <= 0: + raise ValueError("Limit must be positive") + + self.limit = limit + self.cache = {} # key -> node + self.head = None # Most recently used + self.tail = None # Least recently used + + # --------------------- + # Internal helpers + # --------------------- + + def _remove_node(self, node): + if node.previous: + node.previous.next = node.next + else: + self.head = node.next + + if node.next: + node.next.previous = node.previous + else: + self.tail = node.previous + + node.previous = None + node.next = None + + def _add_to_head(self, node): + node.next = self.head + node.previous = None + + if self.head: + self.head.previous = node + else: + self.tail = node + + self.head = node + + # --------------------- + # Public API + # --------------------- + + def get(self, key): + node = self.cache.get(key) + if not node: + return None + + # Move to head (recently used) + self._remove_node(node) + self._add_to_head(node) + + return node.value + + def set(self, key, value): + node = self.cache.get(key) + + if node: + # Update value and move to head + node.value = value + self._remove_node(node) + self._add_to_head(node) + return + + # If full, evict LRU (tail) + if len(self.cache) >= self.limit: + lru = self.tail + self._remove_node(lru) + del self.cache[lru.key] + + # Insert new node + new_node = Node(key, value) + self._add_to_head(new_node) + self.cache[key] = new_node diff --git a/Sprint-2/implement_lru_cache/lru_cache_test.py b/Sprint-2/implement_lru_cache/lru_cache_test.py index d37df01..666852b 100644 --- a/Sprint-2/implement_lru_cache/lru_cache_test.py +++ b/Sprint-2/implement_lru_cache/lru_cache_test.py @@ -1,5 +1,4 @@ import unittest - from lru_cache import LruCache class LruCacheTest(unittest.TestCase): From 80670ec6224f3ea62bd197408a936fc5ae41104c Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Tue, 3 Mar 2026 16:21:48 +0000 Subject: [PATCH 2/4] lru cache update --- Sprint-2/implement_lru_cache/lru_cache.py | 49 +++++++++-------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index fcad074..31cd44c 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -1,10 +1,4 @@ -class Node: - def __init__(self, key, value): - self.key = key - self.value = value - self.previous = None - self.next = None - +from collections import OrderedDict class LruCache: def __init__(self, limit): @@ -12,6 +6,7 @@ def __init__(self, limit): raise ValueError("Limit must be positive") self.limit = limit +<<<<<<< HEAD self.cache = {} # key -> node self.head = None # Most recently used self.tail = None # Least recently used @@ -48,35 +43,27 @@ def _add_to_head(self, node): # --------------------- # Public API # --------------------- +======= + self.cache = OrderedDict() +>>>>>>> fc65c37 (lru cache update) def get(self, key): - node = self.cache.get(key) - if not node: + if key not in self.cache: return None - # Move to head (recently used) - self._remove_node(node) - self._add_to_head(node) - - return node.value + # Move key to the end (most recently used) + value = self.cache.pop(key) + self.cache[key] = value + return value def set(self, key, value): - node = self.cache.get(key) - - if node: - # Update value and move to head - node.value = value - self._remove_node(node) - self._add_to_head(node) - return + if key in self.cache: + # Update existing and move to end + self.cache.pop(key) - # If full, evict LRU (tail) - if len(self.cache) >= self.limit: - lru = self.tail - self._remove_node(lru) - del self.cache[lru.key] + elif len(self.cache) >= self.limit: + # Remove least recently used (first item) + self.cache.popitem(last=False) - # Insert new node - new_node = Node(key, value) - self._add_to_head(new_node) - self.cache[key] = new_node + # Insert as most recently used + self.cache[key] = value \ No newline at end of file From 486700ec7a6e9f7e260165714d5f9d10731c1e90 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Tue, 3 Mar 2026 16:24:03 +0000 Subject: [PATCH 3/4] LRU Cache update --- Sprint-2/implement_lru_cache/lru_cache.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index 31cd44c..6de6501 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -50,7 +50,6 @@ def _add_to_head(self, node): def get(self, key): if key not in self.cache: return None - # Move key to the end (most recently used) value = self.cache.pop(key) self.cache[key] = value From 238028a0aab5a1ff0e60b29669f266dc8503d77e Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Wed, 4 Mar 2026 10:28:47 +0000 Subject: [PATCH 4/4] lru cache update --- Sprint-2/implement_lru_cache/lru_cache.py | 44 +---------------------- 1 file changed, 1 insertion(+), 43 deletions(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index 6de6501..bc8b928 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -6,63 +6,21 @@ def __init__(self, limit): raise ValueError("Limit must be positive") self.limit = limit -<<<<<<< HEAD - self.cache = {} # key -> node - self.head = None # Most recently used - self.tail = None # Least recently used - - # --------------------- - # Internal helpers - # --------------------- - - def _remove_node(self, node): - if node.previous: - node.previous.next = node.next - else: - self.head = node.next - - if node.next: - node.next.previous = node.previous - else: - self.tail = node.previous - - node.previous = None - node.next = None - - def _add_to_head(self, node): - node.next = self.head - node.previous = None - - if self.head: - self.head.previous = node - else: - self.tail = node - - self.head = node - - # --------------------- - # Public API - # --------------------- -======= self.cache = OrderedDict() ->>>>>>> fc65c37 (lru cache update) def get(self, key): if key not in self.cache: return None - # Move key to the end (most recently used) + value = self.cache.pop(key) self.cache[key] = value return value def set(self, key, value): if key in self.cache: - # Update existing and move to end self.cache.pop(key) elif len(self.cache) >= self.limit: - # Remove least recently used (first item) self.cache.popitem(last=False) - # Insert as most recently used self.cache[key] = value \ No newline at end of file