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 de683df commit a668db6Copy full SHA for a668db6
Sprint-2/implement_lru_cache/lru_cache.py
@@ -0,0 +1,26 @@
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
+ self.cache = OrderedDict()
11
12
+ def get(self, key):
13
+ if key not in self.cache:
14
+ return None
15
16
+ self.cache.move_to_end(key)
17
+ return self.cache[key]
18
19
+ def set(self, key, value):
20
+ if key in self.cache:
21
22
23
+ self.cache[key] = value
24
25
+ if len(self.cache) > self.limit:
26
+ self.cache.popitem(last=False)
0 commit comments