Sheffield | 25-SDC-Nov | Sheida Shabankari | Sprint 2 | Implement LRU cache in python#105
Open
sheida-shab wants to merge 6 commits intoCodeYourFuture:mainfrom
Open
Sheffield | 25-SDC-Nov | Sheida Shabankari | Sprint 2 | Implement LRU cache in python#105sheida-shab wants to merge 6 commits intoCodeYourFuture:mainfrom
sheida-shab wants to merge 6 commits intoCodeYourFuture:mainfrom
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.
PR Summary :
Implement LRU Cache with O(1) get/set operations
Description:
This PR adds a fully functional LRU (Least Recently Used) cache in Python. The implementation includes:
Node class: Represents a key-value pair with pointers to previous and next nodes.
Doubly linked list: Maintains the order of nodes for LRU eviction in O(1) time.
push_head: Moves or adds a node to the head (most recently used).
pop_tail: Removes and returns the tail node (least recently used).
remove: Removes a specific node from the list.
LruCache class: Provides the cache interface.
get(key): Retrieves a value by key and marks the node as most recently used.
set(key, value): Adds or updates a key-value pair; evicts the least recently used node if the cache exceeds its limit.
Handles invalid cache limits by raising ValueError.
Each operation (get and set) has O(1) worst-case time complexity.
Testing:
Unit tests cover cache limit enforcement, eviction order, and get/set functionality.