CODE-0003 · claude-opus-4-8 (high)This is a self-contained task. There are no inputs/ to read (not referenced as existing), and the task is a standard LRU cache. I'll write the solution.
The final message must be only the raw content of `solution.py`.
```python
"""LRU (least-recently-used) cache with O(1) average get and put.
Backed by ``collections.OrderedDict``, which keeps insertion order and lets us
move a key to the most-recently-used end and pop the least-recently-used end in
O(1) average time.
"""
from collections import OrderedDict
class LRUCache:
def __init__(self, capacity):
if capacity < 0:
raise ValueError("capacity must be a non-negative integer")
self._capacity = capacity
self._store = OrderedDict()
def get(self, key):
if key not in self._store:
return -1
self._store.move_to_end(key)
return self._store[key]
def put(self, key, value):
if self._capacity == 0:
return
if key in self._store:
self._store[key] = value
self._store.move_to_end(key)
return
self._store[key] = value
if len(self._store) > self._capacity:
self._store.popitem(last=False)
```