Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.1 KiB · at b0c0c21

Write two functions over an array of numbers.

summarize(values) returns { count, mean, median, min, max }:

  • count is the number of values and mean is their sum divided by the count.
  • median is the middle value when the values are put in ascending numeric order. For an even count it is the mean of the two middle values.
  • min and max are the smallest and largest values.

percentile(values, p) returns the nearest-rank percentile: sort the values in ascending numeric order, let rank = ceil(p / 100 * count), and return the value at 1-based position rank. When p is 0 the rank is 1 (the smallest value). p must be a finite number from 0 to 100 inclusive.

Errors, checked in this order by both functions:

  • { kind: "empty" } when values has no elements.
  • { kind: "not_finite", index } when a value is NaN, Infinity or -Infinity. index is the position of the first such value in the input.
  • { kind: "invalid_percentile" } (percentile only) when p is not a finite number in [0, 100].

Neither function may modify the array it is given.