Skip to content
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

'Solution' #768

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
from typing import Callable
from typing import Callable, Any, Tuple


def cache(func: Callable) -> Callable:
# Write your code here
pass
def cache(func: Callable[..., Any]) -> Callable[..., Any]:
results_cache = {}

def wrapper(*args: Tuple, **kwargs: Any) -> Any:

Choose a reason for hiding this comment

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

kwargs is a dictionary, and you don't really need to specify types for args and kwargs here


key = (args, frozenset(kwargs.items()))

if key in results_cache:
print("Getting from cache")
return results_cache[key]
else:
print("Calculating new result")
result = func(*args, **kwargs)
results_cache[key] = result
return result

Choose a reason for hiding this comment

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

Try to rewrite without return duplicating


return wrapper