Q. Write a program using user defined functions that accepts a List of numbers as an argument and finds its median.
Hint: Use bubble sort to sort the accepted list. If there are odd number of terms, the median is the centre term. If there are even number of terms, add the two middle terms and divide by 2 get median
Answer :-
def median ( lst ) : n = len(lst) for i in range(n): for j in range(0, n-i-1): if lst[j] > lst[j+1] : lst[j], lst[j+1] = lst[j+1], lst[j] if n % 2 == 0 : median = ( lst [ n // 2 -1 ] + lst [ (n // 2) ]) / 2 return median else : median = n // 2 + 1 return median lst = eval(input( "Enter a List :-" )) print( "Median of List is ", median ( lst ) )
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )