From 82317fda9d53fa71948434e8204127ba405696d1 Mon Sep 17 00:00:00 2001 From: WoyDiam Date: Thu, 21 Sep 2023 19:57:05 +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..d7a428b0 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 + cache_dict = {} + + def wrapper(*args, **kwargs) -> Any: + key = (args, frozenset(kwargs.items())) + if key in cache_dict: + print("Getting from cache") + return cache_dict[key] + else: + print("Calculating new result") + result = func(*args, **kwargs) + cache_dict[key] = result + return result + return wrapper From c8c40351d4a98c88a27fa40b4eb1650c040eb21e Mon Sep 17 00:00:00 2001 From: WoyDiam Date: Thu, 21 Sep 2023 23:26:31 +0300 Subject: [PATCH 2/2] Solution --- app/main.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/main.py b/app/main.py index d7a428b0..48f88630 100644 --- a/app/main.py +++ b/app/main.py @@ -6,12 +6,11 @@ def cache(func: Callable) -> Callable: def wrapper(*args, **kwargs) -> Any: key = (args, frozenset(kwargs.items())) - if key in cache_dict: - print("Getting from cache") - return cache_dict[key] - else: + if key not in cache_dict: print("Calculating new result") - result = func(*args, **kwargs) - cache_dict[key] = result - return result + cache_dict[key] = func(*args, **kwargs) + else: + print("Getting from cache") + return cache_dict[key] + return wrapper