From 7a5ad96868511d3730d26a93cdf9372db0ab3f82 Mon Sep 17 00:00:00 2001 From: dmytro0908 Date: Mon, 9 Dec 2024 17:29:47 +0200 Subject: [PATCH] Solution --- app/main.py | 21 ++++++++++++++++++--- checklist.md | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..6aa9b137 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,21 @@ -from typing import Callable +from typing import Callable, Any, Dict, Tuple, Hashable def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_store: Dict[Callable, Dict[Tuple[Hashable, ...], Any]] = {} + + def wrapper(*args, **kwargs) -> Any: + if func not in cache_store: + cache_store[func] = {} + key = (args, frozenset(kwargs.items())) + + if key in cache_store[func]: + print("Getting from cache") + return cache_store[func][key] + + print("Calculating new result") + result = func(*args, **kwargs) + cache_store[func][key] = result + return result + + return wrapper diff --git a/checklist.md b/checklist.md index e1cbafc2..83ffb14b 100644 --- a/checklist.md +++ b/checklist.md @@ -1,4 +1,4 @@ -# Сheck Your Code Against the Following Points +v# Сheck Your Code Against the Following Points ## Code Efficiency