From dc7ff7c018040ed0241bd830e25ff648302e0772 Mon Sep 17 00:00:00 2001 From: Tkachov Mykhailo Date: Sun, 6 Oct 2024 17:13:24 +0300 Subject: [PATCH 1/4] Solution --- app/main.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..c793704a 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,23 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + save_result = {} + + def inner(*args) -> Callable: + if save_result: + key_in_save_result = (func, args,) + if key_in_save_result in save_result: + print("Getting from cache") + return save_result[key_in_save_result] + else: + print("Calculating new result") + result = func(*args) + save_result[(func, args,)] = result + return result + else: + print("Calculating new result") + result = func(*args) + save_result[(func, args,)] = result + return result + + return inner From 372fe39888cd5edcaa200c4ad1506505637624c9 Mon Sep 17 00:00:00 2001 From: Tkachov Mykhailo Date: Sun, 6 Oct 2024 20:31:11 +0300 Subject: [PATCH 2/4] DRY and add kwargs --- app/main.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/app/main.py b/app/main.py index c793704a..73cc2867 100644 --- a/app/main.py +++ b/app/main.py @@ -4,21 +4,18 @@ def cache(func: Callable) -> Callable: save_result = {} - def inner(*args) -> Callable: + def inner(*args, **kwargs) -> Callable: + key_in_save_result = (func.__name__, args,) + for value in kwargs.values(): + key_in_save_result.add(value) if save_result: - key_in_save_result = (func, args,) if key_in_save_result in save_result: print("Getting from cache") return save_result[key_in_save_result] - else: - print("Calculating new result") - result = func(*args) - save_result[(func, args,)] = result - return result - else: - print("Calculating new result") - result = func(*args) - save_result[(func, args,)] = result - return result + + print("Calculating new result") + result = func(*args) + save_result[key_in_save_result] = result + return result return inner From e4935e491ade6b0c77c662559b01d887345f4078 Mon Sep 17 00:00:00 2001 From: Tkachov Mykhailo Date: Sun, 6 Oct 2024 21:50:14 +0300 Subject: [PATCH 3/4] solution --- app/main.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 73cc2867..d10bc8f3 100644 --- a/app/main.py +++ b/app/main.py @@ -5,16 +5,14 @@ def cache(func: Callable) -> Callable: save_result = {} def inner(*args, **kwargs) -> Callable: - key_in_save_result = (func.__name__, args,) - for value in kwargs.values(): - key_in_save_result.add(value) + key_in_save_result = (func.__name__, args, tuple(kwargs.items())) if save_result: if key_in_save_result in save_result: print("Getting from cache") return save_result[key_in_save_result] print("Calculating new result") - result = func(*args) + result = func(*args, **kwargs) save_result[key_in_save_result] = result return result From 434675daf39b830dea5c7060b9febd4840443e7e Mon Sep 17 00:00:00 2001 From: Tkachov Mykhailo Date: Sun, 6 Oct 2024 22:19:05 +0300 Subject: [PATCH 4/4] removed unnecessary check --- app/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index d10bc8f3..581c9201 100644 --- a/app/main.py +++ b/app/main.py @@ -6,10 +6,10 @@ def cache(func: Callable) -> Callable: def inner(*args, **kwargs) -> Callable: key_in_save_result = (func.__name__, args, tuple(kwargs.items())) - if save_result: - if key_in_save_result in save_result: - print("Getting from cache") - return save_result[key_in_save_result] + + if key_in_save_result in save_result: + print("Getting from cache") + return save_result[key_in_save_result] print("Calculating new result") result = func(*args, **kwargs)