From c8dcf2d7cea84bd0dd0965b5b58cd116b0df073f Mon Sep 17 00:00:00 2001 From: Vishnevskyi Dmytro Date: Fri, 22 Sep 2023 15:57:51 +0300 Subject: [PATCH 1/3] Cache decorator solution --- app/main.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..d592f07c 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,24 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + caches = {} + + def cache_checker(*args) -> Callable: + if args in caches: + print("Getting from cache") + else: + caches[args] = func(*args) + print("Calculating new result") + return caches[args] + + return cache_checker + + +@cache +def long_time_func(a: int, b: int, c: int) -> int: + return (a ** b ** c) % (a * c) + + +long_time_func(1, 2, 3) +long_time_func(1, 2, 3.2) +long_time_func(1, 2, 3) From cef73d66c7c2287fbe7acfd5db185f3d183790be Mon Sep 17 00:00:00 2001 From: Vishnevskyi Dmytro Date: Fri, 22 Sep 2023 15:57:51 +0300 Subject: [PATCH 2/3] Cache decorator solution --- app/main.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index d592f07c..9e9fb2a0 100644 --- a/app/main.py +++ b/app/main.py @@ -16,10 +16,6 @@ def cache_checker(*args) -> Callable: @cache -def long_time_func(a: int, b: int, c: int) -> int: - return (a ** b ** c) % (a * c) +def long_time_func(first: int, second: int, third: int) -> int: + return (first ** second ** third) % (first * third) - -long_time_func(1, 2, 3) -long_time_func(1, 2, 3.2) -long_time_func(1, 2, 3) From f4cdc8bad0bb294a70f96d6caee6bc4adc57a17c Mon Sep 17 00:00:00 2001 From: Vishnevskyi Dmytro Date: Fri, 22 Sep 2023 16:07:19 +0300 Subject: [PATCH 3/3] fix flake8 --- app/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/main.py b/app/main.py index 9e9fb2a0..c007370c 100644 --- a/app/main.py +++ b/app/main.py @@ -18,4 +18,3 @@ def cache_checker(*args) -> Callable: @cache def long_time_func(first: int, second: int, third: int) -> int: return (first ** second ** third) % (first * third) -