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 4b7f7b8Copy full SHA for 4b7f7b8
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
5
+ def __init__(self, capacity: int):
6
+ self.cache = OrderedDict()
7
+ self.capacity = capacity
8
9
+ def get(self, key: int) -> int:
10
+ if key not in self.cache:
11
+ return -1
12
+ else:
13
+ self.cache.move_to_end(key)
14
+ return self.cache[key]
15
16
+ def set(self, key: int, value: int) -> None:
17
+ self.cache[key] = value
18
19
+ if len(self.cache) > self.capacity:
20
+ self.cache.popitem(last = False)
21
0 commit comments