MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 2 | feat/lru cache - #5
Open
marissamolejon wants to merge 1 commit into
Open
MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 2 | feat/lru cache#5marissamolejon wants to merge 1 commit into
marissamolejon wants to merge 1 commit into
Conversation
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
Implements
LruCacheinlru_cache.py, supportinggetandset,both O(1) worst-case, with least-recently-used eviction once the cache
reaches its
limit.Design
Combines two structures:
_nodes_by_key) for O(1) key lookup.the head, least-recently-used at the tail.
Every
getorseton an existing key moves that entry's node to thehead of the list. When
setis called on a new key and the cache isalready at
limit, the tail node (least recently used) is evicted fromboth the dict and the list before the new entry is inserted at the head.
This mirrors the doubly-linked list design from Issue CodeYourFuture#9 - the same
"give each node enough info to reconnect its own neighbors" idea makes
both moving-to-head and evicting-the-tail O(1), with no searching.
LruCache(limit=0)(or negative) raisesValueErrorimmediately.Testing
All 5 provided tests pass, including the two that specifically exercise
eviction order:
test_eviction_order_just_inserts- eviction based on insertion ordertest_eviction_order_after_gets- provesgetalso refreshes recency,not just
setRan locally via
python3 -m pytest lru_cache_test.py -v- all passing.Learning points
jump straight to a key. Combining them is what makes both lookup and
reordering O(1) - each structure covers the other's weakness.
(insert, remove, evict) - this is a manual invariant Python won't
enforce, so eviction updates both together in one method.
getandsetmust refresh recency, not justset- easy tomiss if you only think about "writes" as the thing that matters, but
the README (and
test_eviction_order_after_gets) make clear readscount too.
Checklist
getandsetare both O(1) worst-case