Created
June 13, 2013 13:23
-
-
Save yanzay/5773620 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 quartile(array, q=3) | |
return array.first if array.length <= 1 | |
sorted = array.sort | |
return sorted.last if q == 4 | |
quartile_position = 0.25 * (q*sorted.length + 4 - q) | |
quartile_int = quartile_position.to_i | |
lower = sorted[quartile_int - 1] | |
upper = sorted[quartile_int] | |
lower + (upper - lower) * (quartile_position - quartile_int) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment