Question

Because your functions are objects, they can serve as inputs to other functions! Create a new...

Because your functions are objects, they can serve as inputs to other functions! Create a new function, called "add_or_mult()". This function should take two parameters.

  1. The first parameter will be a function. The argument you pass for the parameter will be either your multiply or your add function.
  2. The second parameter will be a list, to be used by the function you pass as an argument for the first parameter.
  3. Show that you know how to call parameters "out of order" by including positional arguments in your function call.

    Call your "add_or_mult" function, but include the list as the first argument, and your function name as the second. Do not modify any of your functions when completeing this step.

If this is unclear, see the sample output below:

add_or_mult(multiply, [1,3,5,8])
120
add_or_mult(addify, [1,3,5,8])
17

def multiply(ListNum):
  
ans = 1
for x in ListNum:
ans = ans * x
  
return ans

def addify(ListNum):
  
ans = 1
for x in ListNum:
ans = ans + x
  
return ans

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def multiply(ListNum):
    ans = 1
    for x in ListNum:
        ans = ans * x
    return ans


def addify(ListNum):
    ans = 0
    for x in ListNum:
        ans = ans + x
    return ans

def add_or_mult(f, lst):
    return f(lst)

print(add_or_mult(multiply, [1,3,5,8]))
print(add_or_mult(addify, [1,3,5,8]))
Add a comment
Know the answer?
Add Answer to:
Because your functions are objects, they can serve as inputs to other functions! Create a new...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a PowerShell script. Make sure to: Comment your script by including your name, date, and...

    Write a PowerShell script. Make sure to: Comment your script by including your name, date, and short description what the script does The script receives two arguments/parameters, first one should be a string and the second one an integer number. Both should be passed to the script as (positional) arguments/parameters Check if there are two passed parameters Check if passed parameters are as expected (check if first is string and secondis integer). You can use param() here. Displays on the...

  • PYTHON Hello can someone create the following functions? I'm not sure how to start on them....

    PYTHON Hello can someone create the following functions? I'm not sure how to start on them. makeDictionary Define a function named makeDictionary with two parameters. The first argument passed the function must be a list of strings to be used as keys, while the second is a list of the same length containing values (of some type). This function must return a dictionary consisting of the keys paired with the corresponding values. For example, makeDictionary(['apno','value'],['REP-12','450']) must return this dictionary: {'apno':...

  • Please help as soon as you can Thnx! // ==== Callbacks ==== /* Step 1: Create...

    Please help as soon as you can Thnx! // ==== Callbacks ==== /* Step 1: Create a higher-order function * Create a higher-order function named consume with 3 parameters: a, b and cb * The first two parameters can take any argument (we can pass any value as argument) * The last parameter accepts a callback * The consume function should return the invocation of cb, passing a and b into cb as arguments */ /* Step 2: Create several...

  • Python Homework: - NOTE - Must not use built-in functions (other than the range() function), slice...

    Python Homework: - NOTE - Must not use built-in functions (other than the range() function), slice expressions, list methods (other than the append() method) or string methods in your solution. Write a function called reverse(my_list, number=-1) that takes a list and a number as parameters. The function returns a copy of the list with the first number of items reversed. Conditions: - The number parameter must be a default argument. -      If the default argument for number is given in...

  • 1. In Python, 'mutable' objects such as lists are passed to functions 'by reference'. This means:...

    1. In Python, 'mutable' objects such as lists are passed to functions 'by reference'. This means: a.) Changes made to the object within the function persist after the function completes b.) The function receives a copy of the object c.) Python keeps a history of where then object was used d.) Statements in the function can refer to the object by nickname 2. Which of the following is NOT a requirement for a Python function: a.) The function must have...

  • Please place all of the following functions (defun …) into a SINGLE .lsp file As such,...

    Please place all of the following functions (defun …) into a SINGLE .lsp file As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide ​ Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB...

  • PLEASE USE WHILE LOOP (NOT FOR LOOPS). Python programming. In this problem, you should write one...

    PLEASE USE WHILE LOOP (NOT FOR LOOPS). Python programming. In this problem, you should write one function named multiply. This function should accept one parameter, which you can assume will be a list with one or more integers in it. (If you don’t know what I mean when I say “list of integers”, go do the reading!) The function should loop through all of the numbers in the list, and multiply all of the numbers together. It should return the...

  • python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a...

    python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key. 2.Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of...

  • Functionality to build (4 functions) read_csv_header Define a function named read_csv_header with one parameter. This parameter...

    Functionality to build (4 functions) read_csv_header Define a function named read_csv_header with one parameter. This parameter will be a csv reader object (e.g., the value returned when calling the function csv.reader). The first row read using the parameter is the file's header row. The header row contains the keys for the data stored in the CSV file. This function must return a list containing the strings from that header row. The function parameter will be a csv reader object and...

  • FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...

    FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT