From 1de3bd805e5d70e804bf165ee870d748ef6a72bd Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 05:05:43 +0000 Subject: [PATCH 01/12] start constructing LRU cache with inital data points (key & limit/capacity) --- Sprint-2/implement_lru_cache/lru_cache.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index e69de29..c466f54 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -0,0 +1,4 @@ +class LruCache: + def __init__(self, limit): + self.key = None + self.limit = None \ No newline at end of file From 3785f2cac4fcaab7e342637d5575595a66a26f63 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 05:11:34 +0000 Subject: [PATCH 02/12] fixed the constructor/ internal state --- Sprint-2/implement_lru_cache/lru_cache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index c466f54..6e205d0 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -1,4 +1,4 @@ class LruCache: def __init__(self, limit): - self.key = None - self.limit = None \ No newline at end of file + self.limit = limit + self.cache = {} \ No newline at end of file From 8bbe322efaf9a34c677558a28a9e80f8316b4570 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 05:24:00 +0000 Subject: [PATCH 03/12] build a function that sets key and value --- Sprint-2/implement_lru_cache/lru_cache.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index 6e205d0..02a1dd6 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -1,4 +1,9 @@ class LruCache: def __init__(self, limit): self.limit = limit - self.cache = {} \ No newline at end of file + self.cache = {} + + def set(self, key, value): + self.key = key + self.value = value + \ No newline at end of file From 9888128a3d887bb3e8e88352f70a857a2aa2fe41 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 05:27:25 +0000 Subject: [PATCH 04/12] update the internal structure of the set method so that it pairs a key and it's associated value corretly inside the cache structure --- Sprint-2/implement_lru_cache/lru_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index 02a1dd6..ddf0868 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -5,5 +5,5 @@ def __init__(self, limit): def set(self, key, value): self.key = key - self.value = value + self.cache[key] = value \ No newline at end of file From 5b96fa5f424e874792f3ec979bed7826c9348ea6 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 05:33:00 +0000 Subject: [PATCH 05/12] construct a method that returns a value associated with a key --- Sprint-2/implement_lru_cache/lru_cache.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index ddf0868..51f01f4 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -6,4 +6,6 @@ def __init__(self, limit): def set(self, key, value): self.key = key self.cache[key] = value - \ No newline at end of file + + def get(self, key): + return self.cache[key] From 992963901b3351e83673e4b0efb7a005bcfef95f Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 05:34:28 +0000 Subject: [PATCH 06/12] get rid of the unnecessary problematic line from set method to avoid bugs --- 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 51f01f4..9cfb428 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -4,7 +4,6 @@ def __init__(self, limit): self.cache = {} def set(self, key, value): - self.key = key self.cache[key] = value def get(self, key): From 3d7bd7b73660261a1e8318b8a2be2f9b974c08dc Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 06:41:30 +0000 Subject: [PATCH 07/12] replace normal dict {} with OrderedDict so that we're able to maintain keys order and thus be able to eventually remove the least recently used one. --- Sprint-2/implement_lru_cache/lru_cache.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index 9cfb428..5761f4e 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -1,7 +1,9 @@ +from collections import OrderedDict + class LruCache: def __init__(self, limit): self.limit = limit - self.cache = {} + self.cache = OrderedDict() def set(self, key, value): self.cache[key] = value From 62f033ddc956511a8b6dd71c83f7c7cfc66b503e Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 06:50:15 +0000 Subject: [PATCH 08/12] update set method to check if a key is present in cache. If true - mark as most recently used by moving it to be the tail --- 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 5761f4e..dae372f 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -6,6 +6,8 @@ def __init__(self, limit): self.cache = OrderedDict() def set(self, key, value): + if key in self.cache: + self.cache.move_to_end(key) self.cache[key] = value def get(self, key): From 21c79881d5a152d21e3570af533a5e4b99e24577 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 07:01:58 +0000 Subject: [PATCH 09/12] update set method to handle when the number of keys inside a cache is greater than its limit and if so the least recently used key is removed --- Sprint-2/implement_lru_cache/lru_cache.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index dae372f..be26cf0 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -5,10 +5,17 @@ def __init__(self, limit): self.limit = limit self.cache = OrderedDict() + def set(self, key, value): if key in self.cache: self.cache.move_to_end(key) + else: + if len(self.cache) >= self.limit: + self.cache.popitem(key) + self.cache[key] = value + + def get(self, key): return self.cache[key] From 3a6da2b2bc22651aaa155967f95158ca08f223bd Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 07:06:27 +0000 Subject: [PATCH 10/12] update get method to mark a key as the most recently used if it's present in cache - else do nothing --- Sprint-2/implement_lru_cache/lru_cache.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index be26cf0..f2555d5 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -18,4 +18,8 @@ def set(self, key, value): def get(self, key): - return self.cache[key] + if key in self.cache: + self.cache.move_to_end(key) + return self.cache[key] + else: + return None From 4caae0a93a996b591ec52cb260a5f42acee05a42 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 07:18:40 +0000 Subject: [PATCH 11/12] inside set method, in the else condition, update the argument for .popitem() method so the oldest item (least cently used) gets removed for cache --- Sprint-2/implement_lru_cache/lru_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index f2555d5..8523dad 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -11,7 +11,7 @@ def set(self, key, value): self.cache.move_to_end(key) else: if len(self.cache) >= self.limit: - self.cache.popitem(key) + self.cache.popitem(last=False) self.cache[key] = value From 713e3a8d7477a8cd4965dc710a6b93df15a34bfd Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 07:29:35 +0000 Subject: [PATCH 12/12] update the initializer inside the LruCache class to throw an error when the cash limit is zero and below - This leads to passing the last remaining unit test --- 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 8523dad..3408b73 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -2,6 +2,8 @@ class LruCache: def __init__(self, limit): + if limit <= 0: + raise ValueError("Limit must be greater than zero") self.limit = limit self.cache = OrderedDict()