Many big data or scientific programs use boxplots to show distributions of data.   In addition, sometimes saving large arrays for boxplots can be impractical and use extreme amounts of RAM.   It can be useful to save large arrays as arrays with five numbers to save memory.

For example, the   R   programming language implements Tukey's five-number summary as the fivenum function.



Task

Given an array of numbers, compute the five-number summary.



Note
 
While these five numbers can be used to draw a boxplot,   statistical packages will typically need extra data.

Moreover, while there is a consensus about the "box" of the boxplot,   there are variations among statistical packages for the whiskers.




 
program Fivenum;
 
{$APPTYPE CONSOLE}
 
uses
  System.SysUtils,
  System.Generics.Collections;
 
function Median(x: TArray<Double>; start, endInclusive: Integer): Double;
var
  size, m: Integer;
begin
  size := endInclusive - start + 1;
  if (size <= 0) then
    raise EArgumentException.Create('Array slice cannot be empty');
  m := start + size div 2;
  if (odd(size)) then
    Result := x[m]
  else
    Result := (x[m - 1] + x[m]) / 2;
end;
 
function FiveNumber(x: TArray<Double>): TArray<Double>;
var
  m, lowerEnd: Integer;
begin
  SetLength(result, 5);
  TArray.Sort<double>(x);
  result[0] := x[0];
  result[2] := median(x, 0, length(x) - 1);
  result[4] := x[length(x) - 1];
  m := length(x) div 2;
  if odd(length(x)) then
    lowerEnd := m
  else
    lowerEnd := m - 1;
  result[1] := median(x, 0, lowerEnd);
  result[3] := median(x, m, length(x) - 1);
end;
 
function ArrayToString(x: TArray<double>): string;
var
  i: Integer;
begin
  Result := '[';
  for i := 0 to High(x) do
  begin
    if i > 0 then
      Result := Result + ',';
    Result := Result + format('%.4f', [x[i]]);
  end;
  Result := Result + ']';
end;
 
var
  xl: array of TArray<double> = [[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0,
    39.0, 47.0, 43.0], [36.0, 40.0, 7.0, 39.0, 41.0, 15.0], [0.14082834,
    0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726,
    1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -
    1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, 0.75775634,
    0.32566578]];
  x: TArray<double>;
 
begin
  for x in xl do
    writeln(ArrayToString(FiveNumber(x)), #10);
 
  readln;
end.