From 83408979af4ef084b6b4fb1ade503afdec4f2b43 Mon Sep 17 00:00:00 2001 From: Diana Zatvarska Date: Wed, 13 Nov 2024 13:37:12 -0800 Subject: [PATCH 1/5] 'Solution' --- app/main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..dc7d3fda 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,8 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + def wrapper(*args, **kwargs) -> None: + result = func(*args, **kwargs) + return result + return wrapper + From 59d4bd5df41391f4da921279845f5c49733b52d0 Mon Sep 17 00:00:00 2001 From: Diana Zatvarska Date: Wed, 13 Nov 2024 13:40:54 -0800 Subject: [PATCH 2/5] 'Solution' --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index dc7d3fda..4c4aa7ba 100644 --- a/app/main.py +++ b/app/main.py @@ -1,8 +1,8 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: - def wrapper(*args, **kwargs) -> None: + def wrapper(*args, **kwargs) -> Any: result = func(*args, **kwargs) return result return wrapper From b27901d91f46dcf42231ce9f98aba94dd1513c57 Mon Sep 17 00:00:00 2001 From: Diana Zatvarska Date: Wed, 13 Nov 2024 14:34:50 -0800 Subject: [PATCH 3/5] 'Solution' --- app/main.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 4c4aa7ba..2e506fdb 100644 --- a/app/main.py +++ b/app/main.py @@ -1,9 +1,16 @@ from typing import Callable, Any -def cache(func: Callable) -> Callable: +def cache(numero: int) -> Any: def wrapper(*args, **kwargs) -> Any: - result = func(*args, **kwargs) - return result + def inner(func: Callable) -> Any: + results = [] + for _ in range(numero): + results.append(func(*args, **kwargs)) + for result in results: + if results.count(result) > 1: + print(f"Getting from cache {result}") + else: + print(f"Calculating new result {result}") + return inner return wrapper - From e30e5101d0abb7112d86f7dd4cb124eb9d148968 Mon Sep 17 00:00:00 2001 From: Diana Zatvarska Date: Thu, 14 Nov 2024 11:49:09 -0800 Subject: [PATCH 4/5] 'Solution' --- app/main.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/app/main.py b/app/main.py index 2e506fdb..aa84fbd7 100644 --- a/app/main.py +++ b/app/main.py @@ -1,16 +1,14 @@ from typing import Callable, Any -def cache(numero: int) -> Any: +def cache(func: Callable) -> Any: + results = {} + def wrapper(*args, **kwargs) -> Any: - def inner(func: Callable) -> Any: - results = [] - for _ in range(numero): - results.append(func(*args, **kwargs)) - for result in results: - if results.count(result) > 1: - print(f"Getting from cache {result}") - else: - print(f"Calculating new result {result}") - return inner + if args in results: + print(f"Getting from cache {results[args]}") + else: + result = func(*args) + results[args] = result + print(f"Calculating new result {result}") return wrapper From b33fa21985b887b4c54f535429dd8ff649f46762 Mon Sep 17 00:00:00 2001 From: Diana Zatvarska Date: Fri, 15 Nov 2024 17:29:01 -0800 Subject: [PATCH 5/5] 'Solution' --- app/main.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index aa84fbd7..093f52bb 100644 --- a/app/main.py +++ b/app/main.py @@ -4,11 +4,13 @@ def cache(func: Callable) -> Any: results = {} - def wrapper(*args, **kwargs) -> Any: + def wrapper(*args) -> Any: if args in results: - print(f"Getting from cache {results[args]}") + print("Getting from cache") + return results[args] else: result = func(*args) results[args] = result - print(f"Calculating new result {result}") + print("Calculating new result") + return result return wrapper