From b6c2d59fcbf8d9847efd80a1ed0b641f27bacdc1 Mon Sep 17 00:00:00 2001 From: Mariana Lishchynska Date: Tue, 22 Oct 2024 13:18:40 +0300 Subject: [PATCH 1/3] Solution --- app/main.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..5eb52be3 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,13 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_saved_results = {} + + def wrapper(*args) -> int: + if args in cache_saved_results: + print("Getting from cache") + return cache_saved_results[args] + print("Calculating new result") + cache_saved_results[args] = func(*args) + return cache_saved_results[args] + return wrapper From 056a01b5f5d20679c0748a3d1fc1a4eb89a1049c Mon Sep 17 00:00:00 2001 From: Mariana Lishchynska Date: Tue, 26 Nov 2024 12:02:33 +0200 Subject: [PATCH 2/3] 'Fixed' --- app/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 5eb52be3..f72cc6f2 100644 --- a/app/main.py +++ b/app/main.py @@ -4,11 +4,10 @@ def cache(func: Callable) -> Callable: cache_saved_results = {} - def wrapper(*args) -> int: + def wrapper(*args) -> any: if args in cache_saved_results: print("Getting from cache") return cache_saved_results[args] print("Calculating new result") - cache_saved_results[args] = func(*args) - return cache_saved_results[args] + return cache_saved_results.setdefault(args, func(*args)) return wrapper From db4aa66e5f87cf8200c930372144d04d8956468a Mon Sep 17 00:00:00 2001 From: Mariana Lishchynska Date: Wed, 27 Nov 2024 18:17:11 +0200 Subject: [PATCH 3/3] 'Remove_return' --- app/main.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index f72cc6f2..bff78492 100644 --- a/app/main.py +++ b/app/main.py @@ -7,7 +7,8 @@ def cache(func: Callable) -> Callable: def wrapper(*args) -> any: if args in cache_saved_results: print("Getting from cache") - return cache_saved_results[args] - print("Calculating new result") - return cache_saved_results.setdefault(args, func(*args)) + else: + print("Calculating new result") + cache_saved_results[args] = func(*args) + return cache_saved_results[args] return wrapper