From a1302d32a8d739f3d4cf8d2cf7b5e1691fb027ab Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Fri, 20 Feb 2026 20:37:37 +0000 Subject: [PATCH 1/6] lru cache task completed --- Sprint-2/implement_lru_cache/lru_cache.py | 102 ++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index e69de29..b59ba4e 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -0,0 +1,102 @@ +import time + +our_list = [] +lookup_map = {} +limit = 0 + +# `LruCache(limit)` should construct +# an LRU cache which never stores more than `limit` entries. +def LruCache(user_limit): + global limit, our_list, lookup_map + limit = user_limit + our_list = [] + lookup_map = {} + +# * `set(key, value)` should associate `value` with the passed `key`. +def set(key, value): + global our_list, lookup_map + + if key in lookup_map: + old_item = lookup_map[key] + our_list.remove(old_item) + + wrapped_item = { + "key": key, + "value": value, + "tracker": time.time() + } + +#add to list and map + our_list.insert(0, wrapped_item) + lookup_map[key] = wrapped_item + +#if full remove oldest timestamp so last + if len(our_list) > limit: + oldest_item = our_list.pop() + + del lookup_map[oldest_item["key"]] + + +# * `get(key)` should look-up the value previously associated with `key`. +def get(key): + global our_list, lookup_map +#check map instead of for loop + if key in lookup_map: + # find by key + item = lookup_map[key] + + # // tracker to now timestamp updaed + item["tracker"] = time.time() + + #to front + our_list.remove(item) + our_list.insert(0, item) + + return item["value"] + + return None + + +#before i did it this was but was looping over each item and did not +#fit the required complexity +# def get_old(key): +# for item in our_list: +# if item["key"] == key: +# item["tracker"] = time.time() +# our_list.remove(item) +# our_list.insert(0, item) +# return item["value"] +# return None + +#and before tried with an id not timestamp +# tracker_number = 0 + +# def set(key, value): +# global tracker_number, our_list, lookup_map + + +# wrapped_item = { +# "key": key, +# "value": value, +# "tracker": tracker_number +# } + + +# tracker_number += 1 + +# our_list.insert(0, wrapped_item) +# lookup_map[key] = wrapped_item + +# and the loop +# def get_old(key): +# global tracker_number, our_list +# for item in our_list: +# if item["key"] == key: +# item["tracker"] = tracker_number +# tracker_number += 1 + +# our_list.remove(item) +# our_list.insert(0, item) + +# return item["value"] +# return None \ No newline at end of file From e30a88faf52f004d21da7024a44450535339777f Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Fri, 20 Feb 2026 20:39:11 +0000 Subject: [PATCH 2/6] lru cache finished --- Sprint-2/implement_lru_cache/lru_cache.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index b59ba4e..9f3e784 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -48,12 +48,11 @@ def get(key): # // tracker to now timestamp updaed item["tracker"] = time.time() - #to front + #move to front our_list.remove(item) our_list.insert(0, item) return item["value"] - return None From 06dd9f1c5f1222dbe2502231aa84600d0622d443 Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Mon, 2 Mar 2026 12:56:06 +0000 Subject: [PATCH 3/6] reviewer feedback implemented --- .gitignore | 1 + Sprint-2/implement_lru_cache/lru_cache.py | 130 +++++++--------------- 2 files changed, 39 insertions(+), 92 deletions(-) diff --git a/.gitignore b/.gitignore index bfb5886..6367f73 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules .venv __pycache__ +package-lock.json diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index 9f3e784..f7dcbe3 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -1,101 +1,47 @@ import time -our_list = [] -lookup_map = {} -limit = 0 - -# `LruCache(limit)` should construct -# an LRU cache which never stores more than `limit` entries. -def LruCache(user_limit): - global limit, our_list, lookup_map - limit = user_limit - our_list = [] - lookup_map = {} - -# * `set(key, value)` should associate `value` with the passed `key`. -def set(key, value): - global our_list, lookup_map - - if key in lookup_map: - old_item = lookup_map[key] - our_list.remove(old_item) - - wrapped_item = { - "key": key, - "value": value, - "tracker": time.time() - } - -#add to list and map - our_list.insert(0, wrapped_item) - lookup_map[key] = wrapped_item - -#if full remove oldest timestamp so last - if len(our_list) > limit: - oldest_item = our_list.pop() - - del lookup_map[oldest_item["key"]] - - -# * `get(key)` should look-up the value previously associated with `key`. -def get(key): - global our_list, lookup_map -#check map instead of for loop - if key in lookup_map: - # find by key - item = lookup_map[key] +class LruCache: + # `LruCache(limit)` should construct + # an LRU cache which never stores more than `limit` entries. + def __init__(self, user_limit): + self.limit = user_limit + self.our_list = [] + self.lookup_map = {} + + # * `set(key, value)` should associate `value` with the passed `key`. + def set(self, key, value): - # // tracker to now timestamp updaed - item["tracker"] = time.time() + if key in self.lookup_map: + old_item = self.lookup_map[key] + self.our_list.remove(old_item) + + wrapped_item = { + "key": key, + "value": value, + } - #move to front - our_list.remove(item) - our_list.insert(0, item) + #add to list and map + self.our_list.insert(0, wrapped_item) + self.lookup_map[key] = wrapped_item - return item["value"] - return None - - -#before i did it this was but was looping over each item and did not -#fit the required complexity -# def get_old(key): -# for item in our_list: -# if item["key"] == key: -# item["tracker"] = time.time() -# our_list.remove(item) -# our_list.insert(0, item) -# return item["value"] -# return None - -#and before tried with an id not timestamp -# tracker_number = 0 + #if full remove oldest timestamp so last + if len(self.our_list) > self.limit: + oldest_item = self.our_list.pop() + + del self.lookup_map[oldest_item["key"]] -# def set(key, value): -# global tracker_number, our_list, lookup_map - - -# wrapped_item = { -# "key": key, -# "value": value, -# "tracker": tracker_number -# } - - -# tracker_number += 1 - -# our_list.insert(0, wrapped_item) -# lookup_map[key] = wrapped_item -# and the loop -# def get_old(key): -# global tracker_number, our_list -# for item in our_list: -# if item["key"] == key: -# item["tracker"] = tracker_number -# tracker_number += 1 + # * `get(key)` should look-up the value previously associated with `key`. + def get(self, key): + #check map instead of for loop + if key in self.lookup_map: + # find by key + item = self.lookup_map[key] -# our_list.remove(item) -# our_list.insert(0, item) + #move to front + self.our_list.remove(item) + self.our_list.insert(0, item) -# return item["value"] -# return None \ No newline at end of file + return item["value"] + return None + From 6d42d37bcb05dd6878f5673c19a82f8d0969ec98 Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Tue, 3 Mar 2026 18:04:28 +0000 Subject: [PATCH 4/6] feedback implemented --- Sprint-2/implement_lru_cache/lru_cache.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index f7dcbe3..46bac3d 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -1,11 +1,12 @@ import time +from collections import OrderedDict class LruCache: # `LruCache(limit)` should construct # an LRU cache which never stores more than `limit` entries. def __init__(self, user_limit): self.limit = user_limit - self.our_list = [] + self.our_list = OrderedDict() self.lookup_map = {} # * `set(key, value)` should associate `value` with the passed `key`. @@ -13,23 +14,24 @@ def set(self, key, value): if key in self.lookup_map: old_item = self.lookup_map[key] - self.our_list.remove(old_item) - + self.our_list.pop(key) wrapped_item = { "key": key, "value": value, } #add to list and map - self.our_list.insert(0, wrapped_item) + self.our_list[key] = wrapped_item + self.our_list.move_to_end(key, last=False) self.lookup_map[key] = wrapped_item #if full remove oldest timestamp so last if len(self.our_list) > self.limit: - oldest_item = self.our_list.pop() + # oldest_item = self.our_list.pop() - del self.lookup_map[oldest_item["key"]] - + # del self.lookup_map[oldest_item["key"]] + oldest_key, oldest_item = self.our_list.popitem() + del self.lookup_map[oldest_key] # * `get(key)` should look-up the value previously associated with `key`. def get(self, key): @@ -39,8 +41,9 @@ def get(self, key): item = self.lookup_map[key] #move to front - self.our_list.remove(item) - self.our_list.insert(0, item) + # self.our_list.remove(item) + # self.our_list.insert(0, item) + self.our_list.move_to_end(key, last=False) return item["value"] return None From 0af9dcf13275e15aacf116aefebae99f143a8359 Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Wed, 4 Mar 2026 12:23:33 +0000 Subject: [PATCH 5/6] reviewer feedback implemented --- Sprint-2/implement_lru_cache/lru_cache.py | 42 ++++++++--------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index 46bac3d..753bada 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -1,50 +1,36 @@ -import time from collections import OrderedDict class LruCache: - # `LruCache(limit)` should construct + # TASK + # `LruCache(limit)` should construct # an LRU cache which never stores more than `limit` entries. def __init__(self, user_limit): self.limit = user_limit - self.our_list = OrderedDict() - self.lookup_map = {} + self.our_key_dictionary = OrderedDict() - # * `set(key, value)` should associate `value` with the passed `key`. + # TASK + # `set(key, value)` should associate `value` with the passed `key`. def set(self, key, value): if key in self.lookup_map: old_item = self.lookup_map[key] - self.our_list.pop(key) + self.our_key_dictionary.pop(key) wrapped_item = { "key": key, "value": value, } - #add to list and map - self.our_list[key] = wrapped_item - self.our_list.move_to_end(key, last=False) - self.lookup_map[key] = wrapped_item + self.our_key_dictionary[key] = wrapped_item + self.our_key_dictionary.move_to_end(key, last=False) - #if full remove oldest timestamp so last - if len(self.our_list) > self.limit: - # oldest_item = self.our_list.pop() - - # del self.lookup_map[oldest_item["key"]] - oldest_key, oldest_item = self.our_list.popitem() - del self.lookup_map[oldest_key] + if len(self.our_key_dictionary) > self.limit: + self.our_key_dictionary.popitem() - # * `get(key)` should look-up the value previously associated with `key`. def get(self, key): - #check map instead of for loop - if key in self.lookup_map: - # find by key - item = self.lookup_map[key] - - #move to front - # self.our_list.remove(item) - # self.our_list.insert(0, item) - self.our_list.move_to_end(key, last=False) - + if key in self.our_key_dictionary: + item = self.our_key_dictionary[key] + self.our_key_dictionary.move_to_end(key, last=False) + return item["value"] return None From ec3b9b9e84d2d7d7b3fdc09a521904a74678e468 Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Wed, 4 Mar 2026 18:48:52 +0000 Subject: [PATCH 6/6] prefix removed --- Sprint-2/implement_lru_cache/lru_cache.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index 753bada..3470167 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -6,7 +6,7 @@ class LruCache: # an LRU cache which never stores more than `limit` entries. def __init__(self, user_limit): self.limit = user_limit - self.our_key_dictionary = OrderedDict() + self.key_dictionary = OrderedDict() # TASK # `set(key, value)` should associate `value` with the passed `key`. @@ -14,22 +14,22 @@ def set(self, key, value): if key in self.lookup_map: old_item = self.lookup_map[key] - self.our_key_dictionary.pop(key) + self.key_dictionary.pop(key) wrapped_item = { "key": key, "value": value, } - self.our_key_dictionary[key] = wrapped_item - self.our_key_dictionary.move_to_end(key, last=False) + self.key_dictionary[key] = wrapped_item + self.key_dictionary.move_to_end(key, last=False) if len(self.our_key_dictionary) > self.limit: - self.our_key_dictionary.popitem() + self.oey_dictionary.popitem() def get(self, key): if key in self.our_key_dictionary: item = self.our_key_dictionary[key] - self.our_key_dictionary.move_to_end(key, last=False) + self.key_dictionary.move_to_end(key, last=False) return item["value"] return None