From 769c4e27ab9bad7cfd05fa7e6c764825486a33ad Mon Sep 17 00:00:00 2001 From: Eugene Rabenko Date: Sun, 1 Oct 2023 13:14:33 +0300 Subject: [PATCH 1/2] Solution --- app/main.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..33eab968 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,17 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: - # Write your code here - pass + cash_dictionary = {} + + def wrapper(*args, **kwargs) -> Any: + args_key = (args, tuple(kwargs.items())) + if args_key in cash_dictionary: + print("Getting from cache") + return cash_dictionary[args_key] + else: + result = func(*args, **kwargs) + cash_dictionary[args_key] = result + print("Calculating new result") + return result + return wrapper From dfcce5f40a66a2d037ba24c1a6bb174612589eeb Mon Sep 17 00:00:00 2001 From: Eugene Rabenko Date: Mon, 2 Oct 2023 19:12:35 +0300 Subject: [PATCH 2/2] Solution --- app/main.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 33eab968..61329bb9 100644 --- a/app/main.py +++ b/app/main.py @@ -8,10 +8,8 @@ def wrapper(*args, **kwargs) -> Any: args_key = (args, tuple(kwargs.items())) if args_key in cash_dictionary: print("Getting from cache") - return cash_dictionary[args_key] else: - result = func(*args, **kwargs) - cash_dictionary[args_key] = result + cash_dictionary[args_key] = func(*args, **kwargs) print("Calculating new result") - return result + return cash_dictionary[args_key] return wrapper