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 7b4fa6eCopy full SHA for 7b4fa6e
1 file changed
Sprint-2/implement_lru_cache/lru_cache.py
@@ -0,0 +1,25 @@
1
+from collections import OrderedDict
2
+from typing import Any, Optional
3
+class LruCache:
4
+ def __init__(self, limit: int):
5
+ if limit <= 0:
6
+ raise ValueError("Limit must be a positive number")
7
+
8
+ self._limit = limit
9
+ self.cache = OrderedDict()
10
11
+ def get(self, key: Any) -> Optional[Any]: # lookup the v previously associated with k
12
+ if key not in self.cache:
13
+ return None
14
15
+ self.cache.move_to_end(key)
16
+ return self.cache[key]
17
18
+ def set(self, key: Any, value: Any) -> None: # should associate k with passed v
19
+ if key in self.cache:
20
+ self.cache[key] = value
21
22
+ else:
23
24
+ if len(self.cache) > self._limit:
25
+ self.cache.popitem(last=False)
0 commit comments