Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution
Browse files Browse the repository at this point in the history
Anatolich911 committed Oct 17, 2024
1 parent 5891c38 commit 48088b4
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
from typing import Callable
from functools import wraps
from typing import Callable, Any


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

cache_storage = {}

@wraps(func)
def wrapper(*args: Any) -> Any:
cache_key = (func.__name__, args)

if cache_key in cache_storage:
print("Getting from cache")
return cache_storage[cache_key]
else:
print("Calculating new result")
result = func(*args)
cache_storage[cache_key] = result
return result

return wrapper


@cache
def long_time_func(base: int, exponent: int, modulus: int) -> int:
return (base ** exponent) % (base * modulus)


@cache
def long_time_func_2(n_tuple: tuple, power: int) -> list:
return [number ** power for number in n_tuple]

0 comments on commit 48088b4

Please sign in to comment.