← 返回 anthropic 的题目列表Fix a Buggy LRU Cache Key Built from args and kwargs
类型:online_judge
Problem: Fix LRU Cache Key Construction from args and kwargs
Given a Python LRU cache decorator that constructs cache keys from *args and **kwargs, the implementation has a bug: semantically same or different calls may be incorrectly treated as the same key, or calls with reordered kwargs may miss the cache.
Implement a correct make_key(args, kwargs) function and use it to implement a capacity-limited LRU cache decorator.
Requirements
Positional arguments must be included in the key.
Keyword arguments must be included independent of insertion order.
Argument boundaries must not collide; for example, args=(1, 2), kwargs={} must not be confused with args=(1,), kwargs={'x': 2}.
When cache size exceeds capacity, evict the least recently used item.
I/O
The core task is code repair and explanation. For runnable testing, stdin version uses:
First line: capacity Q.
Each following line: CALL a b, representing f(a, b) = a + b.
Print each result, then print the number of actual function executions as calls=X.
Example
Input
2 4
CALL 1 2
CALL 1 2
CALL 2 3
CALL 1 2
Output
3
3
5
3
calls=2