-
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
'cache-decorator' #1411
base: master
Are you sure you want to change the base?
'cache-decorator' #1411
Changes from all commits
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 |
---|---|---|
|
@@ -2,5 +2,33 @@ | |
|
||
|
||
def cache(func: Callable) -> Callable: | ||
# Write your code here | ||
pass | ||
cache_data = {} | ||
|
||
def wrapper(*args) -> Callable: | ||
if args in cache_data: | ||
print("Getting from cache") | ||
return cache_data[args] | ||
print("Calculating new result") | ||
result = func(*args) | ||
cache_data[args] = result | ||
return result | ||
|
||
return wrapper | ||
|
||
|
||
@cache | ||
def long_time_func(a: int, b: int, c: int) -> int: | ||
return (a ** b ** c) % (a * c) | ||
|
||
|
||
@cache | ||
def long_time_func_2(numbers_tuple: tuple, power: int) -> list: | ||
return [number ** power for number in numbers_tuple] | ||
|
||
|
||
long_time_func(1, 2, 3) | ||
long_time_func(2, 2, 3) | ||
long_time_func_2((5, 6, 7), 5) | ||
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. When calling |
||
long_time_func(1, 2, 3) | ||
long_time_func_2((5, 6, 7), 10) | ||
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. Similar to the previous comment, ensure that the tuple passed to |
||
long_time_func_2((5, 6, 7), 10) | ||
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. Ensure that the tuple passed to |
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.
The function
long_time_func_2
takes a tuple and an integer as arguments. Ensure that the tuple contains only immutable elements, as mutable elements like lists cannot be used as dictionary keys for caching.