Complete the code to store the result in cache after computation.
cache['result'] = [1](data)
The function process_data computes the result which we want to cache for reuse.
Complete the code to check if the result is already cached before computing.
if 'result' not in cache: cache['result'] = [1](input_data)
We only compute the result if it is not already in the cache, so we call compute_result.
Fix the error in the code to reuse cached results properly.
def get_result(data): if [1] in cache: return cache['result'] cache['result'] = compute(data) return cache['result']
The key to check in the cache dictionary must be a string, so it should be 'result'.
Fill both blanks to create a cache dictionary that stores results only if input is new.
def cached_compute(input): if input [1] cache: return cache[input] cache[input] = [2](input) return cache[input]
We check if the input is not in cache to avoid recomputation, then compute and store the result.
Fill all three blanks to implement a caching mechanism with a function and cache dictionary.
cache = {}
def [1](x):
if x [2] cache:
return cache[x]
cache[x] = [3](x)
return cache[x]The function get_cached_result checks if x is not in cache, then calls expensive_calculation to compute and store the result.
