Question: USE PYTHON3; DO NOT IMPORT PACKAGES A piecewise function is a function that applies different sub-functions in different intervals of domain. For example, For convenience,

USE PYTHON3; DO NOT IMPORT PACKAGES

A piecewise function is a function that applies different sub-functions in different intervals of domain. For example,

For convenience, we will call a combination of an interval and its corresponding sub-function a case. If you need to implement many piecewise functions in Python, you may find that each piecewise function has the same structure (collection of cases), the differences are just the specific intervals and sub-functions. Thus, a creator function that takes all cases and returns a piecewise function that stores these cases and applies the correct case to any numeric input can save your effort copy-pasting the same logic multiple times.

The creator function (create_piecewise_function) takes all cases as arguments. Each case is a (lower_bound, upper_bound, subfunction) tuple, which indicates that in the created piecewise function, if

lower_bound (inclusive) <= input number < upper_bound (exclusive),

the piecewise function should apply the corresponding sub-function to the number and return the result.

The piecewise function assumes the domain is (-inf, inf). However, if the provided cases failed to cover all the domain, this function applies an identity function (i.e. return the number itself) if the input number is within an uncovered interval.

Note:

  1. You may assume that lower bound and upper bound are numeric values, and the subfunction is a function to process numeric values. You may also assume that all intervals in the provided cases will not overlap; and in each interval, lower bound is smaller than upper bound.

  2. In Python, float(inf) is positive infinity and -float(inf) is negative infinity. They can appear in the cases for the creator function, but will not be passed to the created piecewise function. By definition, all numeric values are greater than the negative infinity, and all numeric values are smaller than the positive infinity, so you dont need to add extra conditions to handle the infinities.

  3. Do not overthink the problem. Our solution is 7 lines.

Example (provided doctest):

The statement

f_x = create_piecewise_function(

(-float('inf'), -1, lambda num: -3 - num),

(1, float('inf'), lambda num: num * 3 + 1)

)

creates a piecewise function f(x) and assigns it to the variable f_x. The function is defined as:

Note:

  1. (-inf <= x) is not mathematically rigorous since negative infinity should be exclusive, but it will not affect the result in this question.

  2. f(x) = x for the intervals that are not defined in any cases arguments.

Therefore,

f_x(-2) -> f(-2) = -3 - (-2) = -1

f_x(-1) -> f(-1) = -1

f_x(1) -> f(1) = 3 * 1 + 1 = 4

def create_piecewise_function(*cases):

"""

>>> f_x = create_piecewise_function( \

(-float('inf'), -1, lambda num: -3 - num), \

(1, float('inf'), lambda num: num * 3 + 1))

>>> f_x(-2)

-1

>>> f_x(-1)

-1

>>> f_x(1)

4

"""

# YOUR CODE GOES HERE #

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!