I wrote some code that returns the median of an unsorted odd numbered array, but it does not return the median of an even numbered array.
I know that in order to find
I'm going to just jump in with a solution here...
def median(ary) mid = ary.length / 2 sorted = ary.sort ary.length.odd? ? sorted[mid] : 0.5 * (sorted[mid] + sorted[mid - 1]) end
Edit - I've incorporated .odd? as per BroiSatse's suggestion.
.odd?