22
33
44class DoubleLinkedListNode :
5- '''
5+ """
66 Double Linked List Node built specifically for LFU Cache
7- '''
7+ """
88
99 def __init__ (self , key : int , val : int ):
1010 self .key = key
@@ -15,19 +15,19 @@ def __init__(self, key: int, val: int):
1515
1616
1717class DoubleLinkedList :
18- '''
18+ """
1919 Double Linked List built specifically for LFU Cache
20- '''
20+ """
2121
2222 def __init__ (self ):
2323 self .head = DoubleLinkedListNode (None , None )
2424 self .rear = DoubleLinkedListNode (None , None )
2525 self .head .next , self .rear .prev = self .rear , self .head
2626
2727 def add (self , node : DoubleLinkedListNode ) -> None :
28- '''
28+ """
2929 Adds the given node at the head of the list and shifting it to proper position
30- '''
30+ """
3131
3232 temp = self .rear .prev
3333
@@ -43,9 +43,9 @@ def _position_node(self, node: DoubleLinkedListNode) -> None:
4343 node1 .next , node2 .prev = node2 , node1
4444
4545 def remove (self , node : DoubleLinkedListNode ) -> DoubleLinkedListNode :
46- '''
46+ """
4747 Removes and returns the given node from the list
48- '''
48+ """
4949
5050 temp_last , temp_next = node .prev , node .next
5151 node .prev , node .next = None , None
@@ -54,7 +54,7 @@ def remove(self, node: DoubleLinkedListNode) -> DoubleLinkedListNode:
5454
5555
5656class LFUCache :
57- '''
57+ """
5858 LFU Cache to store a given capacity of data. Can be used as a stand-alone object
5959 or as a function decorator.
6060
@@ -72,7 +72,7 @@ class LFUCache:
7272 >>> cache.get(4)
7373 4
7474 >>> cache
75- CacheInfo(hits=3, misses=2, capacity=2, current size =2)
75+ CacheInfo(hits=3, misses=2, capacity=2, current_size =2)
7676 >>> @LFUCache.decorator(100)
7777 ... def fib(num):
7878 ... if num in (1, 2):
@@ -83,8 +83,8 @@ class LFUCache:
8383 ... res = fib(i)
8484
8585 >>> fib.cache_info()
86- CacheInfo(hits=196, misses=100, capacity=100, current size =100)
87- '''
86+ CacheInfo(hits=196, misses=100, capacity=100, current_size =100)
87+ """
8888
8989 # class variable to map the decorator functions to their respective instance
9090 decorator_function_to_instance_map = {}
@@ -98,30 +98,32 @@ def __init__(self, capacity: int):
9898 self .cache = {}
9999
100100 def __repr__ (self ) -> str :
101- '''
101+ """
102102 Return the details for the cache instance
103103 [hits, misses, capacity, current_size]
104- '''
104+ """
105105
106- return (f'CacheInfo(hits={ self .hits } , misses={ self .miss } , '
107- f'capacity={ self .capacity } , current size={ self .num_keys } )' )
106+ return (
107+ f"CacheInfo(hits={ self .hits } , misses={ self .miss } , "
108+ f"capacity={ self .capacity } , current_size={ self .num_keys } )"
109+ )
108110
109111 def __contains__ (self , key : int ) -> bool :
110- '''
112+ """
111113 >>> cache = LFUCache(1)
112114 >>> 1 in cache
113115 False
114116 >>> cache.set(1, 1)
115117 >>> 1 in cache
116118 True
117- '''
119+ """
118120 return key in self .cache
119121
120122 def get (self , key : int ) -> Optional [int ]:
121- '''
123+ """
122124 Returns the value for the input key and updates the Double Linked List. Returns
123125 None if key is not present in cache
124- '''
126+ """
125127
126128 if key in self .cache :
127129 self .hits += 1
@@ -131,9 +133,9 @@ def get(self, key: int) -> Optional[int]:
131133 return None
132134
133135 def set (self , key : int , value : int ) -> None :
134- '''
136+ """
135137 Sets the value for the input key and updates the Double Linked List
136- '''
138+ """
137139
138140 if key not in self .cache :
139141 if self .num_keys >= self .capacity :
@@ -152,12 +154,11 @@ def set(self, key: int, value: int) -> None:
152154
153155 @staticmethod
154156 def decorator (size : int = 128 ):
155- '''
157+ """
156158 Decorator version of LFU Cache
157- '''
159+ """
158160
159161 def cache_decorator_inner (func : Callable ):
160-
161162 def cache_decorator_wrapper (* args , ** kwargs ):
162163 if func not in LFUCache .decorator_function_to_instance_map :
163164 LFUCache .decorator_function_to_instance_map [func ] = LFUCache (size )
0 commit comments