Skip to content

Added LFU Cache #2151

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

Merged
merged 4 commits into from
Jun 25, 2020
Merged

Added LFU Cache #2151

merged 4 commits into from
Jun 25, 2020

Conversation

ruppysuppy
Copy link
Member

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@TravisBuddy
Copy link

Travis tests have failed

Hey @ruppysuppy,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: b7131530-b5e9-11ea-8cfa-25e523bed74d

@cclauss
Copy link
Member

cclauss commented Jun 24, 2020

The comment said that None was returned but the failing test demonstrates that is not the case.

@ruppysuppy
Copy link
Member Author

ruppysuppy commented Jun 24, 2020

@cclauss, when None is returned in python interpreter or doctests, its not shown. For example try this code in the interpreter:

>>> def f():
	return None

>>> f()
>>> 

Even though f returns None, running f() won't display None in the interpreter or doctests (unless print(f()) is explicitly called)



class DoubleLinkedListNode:
'''
Copy link
Member

Choose a reason for hiding this comment

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

Please change ''' to """ in docstrings

Copy link
Member

Choose a reason for hiding this comment

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

autoblack will do this automatically so let's not slow the review process on this.

LFU Cache to store a given capacity of data. Can be used as a stand-alone object
or as a function decorator.

>>> cache = LFUCache(2)
Copy link
Member

@l3str4nge l3str4nge Jun 25, 2020

Choose a reason for hiding this comment

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

Could LFUCache take optional cache parameter? Would be easier to test.

 def __init__(self, capacity: int, cache: dict = None):
        self.list = DoubleLinkedList()
        self.capacity = capacity
        self.num_keys = 0
        self.hits = 0
        self.miss = 0
        self.cache = cache or {}

Copy link
Member

Choose a reason for hiding this comment

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

That seems like a big change.

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

LGTM

@cclauss cclauss merged commit 9eb3138 into TheAlgorithms:master Jun 25, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Added LFU Cache

* Update lfu_cache.py

* None is returned

* Add type hints

Co-authored-by: Christian Clauss <cclauss@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants