From 82e5cbb36a811430b5d172716104f59b226de194 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sat, 3 Oct 2026 18:55:36 +0100 Subject: [PATCH 1/7] 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 e69de29b..82c561b9 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/7] 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 82c561b9..ad102898 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/7] 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 ad102898..aa15764f 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/7] 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 aa15764f..9d2b11f1 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/7] 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 9d2b11f1..f4e2ef81 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/7] 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 f4e2ef81..f33e73c8 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] From 68c6e406b45fadd457500f7a2cb937c6ff5348cc Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sun, 11 Oct 2026 16:02:22 +0100 Subject: [PATCH 7/7] refactor: separate linked list logic from LRU cache --- Sprint-2/implement_lru_cache/linked_list.py | 56 ++++++++++++++++ Sprint-2/implement_lru_cache/lru_cache.py | 72 +++++++-------------- 2 files changed, 78 insertions(+), 50 deletions(-) create mode 100644 Sprint-2/implement_lru_cache/linked_list.py diff --git a/Sprint-2/implement_lru_cache/linked_list.py b/Sprint-2/implement_lru_cache/linked_list.py new file mode 100644 index 00000000..bc4200bf --- /dev/null +++ b/Sprint-2/implement_lru_cache/linked_list.py @@ -0,0 +1,56 @@ +class LinkedList: + class Node: + __slots__ = ("next","previous","value") + + def __init__(self, value): + self.next = None + self.previous = None + self.value = value + + def __init__(self): + self.head = None + self.tail = None + self.size = 0 + + def push_head(self, value): + new_node = self.Node(value=value) + if self.head == None: + self.head = new_node + self.tail = new_node + else: + new_node.next = self.head + self.head.previous = new_node + self.head = new_node + self.size += 1 + return new_node + + def pop_tail(self): + if self.tail == None: + return + removed_value = self.tail.value + + self.remove(self.tail) + return removed_value + + + 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 + self.size -= 1 \ No newline at end of file diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index f33e73c8..66092f19 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -1,77 +1,49 @@ +from linked_list import LinkedList + + 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): if limit <= 0: raise ValueError("limit must be greater than 0") - self.head = None - self.tail = None + # self.head = None + # self.tail = None + # self.limit = limit + # self.cache = {} + self.list = LinkedList() 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 - - 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 + def move_to_head(self, node): - else: - node.next.previous = node.previous - node.previous.next = node.next - node.next = None - node.previous = None + key, value = node.value - def move_to_head(self, node): - self.remove(node) - self.add_to_head(node) + self.list.remove(node) + new_node = self.list.push_head((key, value)) + self.cache[key] = new_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 + + return target.value[1] def set(self, key, value): target = self.cache.get(key) if target is not None: - target.value = value + + target.value = (key, value) self.move_to_head(target) else: - target = self.clsNode(value, key) - self.add_to_head(target) - self.cache[key] = target + + new_Node = self.list.push_head((key, value)) + self.cache[key] = new_Node if len(self.cache) > self.limit: - old_tail = self.tail - self.remove(old_tail) - del self.cache[old_tail.key] + old_key, old_value = self.list.pop_tail() + del self.cache[old_key]