Answer to Question #315332 in Python for sabiha5

Question #315332

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 values with same frequency in increasing order.

input

the input will be a single line of 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

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

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 6 7 8 8



1
Expert's answer
2022-03-23T02:57:58-0400
nums = list(map(int, input('Please input elements of the list: ').split(' ')))

if len(nums) % 2 == 0:
    mean = sum(nums)/len(nums)
    print(f'Mean: {round(mean, 2)}')
else:
    print("Mean", sum(nums) // len(nums))

nums.sort()
if len(nums) % 2 == 0:
    mid_index = len(nums)//2
    median1 = nums[mid_index-1]
    median2 = nums[mid_index]
    print(f"Median values: {median1} and {median2}")
else:
    mid_index = len(nums)//2
    median = nums[mid_index]
    print(f"Median value: {median}")
values_dict = {}
most_com_val_count = 0
for num in nums:
    if num in values_dict.keys():
        values_dict[num] += 1
    else:
        values_dict[num] = 1
print(values_dict)
for key, value in values_dict.items():
    if value >= most_com_val_count:
        most_com_val_count = value
    else:
        continue
index = 0
for key, value in values_dict.items():
    if index == 0:
        print('Mode values with frequency: ', end='')
    if value == most_com_val_count:
        print((key, value), end=', ')
    index += 1

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