Question: Write a function called median which takes a list of numbers x and returns the median value (see Wikipedia:Median). If the length of the

Write a function called median which takes a list of numbers x

Write a function called median which takes a list of numbers x and returns the median value (see Wikipedia:Median). If the length of the list x is odd then the median is the middle value and if the length is even then the median is the average of the two middle values. For example, the median of [1,3,-1,6,2] is 2 and the median of [1,3,2,6,-1,0] is 1.5. Use the built-in function sorted to sort the list of values. Use the modulo operator % to determine if an integer is odd or even. This is an exercise in pure Python. Do not use any Python packages such as math or numpy or scipy for this exercise. def median(x): |]: "Check median returns the correct datatype. (1 mark)" assert isinstance (median ([1,2,3,4]), float) print("Problem 1 Test 1: Success!") ]: "Check median returns the correct values. This cell contains hidden tests. (2 marks)" assert abs (median ([1.0, 2.0,3.0]) 2.0) < 1e-14 print("Problem 1 Test 2: Success!") - ]: "Check median returns the correct values. This cell contains hidden tests. (2 marks)" assert abs(median([1.0,2.0,3.0, 4.0]) 2.5) < 1e-14 print("Problem 1 Test 3: Success!")

Step by Step Solution

3.50 Rating (150 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Here is the implementation of the median function def medianx xsort length lenx if length 2 ... View full answer

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 Programming Questions!