Last active
August 29, 2015 14:21
-
-
Save graph1zzlle/1a4b7977002e914168fa to your computer and use it in GitHub Desktop.
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
(defn compute-percentile | |
; This function compute a percentile over a sequence of numbers. | |
[percentile coll] | |
{:pre [(number? percentile) (> percentile 0) (< percentile 1) (not (empty? coll))]} | |
(let [sorted (sort coll) | |
length (count coll) | |
index (* percentile length)] | |
(if (== (int index) index) | |
(let [first (nth sorted (- index 1)) | |
second (nth sorted index)] | |
(/ (+ first second) 2)) | |
(nth sorted (- (Math/ceil index) 1))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment