Skip to content

Instantly share code, notes, and snippets.

@JefClaes
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save JefClaes/295b611e30f6c79e2fb6 to your computer and use it in GitHub Desktop.

Select an option

Save JefClaes/295b611e30f6c79e2fb6 to your computer and use it in GitHub Desktop.
Averages are not good enough (2)
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