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 e69de29..3470167 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -0,0 +1,36 @@ +from collections import OrderedDict + +class LruCache: + # 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.key_dictionary = OrderedDict() + + # 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.key_dictionary.pop(key) + wrapped_item = { + "key": key, + "value": value, + } + + self.key_dictionary[key] = wrapped_item + self.key_dictionary.move_to_end(key, last=False) + + if len(self.our_key_dictionary) > self.limit: + self.oey_dictionary.popitem() + + def get(self, key): + if key in self.our_key_dictionary: + item = self.our_key_dictionary[key] + self.key_dictionary.move_to_end(key, last=False) + + return item["value"] + return None +