Skip to content

Instantly share code, notes, and snippets.

@sebastian-palma
Last active December 5, 2017 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastian-palma/91afb4be10525db36566a1d325c45f0d to your computer and use it in GitHub Desktop.
Save sebastian-palma/91afb4be10525db36566a1d325c45f0d to your computer and use it in GitHub Desktop.
(ns intrv)
;; max
(apply max '(1 2 3 4 5 5 4 3 2 1 5))
;; min
(apply min '(1 2 3 4 5 5 4 3 2 1 5))
;; mean
(let [arr '(1 2 3 4 5 5 4 3 2 1 5)]
(/ (reduce + arr) (double (count arr))))
;; median
(let [arr '(1 2 3 4 5 5 4 3 2 1 5)
size (count arr)
sorted (sort arr)
a (nth sorted (/ (- size 1) 2))
b (nth sorted (/ size 2))
r (double (/ (+ a b) 2))] r)
;; mode
;; The frequencies function returns a map of values -> frequencies
;; One can treat a map as a sequence of key-value pairs
;; If one sorts the sequence by value - second item in each pair -
;; the last item in the sequence will represent the mode
(first (last (sort-by second (frequencies arr)))))
;; reverse
(reverse '(1 2 3 4 5 5 4 3 2 1 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment