From 5f4dbd3aada256dbba5323a2fa8ad1e23850127e Mon Sep 17 00:00:00 2001 From: Serhii Fedorov Date: Thu, 28 Sep 2023 23:08:40 +0300 Subject: [PATCH 1/3] 'Solution' --- app/main.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..b5588bf6 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,19 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_dict = {} + + def inner(*args, **kwargs) -> None: + key = args + + if key in cache_dict: + print("Getting from cache") + return cache_dict[key] + else: + print("Calculating new result") + result = func(*args, **kwargs) + cache_dict[key] = result + + return result + + return inner From c234964f77bda40ea5d5b5513c3ca2bfb77c5908 Mon Sep 17 00:00:00 2001 From: Serhii Fedorov Date: Fri, 29 Sep 2023 23:49:33 +0300 Subject: [PATCH 2/3] 'Improving' --- app/main.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/app/main.py b/app/main.py index b5588bf6..3f929728 100644 --- a/app/main.py +++ b/app/main.py @@ -4,17 +4,15 @@ def cache(func: Callable) -> Callable: cache_dict = {} - def inner(*args, **kwargs) -> None: - key = args + def inner(*args, **kwargs) -> [list, int]: - if key in cache_dict: + if args in cache_dict: print("Getting from cache") - return cache_dict[key] - else: - print("Calculating new result") - result = func(*args, **kwargs) - cache_dict[key] = result + return cache_dict[args] - return result + print("Calculating new result") + cache_dict[args] = func(*args, **kwargs) + + return cache_dict[args] return inner From 41e5465081d19b356bc99f548c2250b85dca1dfa Mon Sep 17 00:00:00 2001 From: Serhii Fedorov Date: Mon, 2 Oct 2023 19:10:46 +0300 Subject: [PATCH 3/3] Fixing annotation and redundant return instruction --- app/main.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index 3f929728..c006514f 100644 --- a/app/main.py +++ b/app/main.py @@ -1,17 +1,16 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: cache_dict = {} - def inner(*args, **kwargs) -> [list, int]: + def inner(*args, **kwargs) -> Any: if args in cache_dict: print("Getting from cache") - return cache_dict[args] - - print("Calculating new result") - cache_dict[args] = func(*args, **kwargs) + else: + print("Calculating new result") + cache_dict[args] = func(*args, **kwargs) return cache_dict[args]