-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
5891c38
commit 48088b4
Showing
1 changed file
with
30 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |