writing a median function

i am trying to write a median function, currently i have one that takes a list and sorts it and then gives the median. but i am trying to rewrite it so that if the list has an even number of elements it doesnt print the median but rather the 2 numbers from the list that the median is between. heres my code

def getMedian(A):
    A=sorted(A)
    n=len(A)
    m=n-1
    return (A[int(n/2)]+A[int(m/2)])/2

def main():
    myList=[3,5,9,3,11]
    print(getMedian(myList))
    myList=[3,9,11,5]
    print(getMedian(myList))
main()

currently it outputs 5 and then 7 but im trying to get it to output 5 and then 5,9 any advice?

import statistics

statistics.median(data)

MEDIAN Function, For example, =MEDIAN(1,2,3,4,5) returns 3. Purpose. Get the median of a group of numbers. Return value. A number representing� I have been tasked to write my own median function in R, without using the built-in median function. If the numbers are odd; calculate the two middle values, as is usual concerning the median value.

You could check if the list has an odd number of elements and then change the return value(s) accordingly:

def getMedian(A):
    A = sorted(A)
    n = len(A)
    m = n-1
    if n%2 == 1:
        return (A[int(n/2)]+A[int(m/2)])/2
    else:
        return A[int(m/2)], A[int(n/2)]

def main():
    myList = [3,5,9,3,11]
    print(getMedian(myList))
    myList = [3,9,11,5]
    print(getMedian(myList))

main()

Output:

>>> 
5
(5, 9)

How to use the Excel MEDIAN function, The Excel MEDIAN Function - Returns the Statistical Median of a List of Supplied Numbers - Function Description & Examples. The MEDIAN function finds the middle value for a project. The IF function allows us to choose which project we want a tender for by setting a condition using the project names. The array formula lets the IF function test for multiple conditions in a single cell. When the condition is met, the array formula determines what data (project tenders

Here is an example of doing it.

def getMedian(A):
    A.sort()
    mid = len(A) // 2
    return [[A[~mid], A[mid]], (A[mid] + A[~mid]) / 2][len(A) % 2]


def main():
    myList = [3, 5, 9, 3, 11]
    print(getMedian(myList))
    myList = [3, 9, 11, 5]
    print(getMedian(myList))


main()

output

5.0
[5, 9]

Excel Median Function Examples, If there is an even number of numbers in the set, then MEDIAN calculates the average of the two numbers in the middle. See the second formula in the example. where m is a spatial median, that is, a minimizer of the function ↦ ⁡ (‖ − ‖). The spatial median is unique when the data-set's dimension is two or more. An alternative proof uses the one-sided Chebyshev inequality; it appears in an inequality on location and scale parameters.

MEDIAN function - Office Support, In statistics and probability theory, a median is a value separating the higher half from the lower half of a data sample, a population or a probability distribution. For a data set, it may be thought of as "the middle" value. For example, the basic advantage of the median in describing data compared Hence the density function of the median is a symmetric beta distribution� Python 3.4 has statistics.median function. The list can be of any size, and the numbers are not guaranteed to be in any particular order. If you are looking for a function that calculates the median() in Python 3, then the statistics.median() function is the solution. The median() function returns the median (middle value) of numeric data.

Median, For example, median of {1, 2, 2, 5, 100) is 2 and mean is 22. If user add a constant to Function for calculating median. double findMedian( int� The MEDIAN function returns the median (middle number) in a group of supplied numbers. For example, =MEDIAN(1,2,3,4,5) returns 3.

Median, Example # 1 -Median function on even numbers. Let's understand, below mentioned table and data as a series of even numbers, now I want to know middle no of� Ideally each function does one thing well. Often this means lots of short functions. Short functions are extremely useful. Even if the code in the function body is more complex, ideally it still does one thing well. It does one thing well. The reason for writing a function is not to reuse its code, but to name the operation it performs.

Comments
  • You told it to return the average of the 2 middle numbers at the end of your function. I suggest checking if n%2 == 0 or n%2 ==1 and then have an if-then statement to determine what is returned
  • And what to return for [5,10,10,15]?