From fd44ce1fd994fb80aac693bb3a416215d3ecb42d Mon Sep 17 00:00:00 2001 From: Serhii Date: Fri, 18 Oct 2024 18:45:55 +0300 Subject: [PATCH 1/3] solution --- app/main.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..b7a7d3dd 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,27 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: - # Write your code here - pass + func_cache = {} + + def wrapper(*args) -> Any: + if args in func_cache: + print("Getting from cache") + return func_cache[args] + else: + print("Calculating new result") + result = func(*args) + func_cache[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(n_tuple: tuple, power: int) -> Any: + return [number ** power for number in n_tuple] From fcc3d4625c2e95570cb2605595b3f44c66d0121e Mon Sep 17 00:00:00 2001 From: Serhii Date: Fri, 18 Oct 2024 18:48:52 +0300 Subject: [PATCH 2/3] solution --- app/main.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/app/main.py b/app/main.py index b7a7d3dd..57f9b3fb 100644 --- a/app/main.py +++ b/app/main.py @@ -15,13 +15,3 @@ def wrapper(*args) -> Any: 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(n_tuple: tuple, power: int) -> Any: - return [number ** power for number in n_tuple] From f83fbb571269d954e4b24e387b30a39cbbeb40fc Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 23 Oct 2024 19:16:51 +0300 Subject: [PATCH 3/3] solution --- app/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/main.py b/app/main.py index 57f9b3fb..0ef67da0 100644 --- a/app/main.py +++ b/app/main.py @@ -10,6 +10,7 @@ def wrapper(*args) -> Any: return func_cache[args] else: print("Calculating new result") + result = func(*args) func_cache[args] = result return result