Q. Write a program to read a list of n integers and find their median.
Note: The median value of a list of values is the middle one when they are arranged in order. If there are two middle values then take their average.
Hint: You can use an built-in function to sort the list
Answer :-
lst = eval(input("Enter a list :-")) lst.sort() length = len(lst) if length % 2 == 0 : med = ( lst [ length // 2 - 1 ] + lst [ (length // 2) ]) / 2 else : med = lst [ (length // 2) -1 ] print("Median :-",med)
its true for even no of integer but for odd its false because you don't have to subtract one from length its automatically taking gif of number at last the statement is :
ReplyDeleteelse:lst[(length//2)]
No, you are wrong, because I have used median formula that is :-
DeleteFor even :- ((n-1)+(n))/2
) missing in line1 right
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )