From 11daf20c4c7c6bb9496351842affda2d99a5ac7a Mon Sep 17 00:00:00 2001 From: Dmytro Petrykiv Date: Sat, 30 Sep 2023 15:05:17 +0300 Subject: [PATCH 1/3] Solution: created decorator "Cache" --- app/main.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..b3ca708f 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,14 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_dict = {} + + def wrapper(*args) -> int: + if args in cache_dict: + print("Getting from cache") + else: + new_result = func(*args) + cache_dict[args] = new_result + print("Calculating new result") + return cache_dict[args] + return wrapper From 20e527b40de39b269243df9421e30a327fc86408 Mon Sep 17 00:00:00 2001 From: Dmytro Petrykiv <111526221+petrykivd@users.noreply.github.com> Date: Mon, 2 Oct 2023 18:01:36 +0300 Subject: [PATCH 2/3] Fixed type annotation [int -> Any] --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index b3ca708f..d7060bea 100644 --- a/app/main.py +++ b/app/main.py @@ -1,10 +1,10 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: cache_dict = {} - def wrapper(*args) -> int: + def wrapper(*args) -> Any: if args in cache_dict: print("Getting from cache") else: From 3ab8c25fe7f6ea32c73d12d97f41a90e2c76876e Mon Sep 17 00:00:00 2001 From: Dmytro Petrykiv <111526221+petrykivd@users.noreply.github.com> Date: Mon, 2 Oct 2023 18:30:27 +0300 Subject: [PATCH 3/3] Fixed using additional variable --- app/main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index d7060bea..f98e8c62 100644 --- a/app/main.py +++ b/app/main.py @@ -8,8 +8,7 @@ def wrapper(*args) -> Any: if args in cache_dict: print("Getting from cache") else: - new_result = func(*args) - cache_dict[args] = new_result + cache_dict[args] = func(*args) print("Calculating new result") return cache_dict[args] return wrapper