-
-
Notifications
You must be signed in to change notification settings - Fork 58
London | 26-SDC-March | Jamal Laqdiem| Sprint 2 | Implement an LRU cache #213
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
jamallaqdiem
wants to merge
1
commit into
CodeYourFuture:main
Choose a base branch
from
jamallaqdiem:fix-lru-cache-pr
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.
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,50 @@ | ||
| import sys | ||
| import os | ||
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../implement_linked_list"))) | ||
|
|
||
| from linked_list import LinkedList, Node # type:ignore | ||
| from typing import Any | ||
|
|
||
| class LruCache: | ||
| def __init__(self, limit: int): | ||
| if limit <= 0: | ||
| raise ValueError("Limit must be greater than 0") | ||
| self.capacity = limit | ||
| # maps key | ||
| self.cache: dict[Any, Node] = {} | ||
| # tracking the usage order head first tail last. | ||
| self.list = LinkedList() | ||
|
|
||
| def get(self, key: Any) -> Any: | ||
| # If key not exist return None | ||
| if key not in self.cache: | ||
| return None | ||
|
|
||
| node_handle = self.cache[key] | ||
|
|
||
| # we get the value to return | ||
| _, value = node_handle.value | ||
|
|
||
| # we use remove() and push_head() to remove it from the position to the head | ||
| self.list.remove(node_handle) | ||
| self.cache[key] = self.list.push_head((key, value)) | ||
|
|
||
| return value | ||
|
|
||
| def set(self, key: Any, value: Any) -> None: | ||
| if key in self.cache: | ||
| self.list.remove(self.cache[key]) | ||
| # we do this because the position and value about to change. | ||
| del self.cache[key] | ||
|
|
||
| # we call pop_tail() to cut off the tail | ||
| elif len(self.cache) >= self.capacity: | ||
| # return the value stored in the tail node | ||
| oldest_item = self.list.pop_tail() | ||
| if oldest_item: | ||
| oldest_key, _ = oldest_item | ||
| del self.cache[oldest_key] | ||
|
|
||
| ## place the new items at the head of stack and update and return the object Node. | ||
| new_node = self.list.push_head((key, value)) | ||
| self.cache[key] = new_node | ||
Oops, something went wrong.
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.
I think you can include
linked_lst.pyfrom the other exercise in this folder, as this script requires it to work. If a reviewer haven't yet seen your LinkedList implementation, they may ask for it.Uh oh!
There was an error while loading. Please reload this page.
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.
Hi @cjyuan thanks for the review, I tried that way in my PR Closed however the metadata for the PR check didn't allow it, so I had to close and open a new one.
which better way I should use?
Uh oh!
There was an error while loading. Please reload this page.
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.
The validation bot is very rigid -- it expects changes to be made only in some folder for each exercise.
So to by pass the check, you would have to copy the file
linked_list.pyinto theimplement_lru_cachefolder. It's not a normal way to reuse code; just a way to by pass the bot check.Don't worry about it.