Answer to Question #179156 in Python for phani

Question #179156
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
as the length of the list is an odd number, the median will be the middle number in 

Mean: 5.45
Median: 3
Mode: 2 3

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: 3
Mode: 2 3
1
Expert's answer
2021-04-09T09:31:54-0400
# Main maethod
def main():
    integersList=getIntegersList()
    integersList.sort()
    mean=calculateMean(integersList)
    median=calculateMedian(integersList)
    mode=calculateMode(integersList)
    displayResult(mean,median,mode)
# Gets integers list
def getIntegersList():
    integersList=[]
    userStringNumbers=input("").split(" ")
    for number in userStringNumbers:
        integersList.append(int(number))
    return integersList
# Calculates the average value of all the numbers.
def calculateMean(integersList):
    return sum(integersList) / len(integersList)
# Calculates median - the mid point value in the sorted list.
def calculateMedian(integersList):
    i = len(integersList) // 2
    if len(integersList) % 2:
        return integersList[i]
    else:
        return sum(integersList[i - 1:i + 1]) / 2
# Calculates mode
def calculateMode(integersList):
    counters=integersList[:]
    modes=[]
    for i in range(0,len(integersList)):
        counter=1
        if integersList[i]==-1:
            counters[i]=0
        else:
            for j in range(i+1,len(integersList)):
                if(integersList[i]==integersList[j]):
                    counter+=1
                    integersList[j]=-1
        counters[i]=counter
            
    m=max(counters)
    for i in range(0,len(integersList)):
        if counters[i]==m:
           modes.append(str(integersList[i]))
           
    return ' '.join(modes)
# Displays result
def displayResult(mean,median,mode):
    print("Mean: {:.2f}".format(mean))
    print("Median: {:.2f}".format(median))
    print(f"Mode: {mode}")

main()



Example:

2 4 5 6 7 8 2 4 5 2 3 8
Mean: 4.67
Median: 4.50
Mode: 2

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