Skip to content

London | 25-SDC-NOV | Jesus del Moral | Sprint 2 | LRU Cache#137

Open
delmorallopez wants to merge 4 commits intoCodeYourFuture:mainfrom
delmorallopez:sprint2-lru-cache
Open

London | 25-SDC-NOV | Jesus del Moral | Sprint 2 | LRU Cache#137
delmorallopez wants to merge 4 commits intoCodeYourFuture:mainfrom
delmorallopez:sprint2-lru-cache

Conversation

@delmorallopez
Copy link

LRU Cache

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@delmorallopez delmorallopez added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 1, 2026
Copy link

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 can import your LinkedList class from the other exercise, and then store each key-value pairs as a tuple.

Alternatively, you can use OrderedDict directly within LruCache to maintain order.

Could you update your code using one of these approaches?

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Mar 2, 2026
@delmorallopez
Copy link
Author

I updated the implementation using OrderedDict as suggested. This approach simplifies the code and better follows the Single-Responsibility Principle, since order maintenance is handled by OrderedDict rather than a custom linked list.

The cache now:

  • storing key-value pairs in OrderedDict, which preserves insertion order
  • moving accessed or updated items to the end to mark them as most recently used
  • evicting the least recently used item (the first entry) when the limit is reached
  • providing O(1) get and set operations
    This removes the need for manual node management and linked list logic, making the code cleaner and easier to maintain while still satisfying LRU behavior.

@delmorallopez delmorallopez added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 3, 2026
Comment on lines +9 to +10
<<<<<<< HEAD
self.cache = {} # key -> node
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a merge conflict has not yet been properly resolved.

@cjyuan cjyuan removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 3, 2026
@delmorallopez delmorallopez added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 4, 2026
Copy link

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good.

Comment on lines +15 to +16
value = self.cache.pop(key)
self.cache[key] = value
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OrderedDict has a built-in method that can replace these two operations.

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Reviewed Volunteer to add when completing a review with trainee action still to take. labels Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants