Skip to content

Instantly share code, notes, and snippets.

@graph1zzlle
Last active August 29, 2015 14:21
Show Gist options
  • Save graph1zzlle/1a4b7977002e914168fa to your computer and use it in GitHub Desktop.
Save graph1zzlle/1a4b7977002e914168fa to your computer and use it in GitHub Desktop.
(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