From e57c7e0cede1fa6564dfad51e24643cd52b74238 Mon Sep 17 00:00:00 2001 From: Yurii Lesyk Date: Sat, 30 Nov 2024 16:30:09 +0200 Subject: [PATCH 1/4] 'Solution' --- app/main.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..623ed203 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,17 @@ -from typing import Callable +from typing import Callable, Dict, Tuple def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_store: Dict[Tuple, any] = {} + + + def wrapper(*args): + if args in cache_store: + print("Getting from cache") + return cache_store[args] + else: + print("Calculating new result") + result = func(*args) + cache_store[args] = result + return result + return wrapper() From 16ed7b81fa327491d1a8aecaa5c58c1e73f0c445 Mon Sep 17 00:00:00 2001 From: Yurii Lesyk Date: Sat, 30 Nov 2024 16:36:05 +0200 Subject: [PATCH 2/4] 'Solution' --- app/main.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index 623ed203..b81d638a 100644 --- a/app/main.py +++ b/app/main.py @@ -1,11 +1,10 @@ -from typing import Callable, Dict, Tuple +from typing import Callable, Dict, Tuple, Any def cache(func: Callable) -> Callable: - cache_store: Dict[Tuple, any] = {} + cache_store: Dict[Tuple, Any] = {} - - def wrapper(*args): + def wrapper(*args) -> Any: if args in cache_store: print("Getting from cache") return cache_store[args] @@ -14,4 +13,5 @@ def wrapper(*args): result = func(*args) cache_store[args] = result return result - return wrapper() + + return wrapper From 160f78cba9aa4721f720c5a5f8094a432e80b11a Mon Sep 17 00:00:00 2001 From: Yurii Lesyk Date: Sat, 30 Nov 2024 16:43:29 +0200 Subject: [PATCH 3/4] 'Solution' --- app/main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/main.py b/app/main.py index b81d638a..3123d336 100644 --- a/app/main.py +++ b/app/main.py @@ -2,6 +2,10 @@ def cache(func: Callable) -> Callable: + """ + A decorator that caches the results of function + calls with immutable arguments. + """ cache_store: Dict[Tuple, Any] = {} def wrapper(*args) -> Any: From 71f3046f2cdf05f2c3b976b12447b61e8293767f Mon Sep 17 00:00:00 2001 From: Yurii Lesyk Date: Sat, 30 Nov 2024 16:52:32 +0200 Subject: [PATCH 4/4] 'Solution' --- app/main.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/main.py b/app/main.py index 3123d336..b67b5e9c 100644 --- a/app/main.py +++ b/app/main.py @@ -5,10 +5,19 @@ def cache(func: Callable) -> Callable: """ A decorator that caches the results of function calls with immutable arguments. + + Note: This decorator should only be used with functions + that take immutable arguments (e.g., int, str, tuple). """ cache_store: Dict[Tuple, Any] = {} def wrapper(*args) -> Any: + # Ensure all arguments are immutable + if not all(isinstance(arg, (int, float, str, tuple)) for arg in args): + raise ValueError( + "All arguments must be immutable (int, float, str, tuple)." + ) + if args in cache_store: print("Getting from cache") return cache_store[args]