London | 26-SDC-March | Zobeir Rigi | Sprint 2 | Implement an LRU cache - #207
London | 26-SDC-March | Zobeir Rigi | Sprint 2 | Implement an LRU cache#207Zobeir-Rigi wants to merge 2 commits into
Conversation
| class Node: | ||
| def __init__(self, key, value): | ||
| self.key = key | ||
| self.value = value | ||
| self.next = None | ||
| self.previous = None | ||
|
|
||
|
|
||
| class LruCache: | ||
| def __init__(self, limit): |
There was a problem hiding this comment.
To better adhere to the Single-Responsibility Principle (SRP) from SOLID design principles,
it's preferable to implement the "doubly linked list" and the "LRU Cache" as separate classes, with the linked list used inside LruCache to manage ordering. In fact, you could just import the linked list which you implemented in the other exercise.
Alternatively, OrderedDict can be used directly within LruCache to maintain order.
Could you update your code using one of these approaches?
There was a problem hiding this comment.
To better adhere to the Single-Responsibility Principle (SRP) from SOLID design principles, it's preferable to implement the "doubly linked list" and the "LRU Cache" as separate classes, with the linked list used inside LruCache to manage ordering. In fact, you could just import the linked list which you implemented in the other exercise.
Alternatively,
OrderedDictcan be used directly withinLruCacheto maintain order.Could you update your code using one of these approaches?
I agree with the suggestion. Since the LinkedList implementation is in a separate branch, I couldn't import it directly here. Instead, I separated the linked-list logic into its own class and used it within LruCache to keep the responsibilities separate.
There was a problem hiding this comment.
To by pass the validation bot check, you could copy the linked_list.py from the other folder to the implement_lru_cache folder, and then import the class. It would be a good practice for code reuse.
|
Changes look good. |
I originally completed all the Sprint 2 exercises in one PR, but the validation expected separate PRs for each issue. This PR contains only the solution for the "Implement an LRU cache in Python" exercise.
Implemented an LRU cache with:
All provided tests pass.