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
34 changes: 32 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
from functools import wraps
from typing import Callable


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

@wraps(func)
def wrapper(*args, **kwargs) -> any:
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 is annotated with the builtin any type (-> any) which is incorrect. Use typing.Any (capital A) and add it to the imports (for example: from typing import Callable, Any). This will make the type hint accurate.

key = (args, tuple(sorted(kwargs.items())))
if key in stored_results:
print("Getting from cache")
return stored_results[key]
print("Calculating new result")
stored_results[key] = func(*args, **kwargs)
return stored_results[key]

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(n_tuple: tuple, power: int) -> int:
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 function long_time_func_2 is annotated as returning int but actually returns a list ([number ** power for number in n_tuple]). Update the return annotation to list or List[int] and import List from typing if you choose List[int].

return [number ** power for number in n_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)
Comment on lines +31 to +36
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These calls run at import time and produce side effects. Remove these demonstration calls or move them under an if __name__ == "__main__": guard so the module has no side effects on import. This violates the checklist item: "Add comments, prints, and functions to check your solution when you write your code. Don't forget to delete them when you are ready to commit and push your solution."

Comment on lines +31 to +36
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 violates the checklist item: "2. Add comments, prints, and functions to check your solution when you write your code. Don't forget to delete them when you are ready to commit and push your code." The calls at module level cause side effects on import. Move or wrap these demonstration calls (lines 31–36) inside an if __name__ == "__main__": block (or remove them) so the module is import-safe.

Loading