Skip to content

Commit

Permalink
'cache-decorator'
Browse files Browse the repository at this point in the history
  • Loading branch information
TrMaksym committed Oct 30, 2024
1 parent 5891c38 commit 037b8f8
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,33 @@


def cache(func: Callable) -> Callable:
# Write your code here
pass
cache_data = {}

def wrapper(*args) -> Callable:
if args in cache_data:
print("Getting from cache")
return cache_data[args]
print("Calculating new result")
result = func(*args)
cache_data[args] = result
return result

return wrapper


@cache
def long_time_func(a: int, b: int, c: int) -> int:
return (a ** b ** c) % (a * c)


@cache
def long_time_func_2(numbers_tuple: tuple, power: int) -> list:
return [number ** power for number in numbers_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)

0 comments on commit 037b8f8

Please sign in to comment.