Last active
February 21, 2018 21:41
-
-
Save L8D/6187830 to your computer and use it in GitHub Desktop.
Median of array in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def median_of a | |
return nil if a.empty? | |
sorted = a.sort | |
len = sorted.length | |
(sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment