Question: Suppose you are given a Python list named x that contains at least two or more floating point numbers. You need to create a new

 Suppose you are given a Python list named x that contains

Suppose you are given a Python list named x that contains at least two or more floating point numbers. You need to create a new list named y that contains the average of adjacent pairs of numbers in x. Example: Suppose x = [1, 5, -3, 2] y will be [3.0, 1.0, -0.5] In this case, the first element of y is calculated from the average of 1 and 5, i.e., (1+5)/2 = 3. The second element ofy is given by (5 + (-3))/2 - 1. The last element in y is the average of -3 and 2. Example: Suppose x = (12, -42, 81, 4, 22]. Then y will be [-15.0, 19.5, 42.5, 13.0). NOTE: If you prefer, you may use NumPy 1-D arrays instead of Python lists, if you think that makes it easier for you to solve this problem. It is your choice. For example, x = np.array([1,5,-3,2]) for NumPy. a) Write either a standard for loop or a while loop (just pick one) to create the result y. Your code must be capable of handling an x of arbitrary length Put your loop code in the box below. It will be tested by the grader, so it should be executable. b) For the box below, show a list comprehension to create y in a single line of code. Note that the generated y will be a list, but it does not matter if x is a Python list or a NumPy array; the syntax is the same

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!