Last active
August 29, 2015 14:13
-
-
Save JefClaes/295b611e30f6c79e2fb6 to your computer and use it in GitHub Desktop.
Averages are not good enough (2)
This file contains hidden or 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
| let (|Even|Odd|) x = | |
| if x % 2 = 0 then Even | |
| else Odd | |
| let median input = | |
| let sortedInput = input |> Seq.sort | |
| let length = input |> Seq.length | |
| match length with | |
| | 0 -> raise <| new ArgumentException("Input sequence is empty") | |
| | 1 -> input |> Seq.nth 0 | |
| | _ -> match length with | |
| | Even -> ( let first = sortedInput |> Seq.nth (length / 2 - 1) | |
| let second = sortedInput |> Seq.nth (length / 2) | |
| (first + second) / float 2) | |
| | Odd -> sortedInput |> Seq.nth ((length - 1) / 2) | |
| // Median = 45.0 | |
| // 18 21 41 42 48 50 55 90 | |
| // __ __ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment