Problem 8 (200pts)
Implement the make_averaged function, which is a higher-order function that takes a function original_function as an argument.
The return value of make_averaged is a function that takes in the same number of arguments as original_function. When we call this returned function on arguments, it will return the average value of repeatedly calling original_function on the arguments passed in.
Specifically, this function should call original_function a total of trials_count times and return the average of the results of these calls.
Important: To implement this function, you will need to use a new piece of Python syntax. We would like to write a function that accepts an arbitrary number of arguments, and then calls another function using exactly those arguments. Here's how it works.
Instead of listing formal parameters for a function, you can write
*args, which represents all of the arguments that get passed into the function. We can then call another function with these same arguments by passing these*argsinto this other function. For example:>>> def printed(f): ... def print_and_return(*args): ... result = f(*args) ... print('Result:', result) ... return result ... return print_and_return >>> printed_pow = printed(pow) >>> printed_pow(2, 8) Result: 256 256 >>> printed_abs = printed(abs) >>> printed_abs(-10) Result: 10 10Here, we can pass any number of arguments into
print_and_returnvia the*argssyntax. We can also use*argsinside ourprint_and_returnfunction to make another function call with the same arguments.
Read the docstring for make_averaged carefully to understand how it is meant to work.
Before writing any code, unlock the tests to verify your understanding of the question.
$ python ok -q 08 -u
Once you are done unlocking, begin implementing your solution. You can check your correctness with:
$ python ok -q 08