Add Functions We often use lambda functions as arguments to other functions. For
ID: 3599578 • Letter: A
Question
Add Functions We often use lambda functions as arguments to other functions. For example, if persons is a list of Person objects with a .name attribute, we could sort the list by name using persons.sort (key-lambda p: p.name) In this question, we want you to write a function add_functions(f, g) which returns a function that accepts a single argument and returns f(x) g(x) In other words, we would use your function something like this f add-functions(lambda x: x*2, f(2) # returns (2*2) + (2 + 1) lambda 1) x: x + 7 Hint: You can directly return a lambda Write add_functions(f, g) Submitted: No f g g lambda x: x; g = add add-functions(lambda add-functions(lambda functions(f, f) g(2) -> 4 x: x*2, lambda x: x + 1); g(2) -> 7 x: x - 2, lambda x: x**3); g(2) -> 8 = = = name add functions' is not definedExplanation / Answer
def add_functions(f, g):
return lambda x: f(x) + g(x)
f = add_functions(lambda x: x*2, lambda x: x+1)
print(f(2))
# copy pastable code: https://paste.ee/p/uXZkI
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.