10.4k views
4 votes
Consider the function extract_and_apply(l, p, f) shown below, which extracts the elements of a list l satisfying a boolean predicate p, applies a function f to each such element, and returns the result. What does the function do? a) Extracts and applies a function to all elements in the list l. b) Extracts elements satisfying the boolean predicate p and applies function f to them. c) Extracts and applies a function to all elements in the list l, regardless of the boolean predicate. d) DNE (Does Not Exist)

User Jjpcondor
by
6.8k points

1 Answer

3 votes

The function do option b) Extracts elements satisfying the boolean predicate p and applies function f to them.

What is the boolean function?

This function above is one that takes a list l, a boolean predicate p, as well as a function f. It returns a new list containing the results of applying f to each element of l that satisfies p.

So, the function extract_and_apply is one that can be used to extract and apply a function to a subset of the elements in a list .The final result is a list that is said to have the results of applying the function f to elements that handles all the predicate p.

See text below

[5 points] [5 points] Consider the function extract_and_apply(i, p, f) shown below, which extracts the elements of a list 1 satisfying a boolean predicate p, applies a function f to each such element, and returns the result.

def extract_and_apply(1, P, f):

result = []

for x in 1:

if p(x):

result.append(f(x))

return result

Rewrite extract_and_apply(i, p, f) in one line using a list comprehension.

What does the function do? a) Extracts and applies a function to all elements in the list l. b) Extracts elements satisfying the boolean predicate p and applies function f to them. c) Extracts and applies a function to all elements in the list l, regardless of the boolean predicate. d) DNE (Does Not Exist)

User Brad Lord
by
7.2k points