diff --git a/app/main.py b/app/main.py index 68287892..edc40030 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,24 @@ -from typing import Callable +from typing import Callable, Any +import functools def cache(func: Callable) -> Callable: - # Write your code here - pass + calculated_data = [] + + @functools.wraps(func) + def wrapper(*args, **kwargs) -> Any: + for value in args: + if isinstance(value, (list, set, dict)): + return None + for value in kwargs.values(): + if isinstance(value, (list, set, dict)): + return None + for args_save, kwargs_save, result_save in calculated_data: + if args_save == args and kwargs_save == kwargs: + print("Getting from cache") + return result_save + result = func(*args, **kwargs) + calculated_data.append((args, kwargs, result)) + print("Calculating new result") + return result + return wrapper