From 5160c4b754d80cd3484da4afe32be6f1d3d260c1 Mon Sep 17 00:00:00 2001 From: zaeba Date: Wed, 18 Dec 2024 13:37:48 +0200 Subject: [PATCH] easy --- app/main.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..a78192c9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,29 @@ -from typing import Callable +from functools import wraps +from typing import Callable, Tuple, Any def cache(func: Callable) -> Callable: - # Write your code here - pass + func_cache = {} + + @wraps(func) + def wrapper(*args: Any) -> Any: + if args in func_cache: + print("Getting from cache") + return func_cache[args] + else: + print("Calculating new result") + result = func(*args) + func_cache[args] = result + return result + + return wrapper + + +@cache +def long_time_func(base: int, exponent: int, multiplier: int) -> int: + return (base ** exponent ** multiplier) % (base * multiplier) + + +@cache +def long_time_func_2(numbers: Tuple[int, ...], power: int) -> list[int]: + return [number ** power for number in numbers]