[EMAIL PROTECTED] (Michael Fuhr) wrote: > > Josiah Carlson <[EMAIL PROTECTED]> writes: > > > > "Robert Brewer" <[EMAIL PROTECTED]> wrote: > > > > > > Josiah Carlson wrote: > > > > > > > > median = lambda x: x.sort() or x[len(x)//2] > > > > > > That...is a really sneaky use of null return values. I like. :) > > > > Thank you, I'm just using a paradigm (exploiting lambdas) that I picked > > up while going through various functional programming modules. > > print median([1, 2, 3, 4, 5, 6]) > 4 > > Shouldn't the median be 3.5?
The wikipedia entry (as mentioned by Peter Hanson in another post) is incomplete. There are 3 valid medians for even-lengthed sequences: sorted(seq)[len(seq)//2] sorted(seq)[len(seq)//2-1] sum(sorted(seq)[len(seq)//2-1:len(seq)//2+2])/2.0 All are correct, what is /desired/ is generally application specific. If you want the statistical median, 3.5 is the answer. If you want a computer science median (for quicksort, etc.), 3 or 4 is sufficient. I chose the shortest implementation (using the author's style, without sorted, because I forgot about the new builtin), which gives the greater (or equal) of the two possible computer science median values. - Josiah -- http://mail.python.org/mailman/listinfo/python-list