Answer to Question #179148 in Python for jayanth

Question #179148

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

See Sample Input/Output for the output format.Explanation


For example, if the given list of integers are

2 4 5 6 7 8 2 4 5 2 3 8

The average of all the numbers is 4.67.


After sorting the array,

2 2 2 3 4 4 5 5 6 7 8 8

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









1
Expert's answer
2021-04-08T12:39:37-0400
inputValues=input("").split(" ")
numbers=[]
for n in inputValues:
    numbers.append(int(n))
    
# Mean - The average value of all the numbers.
mean=sum(numbers) / len(numbers)
# Median - The mid point value in the sorted list.
median=0
n = len(numbers)
index = n // 2
if n % 2:
    median=sorted(numbers)[index]
else:
    median=sum(sorted(numbers)[index - 1:index + 1]) / 2
# Mode - The most common value in the list. If multiple elements with same frequency are present, print all the values with same frequency.Input
numbers.sort()
dictionaryCounter = dict()
for i in numbers:
    dictionaryCounter[i] = dictionaryCounter.get(i, 0) + 1
mode = max(dictionaryCounter, key=dictionaryCounter.get)


# The first line of output should contain the mean, round off the value to 2 decimal places.
print("Mean: {:.2f}".format(mean))
# The second line of output should contain the median, round off the value to 2 decimal places.
print("Median: {:.2f}".format(median))
# The third line of output should contain the mode.
print("Mode: {:.2f}".format(mode))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS