Mean, Median and Mode

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. Output

The first line of output should contain the mean, round off the value to 2 decimal places.

The second line of output should contain the median, round off the value to 2 decimal places.

The third line of output should contain the mode.

Mean should always be a float value.

Median should be a float value when there are even number of elements, otherwise should be an integer value.

 

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

 

Answers:

def get_median(arg_list):

arg_list.sort()

size = len(arg_list)

if size %2 == 1:

return arg_list[size//2]

else:

return (arg_list[size//2] + arg_list[size//2 -1])/2

 

def get_mean(arg_list):

sum_list = 0

for i in arg_list:

sum_list +=i

return round(sum_list/len(arg_list), 2)

 

def get_mode(arg_list):

mode_list = sorted(set(user_list), key=user_list.count,reverse=True)

mode_max = user_list.count(mode_list[0])

mode_res = []

for i in mode_list:

if user_list.count(i) < mode_max:

break

mode_res.append(i)

return sorted(mode_res)

 

user_str = input(‘Enter a string of integers separated by a space: ‘)

user_list = [int(i) for i in user_str.split()]

print(‘Mean :’, get_mean(user_list))

print(‘Median :’, get_median(user_list))

# Displaying mode list values separated by a space

print(‘Mode :’, *get_mode(user_list))

 

Looking for Solutions?

Get the best assignment assistance from experts.

Order-Your-Assignment

Content Page Removal Request:

If you are the original writer or copyright-authorized owner of this article/post and no longer wish to have, your work published www.assignmenttask.com, then please email us with page link. help@Assignmenttask.Com