Untitled
unknown
plain_text
9 months ago
649 B
8
Indexable
from collections import OrderedDict
class LRU:
def __init__(self):
self.cache = OrderedDict()
self.cache_size = 10
def get(self,key):
if key in self.cache:
value = self.cache.pop(key)
self.cache[key] = value
return value
return -1
def put(self,key,value):
if len(self.cache) >= self.cache_size:
self.cache.popitem()
self.cache[key] = value
else:
if key in self.cache:
self.cache.pop(key)
self.cache[key] = value
else:
self.cache[key] = valueEditor is loading...
Leave a Comment