Select scoped memory records
Implement select_memory(records, user, now, limit). Each record has id, scope (* or a user), at, expires (integer or None), tier, and key. Exclude other users, future records, and records whose exclusive expiry has arrived. For each (tier,key), prefer the user's scope over global scope, then the latest timestamp, then the lexicographically greatest identifier. Sort winners by descending timestamp and ascending identifier; return at most limit. Raise ValueError for a negative limit. Inputs otherwise satisfy this schema. This is a memory selection algorithm, not an identity provider.
Your task
- Complete the starter function using the contract above.
- Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
- Run tests to record your result, then compare with the explained reference solution.
Examples
InputTwo records with the same key: global at 4, personal at 2; now=5
OutputThe personal record
InputOne record with expires=5; now=5
Output[]
Implement the function, then run the tests.
Ctrl / ⌘ + Enter to test · Shift + Tab leaves editor