Implement a class LRUCache(capacity) with:
get(key) → the value, or -1 if the key is missing. A hit makes the key most recently used.put(key, value) → inserts or updates; when the cache is over capacity, evict the least recently
used key.Both operations must be O(1) on average. The harness creates your cache and replays a list of
operations, collecting the result of every get (and None for put).
Implement LRUCache. Input: capacity: int, ops: list of ['get', key] or ['put', key, value]. Returns: list of results.
Reading 'a' makes 'b' the least recently used, so adding 'c' evicts 'b'.
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
def __harness__(cls, capacity, ops):
cache = cls(capacity)
out = []
for op in ops:
if op[0] == "get":
out.append(cache.get(op[1]))
else:
out.append(cache.put(op[1], op[2]))
return out
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.data = {}
def get(self, key):
return self.data.get(key, -1)
def put(self, key, value) -> None:
self.data[key] = value
Run executes the public tests. Submit grades against public and hidden tests.
Implement a class LRUCache(capacity) with:
get(key) → the value, or -1 if the key is missing. A hit makes the key most recently used.put(key, value) → inserts or updates; when the cache is over capacity, evict the least recently
used key.Both operations must be O(1) on average. The harness creates your cache and replays a list of
operations, collecting the result of every get (and None for put).
Implement LRUCache. Input: capacity: int, ops: list of ['get', key] or ['put', key, value]. Returns: list of results.
Reading 'a' makes 'b' the least recently used, so adding 'c' evicts 'b'.
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
def __harness__(cls, capacity, ops):
cache = cls(capacity)
out = []
for op in ops:
if op[0] == "get":
out.append(cache.get(op[1]))
else:
out.append(cache.put(op[1], op[2]))
return out
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.data = {}
def get(self, key):
return self.data.get(key, -1)
def put(self, key, value) -> None:
self.data[key] = value
Run executes the public tests. Submit grades against public and hidden tests.