Hard
Your Own LRU Cache
Write a decorator factory
lru(capacity) that caches results in an OrderedDict: a hit moves the key to the end, a miss stores the result and, when over capacity, removes the least recently used key and prints evict KEY. Decorate square(n), which prints compute n before returning n². Read the capacity and a line of numbers, call square on each and print the result; finally print hits=H misses=M.Input
Capacity, then numbers.
Output
Lines.
Sample 1
Input
2 1 2 1 3 2 1
Output
compute 1 1 compute 2 4 1 compute 3 evict 2 9 compute 2 evict 1 4 compute 1 evict 3 1 hits=1 misses=5
Sample 2
Input
1 5 5 6 5
Output
compute 5 25 25 compute 6 evict 5 36 compute 5 evict 6 25 hits=1 misses=3
Output appears here