Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Trynadis committed Nov 13, 2024
1 parent 5891c38 commit 736c0e1
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
from typing import Callable
import functools


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

@functools.wraps(func)
def wrapper(*args: any, **kwargs: any) -> any:
# Sort the keyword arguments
kwargs_tuple = tuple(sorted(kwargs.items()))
key = (args, kwargs_tuple)
# Check if this key exists in the cache
if key in cache_storage:
print("Getting from cache")
return cache_storage[key]
# Calculate and store the result
print("Calculating new result")
result = func(*args, **kwargs)
cache_storage[key] = result
return result
return wrapper

0 comments on commit 736c0e1

Please sign in to comment.