From 2dc274d0f0176efba1e88bf3208e9e8b46c672e3 Mon Sep 17 00:00:00 2001 From: Vova Date: Tue, 12 Nov 2024 18:06:59 +0200 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..ba7c4fdf 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 + storage = {} + + def wrapper(*args: list) -> Any: + if args in storage: + print("Getting from cache") + return storage[args] + else: + result = func(*args) + storage[args] = result + print("Calculating new result") + return result + + return wrapper From 481f2af0cb366fc5061ee8359b66a58d1e07d410 Mon Sep 17 00:00:00 2001 From: Vova Date: Wed, 13 Nov 2024 12:58:41 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=C3=91changed=20solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index ba7c4fdf..4d74cbdd 100644 --- a/app/main.py +++ b/app/main.py @@ -4,14 +4,17 @@ def cache(func: Callable) -> Callable: storage = {} - def wrapper(*args: list) -> Any: - if args in storage: + def wrapper(*args: Any, **kwargs: Any) -> Any: + key = (args, tuple(sorted(kwargs.items()))) + + if key in storage: print("Getting from cache") - return storage[args] + return storage[key] else: - result = func(*args) - storage[args] = result + result = func(*args, **kwargs) + storage[key] = result print("Calculating new result") return result return wrapper +