Evict the least recently used key
A cache keeps a limited number of results. When it is full, a least recently used policy drops the key nobody has touched for the longest time. OrderedDict remembers order and can move a key to the end in constant time.
from collections import OrderedDict
recent = OrderedDict(a=1, b=2)
recent.move_to_end("a")
print(list(recent)) # ['b', 'a']Think before coding
Treat the end of the dictionary as most recent. Both get and put on an existing key move it there. When an insert exceeds capacity, popitem(last=False) removes the oldest key from the front. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write LRUCache(capacity) with get(key), returning None when missing, and put(key, value). A full cache evicts the least recently used key.
The explanation opens here once every check passes.