diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index e69de29..bc8b928 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -0,0 +1,26 @@ +from collections import OrderedDict + +class LruCache: + def __init__(self, limit): + if limit <= 0: + raise ValueError("Limit must be positive") + + self.limit = limit + self.cache = OrderedDict() + + def get(self, key): + if key not in self.cache: + return None + + value = self.cache.pop(key) + self.cache[key] = value + return value + + def set(self, key, value): + if key in self.cache: + self.cache.pop(key) + + elif len(self.cache) >= self.limit: + self.cache.popitem(last=False) + + self.cache[key] = value \ No newline at end of file diff --git a/Sprint-2/implement_lru_cache/lru_cache_test.py b/Sprint-2/implement_lru_cache/lru_cache_test.py index d37df01..666852b 100644 --- a/Sprint-2/implement_lru_cache/lru_cache_test.py +++ b/Sprint-2/implement_lru_cache/lru_cache_test.py @@ -1,5 +1,4 @@ import unittest - from lru_cache import LruCache class LruCacheTest(unittest.TestCase):