James Postadan | Oct2025-2 | Module Complexity | Sprint 2 | Implement an LRU cache in Python - #16
Open
japostadan wants to merge 1 commit into
Open
James Postadan | Oct2025-2 | Module Complexity | Sprint 2 | Implement an LRU cache in Python#16japostadan wants to merge 1 commit into
japostadan wants to merge 1 commit into
Conversation
- LruCache(limit) raises ValueError for limit=0 - get(key): dict lookup O(1), move to head O(1), return value or None - set(key, value): insert/update at head O(1), evict tail when over limit O(1) - internal doubly linked list tracks recency (head=most recent, tail=least recent) - _store dict maps key→Node for O(1) access; needed for O(1) eviction of tail by key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎯 Summary
LruCache(limit)— raises ValueError when limit is 0get(key)— returns value or None; counts as a use and promotes entry to most-recentset(key, value)— stores entry; updates and promotes if key exists; evicts least-recently-used when over limit🧾 Changelist
len(store) > limit, evict the tail: delete from dict bytail.keyand unlink from list (all O(1))._push_head,_unlink,_move_to_head,_evict_tail— each does only pointer reassignment, all O(1).❓ Questions
getreturnsNonefor a missing key. Should it raise aKeyErrorinstead (dict-like behaviour), or is returningNonethe right API for a cache where misses are expected?🙌 Notes for Reviewers
python3 -m unittest lru_cache_test -vfromSprint-2/implement_lru_cache/— 5/5 pass