Skip to content
Open
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@


def cache(func: Callable) -> Callable:
# Write your code here
pass
cash = []
Comment thread
dianahorban477-bot marked this conversation as resolved.
Outdated
def wrapper(*args, **kwargs) -> None:
Comment thread
dianahorban477-bot marked this conversation as resolved.
Outdated
if args or kwargs in cash:
print ("Calculating new result")
else:
cash.append((args, kwargs))
print ("Getting from cache")
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 block doesn't correctly implement the caching logic. Here's a summary of the issues:

  1. The if condition does not correctly check if the arguments have been seen before.
  2. The print messages for 'Calculating' and 'Getting from cache' are swapped.
  3. The decorated function func is never actually called.
  4. The function's result is neither stored in the cache nor returned.

The correct logic should be:

  • Check if the arguments are in the cache.
  • If yes: print Getting from cache and return the stored value.
  • If no: print Calculating new result, call the original function, store its result in the cache, and then return that result.

return wrapper
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The wrapper function should not return itself. It must return the result of the decorated function, which is either newly calculated or retrieved from the cache.

Loading