average median Algorithm

The average median algorithm is a statistical method used to determine the central value of a given dataset. In simple terms, it is the value that separates the dataset into two equal halves, with 50% of the data values lying above the median and the remaining 50% lying below it. The median is considered a robust measure of central tendency, as it doesn't get influenced by extreme values (outliers) unlike the mean. It is particularly useful when dealing with skewed datasets, as it provides a more accurate representation of the data's center. To compute the median, the dataset must first be sorted in ascending order. If the dataset has an odd number of values, the median is simply the middle value. For instance, in the dataset {1, 2, 3}, the median is 2, as it is the value that separates the dataset into two equal halves. However, if the dataset has an even number of values, the median is calculated as the average of the two central values. For example, in the dataset {1, 2, 3, 4}, the median is (2+3)/2 = 2.5. The average median algorithm can be applied to a wide range of fields, including finance, economics, and social science, to analyze data and draw meaningful conclusions.
def median(nums):
    """
    Find median of a list of numbers.

    >>> median([0])
    0
    >>> median([4,1,3,2])
    2.5

    Args:
        nums: List of nums

    Returns:
        Median.
    """
    sorted_list = sorted(nums)
    med = None
    if len(sorted_list) % 2 == 0:
        mid_index_1 = len(sorted_list) // 2
        mid_index_2 = (len(sorted_list) // 2) - 1
        med = (sorted_list[mid_index_1] + sorted_list[mid_index_2]) / float(2)
    else:
        mid_index = (len(sorted_list) - 1) // 2
        med = sorted_list[mid_index]
    return med


def main():
    print("Odd number of numbers:")
    print(median([2, 4, 6, 8, 20, 50, 70]))
    print("Even number of numbers:")
    print(median([2, 4, 6, 8, 20, 50]))


if __name__ == "__main__":
    main()

LANGUAGE:

DARK MODE: