From 736c0e13213c1354bfd54bd3cd9fa1630189248c Mon Sep 17 00:00:00 2001 From: Vitaliy Kozovyi Date: Wed, 13 Nov 2024 22:06:26 +0200 Subject: [PATCH] Solution --- app/main.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..e4e7a646 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,22 @@ from typing import Callable +import functools def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_storage = {} + + @functools.wraps(func) + def wrapper(*args: any, **kwargs: any) -> any: + # Sort the keyword arguments + kwargs_tuple = tuple(sorted(kwargs.items())) + key = (args, kwargs_tuple) + # Check if this key exists in the cache + if key in cache_storage: + print("Getting from cache") + return cache_storage[key] + # Calculate and store the result + print("Calculating new result") + result = func(*args, **kwargs) + cache_storage[key] = result + return result + return wrapper