File tree 1 file changed +8
-1
lines changed
1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
'''
4
4
The following implementation of LRU Cache is one of the most elegant pythonic implementations.
5
- It only uses the in-built python dictionary. This works because
5
+ It only uses the in-built python dictionary (https://docs.python.org/3/library/stdtypes.html#typesmapping) . This works because
6
6
the Python dictionary maintains the order of insertion of keys and ensures O(1) operations on insert, delete and access.
7
7
'''
8
8
class LRUCache (dict ):
@@ -17,6 +17,13 @@ def __init__(self, capacity : int):
17
17
This method gets the value associated with the key.
18
18
key : Hashable -> a hashable object that is mapped to a value inside of the LRU cache.
19
19
returns -> value : Any -> any object that is stored as a value inside of the LRU cache.
20
+
21
+ >>> cache = LRUCache(2)
22
+ >>> cache.put(1,1)
23
+ >>> cache.get(1)
24
+ 1
25
+ >>> cache.get(2)
26
+ KeyError: '2 not found.'
20
27
'''
21
28
def get (self , key :Hashable )-> Any :
22
29
if key not in self :
You can’t perform that action at this time.
0 commit comments