From f7fdea4b8e71bdd23f371df1cf3d18c687daf33e Mon Sep 17 00:00:00 2001 From: ComfortCityBud Date: Mon, 11 Nov 2024 20:06:36 +0200 Subject: [PATCH 1/3] Solution --- app/main.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..8e6eaaee 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,30 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_dict = {} + + def wrapper(*args, **kwargs) -> None: + flag = True + while flag: + data_iterable = tuple( + [item for item in args if isinstance(item, (int, str, float))] + ) + if data_iterable not in cache_dict: + print("Calculating new result") + cache_dict[data_iterable] = func(*args, **kwargs) + return cache_dict[data_iterable] + else: + print("Getting from cache") + return cache_dict[data_iterable] + + return wrapper + + +@cache +def long_time_func(a: int, b: int, c: int) -> int: # NOQA VNE001 + return (a ** b ** c) % (a * c) + + +@cache +def long_time_func_2(n_tuple: tuple, power: int) -> int: + return [number ** power for number in n_tuple] From 5261779068eae0b3ef04668288510d89b89727de Mon Sep 17 00:00:00 2001 From: ComfortCityBud Date: Mon, 11 Nov 2024 20:14:06 +0200 Subject: [PATCH 2/3] Solution --- app/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 8e6eaaee..9e5234bf 100644 --- a/app/main.py +++ b/app/main.py @@ -1,10 +1,10 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: cache_dict = {} - def wrapper(*args, **kwargs) -> None: + def wrapper(*args, **kwargs) -> Any: flag = True while flag: data_iterable = tuple( @@ -27,5 +27,5 @@ def long_time_func(a: int, b: int, c: int) -> int: # NOQA VNE001 @cache -def long_time_func_2(n_tuple: tuple, power: int) -> int: +def long_time_func_2(n_tuple: tuple, power: int) -> list: return [number ** power for number in n_tuple] From 40acb648ea9ad0aaddb93ae7c16f1541f8d911ab Mon Sep 17 00:00:00 2001 From: ComfortCityBud Date: Tue, 12 Nov 2024 21:06:13 +0200 Subject: [PATCH 3/3] Solution --- app/main.py | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/app/main.py b/app/main.py index 9e5234bf..6d2f460e 100644 --- a/app/main.py +++ b/app/main.py @@ -5,27 +5,12 @@ def cache(func: Callable) -> Callable: cache_dict = {} def wrapper(*args, **kwargs) -> Any: - flag = True - while flag: - data_iterable = tuple( - [item for item in args if isinstance(item, (int, str, float))] - ) - if data_iterable not in cache_dict: - print("Calculating new result") - cache_dict[data_iterable] = func(*args, **kwargs) - return cache_dict[data_iterable] - else: - print("Getting from cache") - return cache_dict[data_iterable] + data_iterable = (args, frozenset(kwargs.items())) + if data_iterable not in cache_dict: + print("Calculating new result") + cache_dict[data_iterable] = func(*args, **kwargs) + return cache_dict[data_iterable] + print("Getting from cache") + return cache_dict[data_iterable] return wrapper - - -@cache -def long_time_func(a: int, b: int, c: int) -> int: # NOQA VNE001 - return (a ** b ** c) % (a * c) - - -@cache -def long_time_func_2(n_tuple: tuple, power: int) -> list: - return [number ** power for number in n_tuple]