generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 16
ZA | 25-SDC-July | Luke Manyamazi | Sprint 2 | Implement LRU Cache #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Luke-Manyamazi
wants to merge
1
commit into
CodeYourFuture:main
Choose a base branch
from
Luke-Manyamazi:implement_lru_cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+75
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| class LruCache: | ||
| def __init__(self, limit): | ||
| if limit <= 0: | ||
| raise ValueError("Limit must be greater than 0") | ||
|
|
||
| self.limit = limit | ||
| self.cache = {} | ||
| self.access_order = [] # most recent at the end | ||
|
|
||
| def get(self, key): | ||
| if key in self.cache: | ||
| # Move to most recent position | ||
| self.access_order.remove(key) | ||
| self.access_order.append(key) | ||
| return self.cache[key] | ||
| return None | ||
|
|
||
| def set(self, key, value): | ||
| if key in self.cache: | ||
| # Update existing key | ||
| self.cache[key] = value | ||
| # Move to most recent position | ||
| self.access_order.remove(key) | ||
| self.access_order.append(key) | ||
| else: | ||
| # New key | ||
| if len(self.cache) >= self.limit: | ||
| # Remove least recently used (first item) | ||
| lru_key = self.access_order[0] | ||
| del self.cache[lru_key] | ||
| self.access_order.pop(0) | ||
|
|
||
| # Add new key as most recent | ||
| self.cache[key] = value | ||
| self.access_order.append(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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,6 +54,46 @@ def test_eviction_order_after_gets(self): | |
| self.assertEqual(cache.get("a"), 1) | ||
| self.assertEqual(cache.get("c"), 3) | ||
|
|
||
| def test_get_refreshes_item(self): | ||
| """Test that getting an item makes it recently used""" | ||
| cache = LruCache(limit=2) | ||
|
|
||
| cache.set("a", 1) | ||
| cache.set("b", 2) | ||
|
|
||
| # Access "a" to make it recently used | ||
| cache.get("a") | ||
|
|
||
| # Add new item - should evict "b" not "a" | ||
| cache.set("c", 3) | ||
|
|
||
| self.assertIsNone(cache.get("b")) # "b" was evicted | ||
| self.assertEqual(cache.get("a"), 1) # "a" remains | ||
| self.assertEqual(cache.get("c"), 3) | ||
|
|
||
| def test_complex_usage_pattern(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good to see these additional tests. You're the only one I've seen so far that has added these tests.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oooh thank you, David. |
||
| """Test LRU behavior with multiple operations""" | ||
| cache = LruCache(limit=3) | ||
|
|
||
| # Add initial items | ||
| cache.set("a", 1) | ||
| cache.set("b", 2) | ||
| cache.set("c", 3) | ||
|
|
||
| # Use items in various order | ||
| cache.get("a") | ||
| cache.get("c") | ||
| cache.get("b") | ||
| cache.get("a") | ||
|
|
||
| # Add new item - should evict least recently used ("c") | ||
| cache.set("d", 4) | ||
|
|
||
| self.assertIsNone(cache.get("c")) # "c" was evicted | ||
| self.assertEqual(cache.get("a"), 1) | ||
| self.assertEqual(cache.get("b"), 2) | ||
| self.assertEqual(cache.get("d"), 4) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this code works, list.remove has to scan through the list to find key. This isn't O(1), which is the requirement of this test. What O( ) is it?. How else might you manage the order without this?