From 1ec336f3cc75077776e3259bffdaf2261407e96e Mon Sep 17 00:00:00 2001 From: Alina Bondarenko Date: Wed, 11 Dec 2024 12:55:12 +0200 Subject: [PATCH 1/2] Solution --- app/main.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..d2bcf3d7 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,15 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_func = {} + def wrapper(*args, **kwargs) -> str: + key = (args, frozenset(kwargs.items())) + if key in cache_func: + print("Getting from cache") + return cache_func[key] + else: + print("Calculating new result") + result = func(*args, **kwargs) + cache_func[key] = result + return result + return wrapper From bf385d1aa2cc6f39f9a9d10e9dfad66fcbf73da0 Mon Sep 17 00:00:00 2001 From: Alina Bondarenko Date: Wed, 11 Dec 2024 13:06:47 +0200 Subject: [PATCH 2/2] Solution --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index d2bcf3d7..07ea1219 100644 --- a/app/main.py +++ b/app/main.py @@ -1,9 +1,9 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: cache_func = {} - def wrapper(*args, **kwargs) -> str: + def wrapper(*args, **kwargs) -> Any: key = (args, frozenset(kwargs.items())) if key in cache_func: print("Getting from cache")