From 82e5cbb36a811430b5d172716104f59b226de194 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sat, 3 Oct 2026 18:55:36 +0100 Subject: [PATCH 1/6] feat: add LRU cache node and list helpers --- Sprint-2/implement_lru_cache/lru_cache.py | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index e69de29..82c561b 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -0,0 +1,50 @@ +class LruCache: + class clsNode: + def __init__(self,value,key): + self.next= None + self.previous = None + self.value = value + self.key = key + + def __init__(self, limit): + self.head = None + self.tail = None + self.limit = limit + self.cache = {} + + def add_to_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 + + + def remove(self, node): + if node == None: + return + if node == self.head and node == self.tail: + self.head = None + self.tail = None + + elif node == self.head: + self.head = self.head.next + self.head.previous = None + + elif node == self.tail: + self.tail = self.tail.previous + self.tail.next = None + + else: + node.next.previous = node.previous + node.previous.next = node.next + node.next = None + node.previous = None + + def move_to_head(self,node): + self.remove(node) + self.add_to_head(node) + + From 126ce98bfd16c750f2415113e7f3d064e2fa7ac5 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sat, 3 Oct 2026 19:04:59 +0100 Subject: [PATCH 2/6] feat: implement LRU cache get method --- Sprint-2/implement_lru_cache/lru_cache.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index 82c561b..ad10289 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -47,4 +47,12 @@ def move_to_head(self,node): self.remove(node) self.add_to_head(node) - + # cache {key,node} + def get(self,key): + target = self.cache.get(key) + if target == None: + return None + + self.move_to_head(target) + return target.value + From 22ff7c7e35399d0606ce7515ea84103b0809975f Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sat, 3 Oct 2026 19:06:18 +0100 Subject: [PATCH 3/6] fix: handle None node in list helpers --- Sprint-2/implement_lru_cache/lru_cache.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index ad10289..aa15764 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -13,6 +13,8 @@ def __init__(self, limit): self.cache = {} def add_to_head(self, node): + if node == None: + return if self.head is None: self.head = node self.tail = node From 5a81599d6b6a79ce64d7bc3dbe98a397a90348ac Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sat, 3 Oct 2026 19:24:39 +0100 Subject: [PATCH 4/6] feat: implement LRU cache set method --- Sprint-2/implement_lru_cache/lru_cache.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index aa15764..9d2b11f 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -58,3 +58,19 @@ def get(self,key): self.move_to_head(target) return target.value + def set(self,key,value): + target = self.cache.get(key) + + if target is not None: + target.value = value + self.move_to_head(target) + + else: + target = self.clsNode(value,key) + self.add_to_head(target) + self.cache[key] = target + + if len(self.cache) > self.limit: + old_tail = self.tail + self.remove(old_tail) + del self.cache[old_tail.key] \ No newline at end of file From 3953bb87b18ebe2b15652d697609a8f1552301ac Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sat, 3 Oct 2026 19:26:13 +0100 Subject: [PATCH 5/6] fix: reject invalid LRU cache limits --- Sprint-2/implement_lru_cache/lru_cache.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index 9d2b11f..f4e2ef8 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -7,6 +7,8 @@ def __init__(self,value,key): self.key = key def __init__(self, limit): + if limit <= 0: + raise ValueError("limit must be greater than 0") self.head = None self.tail = None self.limit = limit From 639ffed184c30f40f37a7118a2e6feee8cfcd3a7 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sat, 3 Oct 2026 19:27:38 +0100 Subject: [PATCH 6/6] style: format code with Black --- Sprint-2/implement_lru_cache/lru_cache.py | 43 +++++++++++------------ 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index f4e2ef8..f33e73c 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -1,30 +1,29 @@ class LruCache: class clsNode: - def __init__(self,value,key): - self.next= None + def __init__(self, value, key): + self.next = None self.previous = None self.value = value self.key = key def __init__(self, limit): if limit <= 0: - raise ValueError("limit must be greater than 0") + raise ValueError("limit must be greater than 0") self.head = None self.tail = None - self.limit = limit + self.limit = limit self.cache = {} def add_to_head(self, node): - if node == None: - return - if self.head is None: - self.head = node - self.tail = node - else: - node.next = self.head - self.head.previous = node - self.head = node - + if node == None: + return + if self.head is None: + self.head = node + self.tail = node + else: + node.next = self.head + self.head.previous = node + self.head = node def remove(self, node): if node == None: @@ -45,22 +44,22 @@ def remove(self, node): node.next.previous = node.previous node.previous.next = node.next node.next = None - node.previous = None + node.previous = None - def move_to_head(self,node): + def move_to_head(self, node): self.remove(node) self.add_to_head(node) # cache {key,node} - def get(self,key): - target = self.cache.get(key) + def get(self, key): + target = self.cache.get(key) if target == None: return None - + self.move_to_head(target) return target.value - def set(self,key,value): + def set(self, key, value): target = self.cache.get(key) if target is not None: @@ -68,11 +67,11 @@ def set(self,key,value): self.move_to_head(target) else: - target = self.clsNode(value,key) + target = self.clsNode(value, key) self.add_to_head(target) self.cache[key] = target if len(self.cache) > self.limit: old_tail = self.tail self.remove(old_tail) - del self.cache[old_tail.key] \ No newline at end of file + del self.cache[old_tail.key]