Skip to content
Open
Changes from all 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
36 changes: 33 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
from typing import Callable
from functools import wraps
from typing import Callable, Any, List


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

@wraps(func)
def wrapper(*args, **kwargs) -> Any:
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(base: int, power: int, mod: int) -> int:
return (base ** power ** mod) % (base * mod)


@cache
def long_time_func_2(n_tuple: tuple, power: int) -> 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