|
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):
|
9 |
| - ''' |
10 |
| - Initialize an LRU Cache with given capacity. |
11 |
| - capacity : int -> the capacity of the LRU Cache |
12 |
| - ''' |
13 | 9 | def __init__(self, capacity : int)->None:
|
| 10 | + ''' |
| 11 | + Initialize an LRU Cache with given capacity. |
| 12 | + capacity : int -> the capacity of the LRU Cache |
| 13 | + >>> cache = LRUCache(2) |
| 14 | + >>> cache |
| 15 | + {} |
| 16 | + ''' |
14 | 17 | self.remaining:int = capacity
|
15 | 18 |
|
16 |
| - ''' |
17 |
| - This method gets the value associated with the key. |
18 |
| - key : Hashable -> a hashable object that is mapped to a value inside of the LRU cache. |
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.' |
27 |
| - ''' |
28 | 19 | def get(self, key:Hashable)->Any:
|
| 20 | + ''' |
| 21 | + This method gets the value associated with the key. |
| 22 | + key : Hashable -> a hashable object that is mapped to a value inside of the LRU cache. |
| 23 | + returns -> value : Any -> any object that is stored as a value inside of the LRU cache. |
| 24 | +
|
| 25 | + >>> cache = LRUCache(2) |
| 26 | + >>> cache.put(1,1) |
| 27 | + >>> cache.get(1) |
| 28 | + 1 |
| 29 | + >>> cache.get(2) |
| 30 | + Traceback (most recent call last): |
| 31 | + ... |
| 32 | + KeyError: '2 not found.' |
| 33 | + ''' |
29 | 34 | if key not in self:
|
30 | 35 | raise KeyError(f"{key} not found.")
|
31 | 36 | val = self.pop(key) # Pop the key-value and re-insert to maintain the order
|
32 | 37 | self[key] = val
|
33 | 38 | return val
|
34 | 39 |
|
35 |
| - ''' |
36 |
| - This method puts the value associated with the key provided inside of the LRU cache. |
37 |
| - key : Hashable -> a hashable object that is mapped to a value inside of the LRU cache. |
38 |
| - value: Any -> any object that is to be associated with the key inside of the LRU cache. |
39 |
| - >>> cache = LRUCache(2) |
40 |
| - >>> cache.put(3,3) |
41 |
| - >>> cache |
42 |
| - {3:3} |
43 |
| - >>> cache.put(2,2) |
44 |
| - >>> cache |
45 |
| - {3:3, 2:2} |
46 |
| - ''' |
| 40 | + |
47 | 41 | def put(self, key:Hashable, value:Any)->None:
|
| 42 | + ''' |
| 43 | + This method puts the value associated with the key provided inside of the LRU cache. |
| 44 | + key : Hashable -> a hashable object that is mapped to a value inside of the LRU cache. |
| 45 | + value: Any -> any object that is to be associated with the key inside of the LRU cache. |
| 46 | + >>> cache = LRUCache(2) |
| 47 | + >>> cache.put(3,3) |
| 48 | + >>> cache |
| 49 | + {3: 3} |
| 50 | + >>> cache.put(2,2) |
| 51 | + >>> cache |
| 52 | + {3: 3, 2: 2} |
| 53 | + ''' |
48 | 54 | # To pop the last value inside of the LRU cache
|
49 | 55 | if key in self:
|
50 | 56 | self.pop(key)
|
@@ -85,9 +91,10 @@ def main()->None:
|
85 | 91 | except KeyError:
|
86 | 92 | print("Key not found in cache")
|
87 | 93 |
|
88 |
| - import doctest |
89 |
| - doctest.testmod() |
| 94 | + |
90 | 95 |
|
91 | 96 | if __name__ == '__main__':
|
| 97 | + import doctest |
| 98 | + doctest.testmod() |
92 | 99 | main()
|
93 | 100 |
|
0 commit comments