-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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' #1387
base: master
Are you sure you want to change the base?
'solution' #1387
Changes from 3 commits
480dc98
c8da437
d242dd1
c0004cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
from typing import Callable | ||
from collections.abc import Callable | ||
|
||
|
||
def cache(func: Callable) -> Callable: | ||
# Write your code here | ||
pass | ||
storage = {} | ||
|
||
def wrapper(*args) -> Callable: | ||
if args not in storage: | ||
print("Calculating new result") | ||
return storage.setdefault(args, func(*args)) | ||
else: | ||
print("Getting from cache") | ||
return storage.get(args) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to rewrite wrapper not using a duplicated |
||
return wrapper | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always add a blank line at the end of the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeh, sure, but it is there (flake8 always push me to do this) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In many cases , it is better not to use
not
, because we always thinking if somethingis in
but notis not in
. But a it's a minor thing, that can save you a 3 symbols xDBy the way, it's opinion from my side for this task, sometimes we really need to check
not in
because it looks better with variable names and task requirements.At that task, if you swap you
if
code andelse
to avoid using not, nothing will change logically!