Skip to content

London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 2 | Implement LRU cache#143

Open
aydaeslami wants to merge 3 commits into
CodeYourFuture:mainfrom
aydaeslami:Implement-an-LRU-cache-in-Python
Open

London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 2 | Implement LRU cache#143
aydaeslami wants to merge 3 commits into
CodeYourFuture:mainfrom
aydaeslami:Implement-an-LRU-cache-in-Python

Conversation

@aydaeslami

Copy link
Copy Markdown

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

Implemented LRU cache with O(1) get and set operations using a doubly linked list and dictionary.

@aydaeslami aydaeslami added 📅 Sprint 2 Assigned during Sprint 2 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Mar 1, 2026

@cjyuan cjyuan left a comment

Copy link
Copy Markdown

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
@aydaeslami

Copy link
Copy Markdown
Author

Thank you @cjyuan for the suggestion. I refactored the implementation to use OrderedDict , and it made the code much cleaner and easier to read.

I also appreciated the reminder about the Single Responsibility Principle (SRP).

Thanks for the helpful feedback!

@aydaeslami aydaeslami added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Jun 15, 2026
Comment on lines +16 to +17
value = self.cache.pop(key)
self.cache[key] = value

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does OrderedDict have any built-in method to move its item to the end?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. I've replaced the pop-and-reinsert approach with OrderedDict.move_to_end(), which makes the code cleaner and more readable.

I've also run the full test suite again, and all tests are passing.

Please let me know if there's anything else I should change or improve.

Comment on lines +31 to +46
# test

cache = LruCache(3)

cache.set("Aida", 1)
cache.set("Bob", 2)
cache.set("Clare", 3)

print(cache.get("Aida"))

cache.set("David", 4)

print(cache.get("Bob"))
print(cache.get("Clare"))
print(cache.get("David"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why not implement this in lru_cache_test.py?

@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 Jun 15, 2026
@aydaeslami aydaeslami added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants