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 cf9e042 commit c9a3affCopy full SHA for c9a3aff
1 file changed
Sprint-2/implement_lru_cache/lru_cache.py
@@ -0,0 +1,30 @@
1
+from collections import OrderedDict
2
+
3
+class LruCache:
4
5
+ def __init__(self, limit):
6
+ if limit <= 0:
7
+ raise ValueError("Limit must be positive")
8
9
+ self.limit = limit
10
11
+ self.cache = OrderedDict()
12
13
+ def get(self, key):
14
15
+ if key not in self.cache:
16
+ return None
17
18
+ self.cache.move_to_end(key)
19
+ return self.cache[key]
20
21
+ def set(self, key, value):
22
23
+ if key in self.cache:
24
25
26
+ self.cache[key] = value
27
28
+ if len(self.cache) > self.limit:
29
30
+ self.cache.popitem(last=False)
0 commit comments