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

'cache-decorator' #1411

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
32 changes: 30 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

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.

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)

Choose a reason for hiding this comment

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

When calling long_time_func_2, ensure that the tuple contains only immutable elements. If the tuple contains mutable elements, it will raise a TypeError because such tuples cannot be used as dictionary keys.

long_time_func(1, 2, 3)
long_time_func_2((5, 6, 7), 10)

Choose a reason for hiding this comment

The 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 contains only immutable elements to avoid TypeError when using it as a dictionary key.

long_time_func_2((5, 6, 7), 10)

Choose a reason for hiding this comment

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

Ensure that the tuple passed to long_time_func_2 contains only immutable elements to avoid TypeError when using it as a dictionary key.

Loading