Question: PYTHON PROBLEM Current function: def delay(f : callable, n : int) -> callable: pass My attempt in which I am having trouble understanding how to
PYTHON PROBLEM

Current function:
def delay(f : callable, n : int) -> callable:
pass
My attempt in which I am having trouble understanding how to do the problem:
def delay(f : callable, n : int) -> callable: if n >= 0: raise AssertionError('Must be negative number') f(n)
2. (4 pts) Define the delay function, which takes two arguments: (1) a function -call it f- (it is univariate: called with one int argument and returning an int result) and (2) an int value -call it n; if n is not a non- negative int, raise an AssertionBrror exception (with an appropriate error message). The delay function returns a reference to a function defined inside it: this internal function that takes a one int argument; when we call this internal function the first n times it computes f on the argument, but returns None; when we call it the n+1s time it returns the value produced by calling f when called on its first argument. Generally, after returning None n times, it returns the value of the nth previous call (that is the "delay") For example, if f is the kind of function described above, and we define g delay(f, 2), then calling g (1) returns None; then calling g (2) returns None (now we have returned None 2 times); then calling g (3) returns f (1): the result computed by f the first time (2 times previously; a delay of 2) g was called; then calling g (4) returns f (2): the result computed by f the second time (another a delay of 2) g was called. Hint: besides its parameters, delay should declare a local list of the values (initialized with n None values) and declare/return a local function that when called (1) adds a new value computed by f at the end of this list but (2) returns the value at the front of the list. See the pop function on lists
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
