Median
given a list of integers,write a program to print the median.
input
the input will be a single line of containing space-separated integers.
output
the second line of output should contain the median, round off the value to 2 decimal places.
median 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
as the length of the list is an even number, the median will be the average of the two middle numbers in the sorted list.
the middle numbers will be 5 and 4. so the average of 5 and 4 will be median, which is 4.5
so the output should be
Median: 4.5
sample input 1
2 4 5 6 7 8 2 4 5 2 3 8
sample output 1
Median: 4.5
import statistics
a = ( 2, 4, 5, 6, 7, 8, 2, 4, 5, 2, 3, 8)
print(statistics.median(a))
Comments
Leave a comment