We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 134e683 commit f95e61aCopy full SHA for f95e61a
1 file changed
Sprint-2/implement_lru_cache/lru_cache.py
@@ -0,0 +1,21 @@
1
+from collections import OrderedDict
2
+
3
+class LruCache:
4
+ def __init__(self, limit):
5
+ if limit <= 0:
6
+ raise ValueError("Limit must be positive")
7
+ self.limit = limit
8
+ self.cache = OrderedDict()
9
10
+ def get(self, key):
11
+ if key not in self.cache:
12
+ return None
13
+ self.cache.move_to_end(key)
14
+ return self.cache[key]
15
16
+ def set(self, key, value):
17
+ if key in self.cache:
18
19
+ self.cache[key] = value
20
+ if len(self.cache) > self.limit:
21
+ self.cache.popitem(last=False)
0 commit comments