From f9ce8986e8a5141d0076e0d4c2ba502ed912f3be Mon Sep 17 00:00:00 2001 From: Vladyslav Shatrovskyi Date: Fri, 29 Sep 2023 16:20:42 +0300 Subject: [PATCH 1/8] Solution --- app/main.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..2fcb96cd 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,33 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + cached_data = {} + + def inner(*args, **kwargs): + #print(cached_data) + if args in cached_data.keys(): + print("Getting from cache") + else: + new_cach = func(*args) + cached_data[args] = new_cach + print("Calculating new result") + + return func(*args) if args not in cached_data.keys() else cached_data[args] + + return inner + +@cache +def long_time_func(a: int, b: int, c: int) -> int: + 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] + + +long_time_func(1, 2, 3) +long_time_func(2, 2, 3) +long_time_func_2((5, 6, 7), 5) +long_time_func(1, 2, 3) +long_time_func_2((5, 6, 7), 10) +long_time_func_2((5, 6, 7), 10) From 27bbc9606c028fcc76decdd6dd937920f60caaf2 Mon Sep 17 00:00:00 2001 From: Vladyslav Shatrovskyi Date: Fri, 29 Sep 2023 16:22:45 +0300 Subject: [PATCH 2/8] Solution --- app/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 2fcb96cd..7981d019 100644 --- a/app/main.py +++ b/app/main.py @@ -5,12 +5,11 @@ def cache(func: Callable) -> Callable: cached_data = {} def inner(*args, **kwargs): - #print(cached_data) if args in cached_data.keys(): print("Getting from cache") else: - new_cach = func(*args) - cached_data[args] = new_cach + new_cache = func(*args) + cached_data[args] = new_cache print("Calculating new result") return func(*args) if args not in cached_data.keys() else cached_data[args] From b3cda93396f70f3e02787122eb4fbce15333fdd1 Mon Sep 17 00:00:00 2001 From: Vladyslav Shatrovskyi Date: Fri, 29 Sep 2023 16:29:49 +0300 Subject: [PATCH 3/8] Solution --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 7981d019..8b5e09c3 100644 --- a/app/main.py +++ b/app/main.py @@ -4,7 +4,7 @@ def cache(func: Callable) -> Callable: cached_data = {} - def inner(*args, **kwargs): + def inner(*args, **kwargs) -> None: if args in cached_data.keys(): print("Getting from cache") else: From 09fbf302bb77d01f33f504c1452c32be94d9e89e Mon Sep 17 00:00:00 2001 From: Vladyslav Shatrovskyi Date: Fri, 29 Sep 2023 16:31:19 +0300 Subject: [PATCH 4/8] Solution] --- app/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/main.py b/app/main.py index 8b5e09c3..3bab0884 100644 --- a/app/main.py +++ b/app/main.py @@ -16,6 +16,7 @@ def inner(*args, **kwargs) -> None: return inner + @cache def long_time_func(a: int, b: int, c: int) -> int: return (a ** b ** c) % (a * c) From bc7308a3c0065c755002c87ced31dd89860aeb6e Mon Sep 17 00:00:00 2001 From: Vladyslav Shatrovskyi Date: Fri, 29 Sep 2023 16:33:25 +0300 Subject: [PATCH 5/8] Solution] --- app/main.py | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/app/main.py b/app/main.py index 3bab0884..f71eabb0 100644 --- a/app/main.py +++ b/app/main.py @@ -15,20 +15,3 @@ def inner(*args, **kwargs) -> None: return func(*args) if args not in cached_data.keys() else cached_data[args] return inner - - -@cache -def long_time_func(a: int, b: int, c: int) -> int: - 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] - - -long_time_func(1, 2, 3) -long_time_func(2, 2, 3) -long_time_func_2((5, 6, 7), 5) -long_time_func(1, 2, 3) -long_time_func_2((5, 6, 7), 10) -long_time_func_2((5, 6, 7), 10) From a2a1ce95f49581fc431132aede2deef347d18540 Mon Sep 17 00:00:00 2001 From: Vladyslav Shatrovskyi Date: Fri, 29 Sep 2023 16:36:00 +0300 Subject: [PATCH 6/8] Solution] --- app/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index f71eabb0..c913a7aa 100644 --- a/app/main.py +++ b/app/main.py @@ -2,16 +2,16 @@ def cache(func: Callable) -> Callable: - cached_data = {} + cached = {} def inner(*args, **kwargs) -> None: - if args in cached_data.keys(): + if args in cached.keys(): print("Getting from cache") else: new_cache = func(*args) - cached_data[args] = new_cache + cached[args] = new_cache print("Calculating new result") - return func(*args) if args not in cached_data.keys() else cached_data[args] + return func(*args) if args not in cached.keys() else cached[args] return inner From c16ff1171d63438b54513715af6db2f0c7235325 Mon Sep 17 00:00:00 2001 From: Vladyslav Shatrovskyi Date: Fri, 29 Sep 2023 19:36:58 +0300 Subject: [PATCH 7/8] Improved Solution --- app/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index c913a7aa..7712ce04 100644 --- a/app/main.py +++ b/app/main.py @@ -1,11 +1,11 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: cached = {} - def inner(*args, **kwargs) -> None: - if args in cached.keys(): + def inner(*args, **kwargs) -> Any: + if args in cached: print("Getting from cache") else: new_cache = func(*args) From 43d49ab9c13286fc805fbf968176cbc7f2473eb3 Mon Sep 17 00:00:00 2001 From: Vladyslav Shatrovskyi Date: Fri, 29 Sep 2023 21:07:14 +0300 Subject: [PATCH 8/8] Improved Solution --- app/main.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 7712ce04..4f397db7 100644 --- a/app/main.py +++ b/app/main.py @@ -5,13 +5,14 @@ def cache(func: Callable) -> Callable: cached = {} def inner(*args, **kwargs) -> Any: - if args in cached: - print("Getting from cache") - else: + + if args not in cached: new_cache = func(*args) cached[args] = new_cache print("Calculating new result") + else: + print("Getting from cache") - return func(*args) if args not in cached.keys() else cached[args] + return cached[args] return inner