Mean, Median and Mode
Given a list of integers, write a program to print the mean, median and mode.
Mean - The average value of all the numbers.
Median - The mid point value in the sorted list.
Mode - The most common value in the list. If multiple elements with same frequency are present, print all the values with same frequency in increasing order.Input
The input will be a single line containing space-separated integers
Sample Input 1
2 4 5 6 7 8 2 4 5 2 3 8
Sample Output 1
Mean: 4.67
Median: 4.5
Mode: 2
Sample Input 2
2 6 3 1 8 12 2 9 10 3 4
Sample Output 2
Mean: 5.45
Median: 4
Mode: 2 3
.
def getMean(n):
    l=len(n)
    total=sum(n)
    mn=total/l
    return mn
def getMedian(n):
    l = len(n)
    n.sort()
    if l % 2 == 0:
        med1 = n[l // 2]
        med2 = n[l // 2 - 1]
        med = (med1 + med2) / 2
        return med
    else:
        med = n[l // 2]
        return med
def getMode(n):
    n.sort()
    dictCount = dict()
    for i in n:
        dictCount[i] = dictCount.get(i, 0) + 1
    mode = max(dictCount, key=dictCount.get)
    return mode
if __name__ == '__main__':
    num_list = [2,4,5,6,7,8,2,4,5,2,3,8]
    mean=getMean(num_list)
    median=getMedian(num_list)
    mode=getMode(num_list)
    print("Mean: {:.2f} ".format(mean))
    print("Median: {} ".format(median))
    print("Mode: {} ".format(mode))
Comments