Description:

The mean (or average) is the most popular measure of central tendency; however it does not behave very well when the data is skewed (i.e. wages distribution). In such cases, it's better to use the median.

Your task for this kata is to find the median of an array consisting of n elements.

You can assume that all inputs are arrays of numbers in integer format. For the empty array your code should return `NaN` (false in JavaScript/`NULL` in PHP/`nil` in Ruby).

Examples:

Input `[1, 2, 3, 4]` --> Median `2.5`

Input `[3, 4, 1, 2, 5]` --> Median `3`

Difficulty: introductory

Starter code:

def median(arr):
	

Solutions:
from statistics import median

def median(arr):
    arr.sort()
    return (arr[(len(arr) - 1)//2] + arr[(len(arr))//2])/2

from numpy import median

import numpy as np
def median(arr):
    return np.median(arr) if len(arr)>0 else None


from random import choice

def median(lst):
    return quickselect(lst, len(lst) // 2, len(lst) % 2 == 0)

def quickselect(lst, nth, even):
    # O(n) but what a pain to get right.
    pivot = choice(lst)
    smaller = [n for n in lst if n < pivot]
    n_equal = sum(n == pivot for n in lst)
    bigger = [n for n in lst if n > pivot]
    if even:
        if len(smaller) > nth:
            return quickselect(smaller, nth, True)
        elif len(smaller) == nth:
            return (pivot + quickselect(smaller, nth - 1, False)) / 2
        elif len(smaller) + n_equal > nth:
            return pivot
        elif len(smaller) + n_equal == nth:
            return (pivot + quickselect(bigger, nth - len(smaller) - n_equal, False)) / 2
        else:
            return quickselect(bigger, nth - len(smaller) - n_equal, True)
    else:
        if len(smaller) > nth:
            return quickselect(smaller, nth, False)
        elif len(smaller) + n_equal > nth:
            return pivot
        else:
            return quickselect(bigger, nth - len(smaller) - n_equal, False)

from numpy import median as median_

def median(arr):
    return median_(arr)

import numpy
def median(A):
    return None if len(A)<1 else numpy.median(A)

import numpy as np
def median(arr):
    return np.median(arr)