Skip to content

Instantly share code, notes, and snippets.

@yanzay
Created June 13, 2013 13:23
Show Gist options
  • Save yanzay/5773620 to your computer and use it in GitHub Desktop.
Save yanzay/5773620 to your computer and use it in GitHub Desktop.
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