Question

python: def functionSolver(function: callable)->str You will be given a function as a parameter, the function you...

python:

def functionSolver(function: callable)->str

You will be given a function as a parameter, the function you are given only accepts two number parameters and produces a float value. It is your job to figure out what mathematical operation the function you are given is performing by passing it many different parameters. The possible operations the function can perform are: add, subtract, multiply, and divide. The given function will only perform a single operation, it will not change after consecutive invocations. The function will execute the operation in the order the parameters are received. You must input your own values and test the expected responses to figure out the operation of the function. Once you have figured it out return the symbol of the operation (+, -, *, /).

Another way to explain what has to happen, your function will receive as a function a parameter lets call it f(x, y), and the output of f(x, y) is z. We know that x and y are numbers and you know that f(x, y) produces z, which is another number. Your job is to create a function that passes different numbers into f and evaluates the output z to find a pattern. The pattern you are looking for is to see if the function f is adding, subtracting, dividing or multiplying x and y. Once you find the pattern your function should return the mathematic symbol (+, -, *, /) that corresponds to function f’s operation. The return value should be a string.  

Example:

Input: function

Internally you will do the following

Test:function(1, 1) == 2.0

Test: function(2, 3) == 5.0

Test: function(4,1) == 5.0

Output: '+'

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

# required method
def functionSolver(function: callable) -> str:
    # using the numbers (7,3), (5,9) and (3,18) as test numbers.
    # let me know if you want to use random numbers, or more aggressive tests using
    # many numbers in loop.
    # case 1, checking if function is addition function
    if function(7, 3) == 10.0 and function(5, 9) == 14.0 and function(3, 18) == 21.0:
        return '+'
    # case 2, checking if function is subtraction function
    elif function(7, 3) == 4.0 and function(5, 9) == -4.0 and function(3, 18) == -15.0:
        return '-'
    # case 3, checking if function is multiplication function
    elif function(7, 3) == 21.0 and function(5, 9) == 45.0 and function(3, 18) == 54.0:
        return '*'
    # since it is given that function can be any one of the four possible functions and
    # we already ruled out +,- and * operations, the remaining is '/', so we return that
    return '/'



#code for testing. ignore if not needed

#defining four methods performing arithmetic operations for testing above method

def add(n1, n2):
    return n1 + n2


def sub(n1, n2):
    return n1 - n2


def mul(n1, n2):
    return n1 * n2


def div(n1, n2):
    return n1 / n2


#calling functionSolver, passing each method, to see if they return expected values
print(functionSolver(add)) # +
print(functionSolver(sub)) # -
print(functionSolver(mul)) # *
print(functionSolver(div)) # /

#output

+
-
*
/

Add a comment
Know the answer?
Add Answer to:
python: def functionSolver(function: callable)->str You will be given a function as a parameter, the function you...
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
  • Create a python add the following functions below to the module. Each section below is a...

    Create a python add the following functions below to the module. Each section below is a function that you must implement, make sure the function's names and parameters match the documentation (Copy-Paste). DO NOT put the functions in an if-name-main block. 1. def productSum(x: int, y: int, z: int) -> int This function should return: The product of x and y, if the product of x and y is less than z. Else it should return the sum of x...

  • Help needed in python ! Classes can not be used ! Thanx Many games have complex...

    Help needed in python ! Classes can not be used ! Thanx Many games have complex physics engines, and one major function of these engines is to figure out f two objects are colliding. Weirdly-shaped objects are often approximated as balls. In this problem, we will figure out if two balls are colliding. You will need to remember how to unpack tuples For calculating collision, we only care about a ball's position in space, as well as its size. We...

  • c++ Note: Do not use using namespace std; Use std:: Use comments for clear understanding TEST...

    c++ Note: Do not use using namespace std; Use std:: Use comments for clear understanding TEST THE PROGRAM. 1) Create a c++ program to run a simple experiment to see how reference parameters work. 2) You need to add a “&” symbol after the parameter type specification to set up a reference parameter: int myfunc (int & x) { x = 11; return -11; } int testdata = 0; int y; y= myfunc (testdata); 3) Now, You can get a...

  • def max_of_two( x, y): ifx>y: return x returny a. Write a Python function called max_of_three( x,...

    def max_of_two( x, y): ifx>y: return x returny a. Write a Python function called max_of_three( x, y, z ) to return the largest numbers of three numbers passing to the function You can use the function max_of_two in your solution. /Your code

  • Multiplexer Example Implement the following Boolean function using a 4x1 Mux;

     Multiplexer Example Implement the following Boolean function using a 4x1 Mux;    F(x,y,z) = Σ (1,2,6,7) Decoder Example Implement the following functions for a full adder using decoder; S(x,y,z) = Σ (1,2,4,7) C(x,y,z) = Σ (3,5,6,7) Implement the following Boolean function; F(x,y,z) = Σ (0,2,3,7): Using; 1. Two 2x4 decoders and logic gates 2. One 4x1 multiplexer Decoder . Draw the truth table for the function to be implemented. . Pick the terms for output. . Derive appropriate logic to combine terms. . Use two 2x4 decoders to make one3x8 decoder. . Pay attention to fact...

  • def average_word_length(string): num_words = 0 if not type(string)==str: return "Not a string" if not "A" or...

    def average_word_length(string): num_words = 0 if not type(string)==str: return "Not a string" if not "A" or not "B" or not "C" or not "D" or not "E" or not "F" or not "G" or not "H" or not "I" or not "J"\ or not "K" or not "L" or not "M" or not "N" or not "O" or not "P" or not "Q" or not "R" or not "S" or not "T"\ or not "U" or not "V" or not...

  • You should implement several functions that the Phone Contacts program will need Each function with take...

    You should implement several functions that the Phone Contacts program will need Each function with take in data through the parameters (i.e., variables names listed in parentheses) and make updates to data structure that holds the contact information, return, or print information for a particular contact. The parameters needed are listed in the table below and each function is described below that 1. Make an empty dictionary and call it 'contacts'. Where in your code would you implement this? Think...

  • C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all...

    C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all the numbers less that the parameter that are divisors of the parameter (i.e. divides it without a remainder) including 1. So printdivisors(6) will print 1,2,3. Note you may use a wrapper function or default parameters. (b) Write a function, sumdixisors, that takes a single integer parameter and returns the sum of all the divisors of the parameter (including 1). So sumdivisors(6) will return 6...

  • Write a Python program which defines a function named "divide_two_numbers" that accepts two numeric parameters and...

    Write a Python program which defines a function named "divide_two_numbers" that accepts two numeric parameters and divides the first parameter by the second. Your divide_two_numbers function must check the second parameter before dividing; if it is zero, raise an ZeroDivisionError exception which includes the string "second parameter was zero!" as a parameter instead of processing the division. Your main function must call your divide_two_numbers function at least twice; one call must use two normal parameters to verify normal operation, the...

  • Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Plea...

    Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Please do not use 'print' or 'input' statements. Context of the assignment is: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is located in the data series. Please see attachments below: We need to consider the following cases: Case 1 - It is possible that the...

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
ADVERTISEMENT