From 48088b4b82281c5b6bf2083e04fd61995609d666 Mon Sep 17 00:00:00 2001 From: Anatolii Date: Thu, 17 Oct 2024 11:46:50 -0400 Subject: [PATCH] Solution --- app/main.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..5a28c1f8 100644 --- a/app/main.py +++ b/app/main.py @@ -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]