Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
from typing import Callable
from typing import Callable, Any


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

def wrapper(*args, **kwargs) -> Any:
key = (args, frozenset(kwargs.items()))

if key in cache_store:
print("Getting from cache")
return cache_store[key]
else:
print("Calculating new result")
result = func(*args, **kwargs)
Comment on lines +9 to +17
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This uses only args as the cache key and completely ignores kwargs, so calls that differ only by keyword arguments will be cached incorrectly. The task requires a decorator that "stores results of completed runs with different arguments, number of arguments can be also different." Consider creating a composite, hashable key that includes both positional and keyword arguments (for example: key = (args, frozenset(kwargs.items()))).

cache_store[key] = result
return result

return wrapper
Loading