Intermediate
A cache with invalidation by document
Complete the class AnswerCache. put(key, answer, doc_ids) stores the answer and remembers that each document in doc_ids was used. get(key) returns the answer or None.
invalidate(doc_id) removes every answer that used that document, forgets the document's record, and returns how many answers were removed (0 for an unknown document).
What Check tests
- Stores and returns an answer
- Unknown key gives None
- Invalidate removes the answers that used the document
- Other answers survive
- Unknown document removes nothing
- A second invalidate finds nothing
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.