Last active
December 5, 2017 05:14
-
-
Save sebastian-palma/91afb4be10525db36566a1d325c45f0d 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
(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