Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a parameterized decorator @cached that transforms a Python function into one that caches its results, so that subsequent calls with equivalent arguments can be
Write a parameterized decorator @cached that transforms a Python function into one that caches its results, so that subsequent calls with equivalent arguments can be fulfilled by looking up the previous result in a cache, rather than re-calculating the result by calling the original function. One argument is required to be passed to@cached, an integer specifying the size of the cache (i.e., the number of different previous results to store in the cache). Of course, results aren't returned from the cache indiscriminately; only when a subsequent call has arguments - including both positional and keyword arguments - that are equivalent to a previous call will the cache be utilized. That's why, in the example above, only the second call to expensive_square( 3 ) was able to run in fewer than ten seconds; the cache can't know a result until it's seen it already. There are four basic requirements that need to be met. - When a call is made whose arguments are equivalent to those from a previous call, the stored result is returned instead of calling the original function. (Positional arguments match when they have equivalent values in the same positions; keyword arguments match when they have equivalent names and values.) - When a call is made whose arguments are not equivalent to those from a previous call, the original function is called normally, and its result is stored in the cache for subsequent use. - When the cache's size reaches its limit, adding a new result to the cache requires evicting one of the existing ones. This is to be done randomly (i.e., choose a result already in the cache at random, remove it from the cache, and then store the new result). - When any argument has a value that is not hashable, the cache is not consulted at all, the original function is called normally, and the result is returned without storing it in the cache. (Your cache would presumbly be a dictionary, with arguments used to form keys; dictionaries can't have keys that are not hashable. But, additionally, non-hashable objects are quite often also mutable, which is problematic in the case of caching, since we need a result to be associated with the original arguments, rather than subsequently modified versions of them.) Limitations The Python standard library is entirely off-limits in this problem, except for the random module, which you may find useful for choosing results to evict from the cache. Your function must not require any other module to be imported. What to submit Submit one Python script named problem3. py, which contains your decorator, any helper functions or classes required by it, and nothing else. Neither docstrings, comments, nor type annotations are required, since we've all agreed already on what problems we're solving here. There is no credit being offered for writing automated tests - though you certainly might want to, since that's a great way to ensure that your code works as you expect - and we'd prefer you not submit them even if you do
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started