Fivenum: Difference between revisions

33,494 bytes added ,  1 month ago
m
→‎{{header|Ada}}: Fixed sub section headers
m (→‎{{header|Ada}}: Fixed sub section headers)
 
(29 intermediate revisions by 19 users not shown)
Line 11:
 
;Note:
While these five numbers can be used to draw a [[wp:Box plot|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.
 
Moreover, while there is a consensus about the "box" of the boxplot,   there are variations among statistical packages for the whiskers.
<br><br>
 
Line 17 ⟶ 19:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F fivenum(array)
V n = array.len
V x = sorted(array)
Line 33 ⟶ 35:
1.04312009, -0.10305385, 0.75775634, 0.32566578]
 
print(fivenum(x))</langsyntaxhighlight>
 
{{out}}
<pre>
[-1.9506, -0.676741, 0.233247, 0.746071, 1.73132]
</pre>
 
=={{header|Ada}}==
===Direct C Translation===
{{trans|C}}
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Generic_Array_Sort;
 
procedure Main is
package Real_Io is new Float_IO (Long_Float);
use Real_Io;
 
type Data_Array is array (Natural range <>) of Long_Float;
subtype Five_Num_Type is Data_Array (0 .. 4);
 
procedure Sort is new Ada.Containers.Generic_Array_Sort
(Index_Type => Natural, Element_Type => Long_Float,
Array_Type => Data_Array);
 
function Median (X : Data_Array) return Long_Float with
Pre => X'Length > 0;
 
function Median (X : Data_Array) return Long_Float is
M : constant Natural := X'First + X'Last / 2;
begin
if X'Length rem 2 = 1 then
return X (M);
else
return (X (M - 1) + X (M)) / 2.0;
end if;
end Median;
 
procedure fivenum (X : Data_Array; Result : out Five_Num_Type) is
Temp : Data_Array := X;
m : Natural := X'Length / 2;
Lower_end : Natural := (if X'Length rem 2 = 0 then m - 1 else m);
begin
Sort (Temp);
Result (0) := Temp (Temp'First);
Result (2) := Median (Temp);
Result (4) := Temp (Temp'Last);
Result (1) := Median (Temp (1 .. Lower_end));
Result (3) := Median (Temp (m .. Temp'Last));
end fivenum;
 
procedure print (Result : Five_Num_Type; Aft : Natural) is
begin
Put ("[");
for I in Result'Range loop
Put (Item => Result (I), Fore => 1, Aft => Aft, Exp => 0);
if I < Result'Last then
Put (", ");
else
Put_Line ("]");
end if;
end loop;
New_Line;
end print;
 
X1 : Data_Array :=
(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0);
X2 : Data_Array := (36.0, 40.0, 7.0, 39.0, 41.0, 15.0);
X3 : Data_Array :=
(0.140_828_34, 0.097_487_90, 1.731_315_07, 0.876_360_09, -1.950_595_94,
0.734_385_55, -0.030_357_26, 1.466_759_70, -0.746_213_49, -0.725_887_72,
0.639_051_60, 0.615_015_27, -0.989_837_80, -1.004_478_74, -0.627_594_69,
0.662_061_63, 1.043_120_09, -0.103_053_85, 0.757_756_34, 0.325_665_78);
Result : Five_Num_Type;
begin
fivenum (X1, Result);
print (Result, 1);
fivenum (X2, Result);
print (Result, 1);
fivenum (X3, Result);
print (Result, 9);
end Main;
</syntaxhighlight>
{{output}}
<pre>
[6.0, 36.0, 40.0, 48.0, 49.0]
 
[7.0, 25.5, 25.5, 41.0, 41.0]
 
[-1.950595940, -0.627594690, 0.119158120, 1.599037385, 1.731315070]
</pre>
===Using Ada Enumeration===
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Generic_Array_Sort;
 
procedure Main is
package Real_Io is new Float_IO (Long_Float);
use Real_Io;
 
type Data_Array is array (Natural range <>) of Long_Float;
 
type fivenum_index is (minimum, lower_hinge, median, upper_hinge, maximum);
type Five_Num_Type is array (fivenum_index) of Long_Float;
 
procedure Sort is new Ada.Containers.Generic_Array_Sort
(Index_Type => Natural, Element_Type => Long_Float,
Array_Type => Data_Array);
 
function Median (X : Data_Array) return Long_Float with
Pre => X'Length > 0;
 
function Median (X : Data_Array) return Long_Float is
M : constant Natural := X'First + X'Last / 2;
begin
if X'Length rem 2 = 1 then
return X (M);
else
return (X (M - 1) + X (M)) / 2.0;
end if;
end Median;
 
procedure fivenum (X : Data_Array; Result : out Five_Num_Type) is
Temp : Data_Array := X;
m : Natural := X'Length / 2;
Lower_end : Natural := (if X'Length rem 2 = 0 then m - 1 else m);
begin
Sort (Temp);
Result (minimum) := Temp (Temp'First);
Result (lower_hinge) := Median (Temp (0 .. Lower_end));
Result (median) := Median (Temp);
Result (upper_hinge) := Median (Temp (m .. Temp'Last));
Result (maximum) := Temp (Temp'Last);
end fivenum;
 
procedure print (Result : Five_Num_Type) is
package five_io is new Enumeration_IO (fivenum_index);
use five_io;
begin
for I in fivenum_index loop
Put(" ");
Put (Item => I, Width => 12);
end loop;
New_Line;
Put ("[");
for I in Result'Range loop
Put (Item => Result (I), Fore => 3, Aft => 9, Exp => 0);
if I < Result'Last then
Put (", ");
else
Put_Line ("]");
end if;
end loop;
New_Line;
end print;
 
X1 : Data_Array :=
(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0);
X2 : Data_Array := (36.0, 40.0, 7.0, 39.0, 41.0, 15.0);
X3 : Data_Array :=
(0.140_828_34, 0.097_487_90, 1.731_315_07, 0.876_360_09, -1.950_595_94,
0.734_385_55, -0.030_357_26, 1.466_759_70, -0.746_213_49, -0.725_887_72,
0.639_051_60, 0.615_015_27, -0.989_837_80, -1.004_478_74, -0.627_594_69,
0.662_061_63, 1.043_120_09, -0.103_053_85, 0.757_756_34, 0.325_665_78);
Result : Five_Num_Type;
begin
fivenum (X1, Result);
print (Result);
fivenum (X2, Result);
print (Result);
fivenum (X3, Result);
print (Result);
end Main;
</syntaxhighlight>
{{output}}
<pre>
MINIMUM LOWER_HINGE MEDIAN UPPER_HINGE MAXIMUM
[ 6.000000000, 11.000000000, 40.000000000, 48.000000000, 49.000000000]
 
MINIMUM LOWER_HINGE MEDIAN UPPER_HINGE MAXIMUM
[ 7.000000000, 15.000000000, 25.500000000, 41.000000000, 41.000000000]
 
MINIMUM LOWER_HINGE MEDIAN UPPER_HINGE MAXIMUM
[ -1.950595940, -0.736050605, 0.119158120, 1.599037385, 1.731315070]
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|11l}}
Includes additional test cases and adjustment to n4 for odd length array as in a number of other samples.
{{libheader|ALGOL 68-rows}}
<syntaxhighlight lang="algol68">BEGIN # construct an R-style fivenum function #
PR read "rows.incl.a68" PR
 
PROC fivenum = ( []REAL array )[]REAL:
BEGIN
INT n = ( UPB array + 1 ) - LWB array;
[ 1 : n ]REAL x := array[ AT 1 ];
QUICKSORT x FROMELEMENT LWB x TOELEMENT UPB x;
REAL n4 = ( ( ( n + IF ODD n THEN 3 ELSE 2 FI ) / 2 ) / 2 ) ;
[]REAL d = ( 1, n4, ( n + 1 ) / 2, n + 1 - n4, n );
[ 1 : 5 ]REAL sum_array;
FOR e TO 5 DO
INT fl = ENTIER d[ e ];
INT ce = IF fl < d[ e ] THEN 1 + fl ELSE fl FI;
sum_array[ e ] := 0.5 * ( x[ fl ] + x[ ce ] )
OD;
sum_array
END # five num # ;
 
SHOW fivenum( ( 36, 40, 7, 39, 41, 15 ) );
print( ( newline ) );
SHOW fivenum( ( 15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43 ) );
print( ( newline ) );
SHOW fivenum( ( 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
)
)
END</syntaxhighlight>
{{out}}
<pre>
7.00000000 15.00000000 37.50000000 40.00000000 41.00000000
6.00000000 25.50000000 40.00000000 42.50000000 49.00000000
-1.95059594 -0.67674120 0.23324706 0.74607095 1.73131507
</pre>
 
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- Mac OS X 10.10. (Yosemite) or later.
use framework "Foundation"
 
Line 76 ⟶ 297:
set z to {0.14082834, 0.0974879, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.4667597, -0.74621349, -0.72588772, ¬
0.6390516, 0.61501527, -0.9898378, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
return {fivenum(x, 1, count x), fivenum(y, 1, count y), fivenum(z, 1, count z)}</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{{6, 25.5, 40, 42.5, 49}, {7, 15, 37.5, 40, 41}, {-1.95059594, -0.676741205, 0.23324706, 0.746070945, 1.73131507}}</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">fivenum: function [lst][
lst: sort lst
m: (size lst)/2
lowerEnd: (odd? size lst)? -> m -> m-1
 
return @[
first lst
median slice lst 0 lowerEnd
median slice lst 0 dec size lst
median slice lst m dec size lst
last lst
]
]
 
lists: @[
@[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,0-1.95059594,
0.73438555,0-0.03035726, 1.46675970,0-0.74621349,0-0.72588772,
0.63905160, 0.61501527,0-0.98983780,0-1.00447874,0-0.62759469,
0.66206163, 1.04312009,0-0.10305385, 0.75775634, 0.32566578]
]
 
loop lists 'l [
print [l "->"]
print [fivenum l]
print ""
]</syntaxhighlight>
 
{{out}}
 
<pre>[15.0 6.0 42.0 41.0 7.0 36.0 49.0 40.0 39.0 47.0 43.0] ->
[6.0 25.5 40.0 42.5 49.0]
 
[36.0 40.0 7.0 39.0 41.0 15.0] ->
[7.0 15.0 37.5 40.0 41.0]
 
[0.14082834 0.0974879 1.73131507 0.87636009 -1.95059594 0.7343855500000001 -0.03035726 1.4667597 -0.74621349 -0.72588772 0.6390516000000001 0.61501527 -0.9898378 -1.00447874 -0.62759469 0.66206163 1.04312009 -0.10305385 0.75775634 0.32566578] ->
[-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507]</pre>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
{{trans|Phix}}
<syntaxhighlight lang="vb">#define floor(x) ((x*2.0-0.5) Shr 1)
 
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
 
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
 
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
 
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
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}
fivenum(x1())
fivenum(x2())
fivenum(x3())
 
Sleep</syntaxhighlight>
{{out}}
<pre>[6, 25.5, 40, 43, 49]
[7, 15, 37.5, 40, 41]
[-1.950596,-0.950596, 0.04940403, 1.049404, 0.3256658]</pre>
 
==={{header|VBA}}===
Uses [[Sorting_algorithms/Quicksort#VBA|Quicksort]].
{{trans|Phix}}<syntaxhighlight lang="vb">Option Base 1
Private Function median(tbl As Variant, lo As Integer, hi As Integer)
Dim l As Integer: l = hi - lo + 1
Dim m As Integer: m = lo + WorksheetFunction.Floor_Precise(l / 2)
If l Mod 2 = 1 Then
median = tbl(m)
Else
median = (tbl(m - 1) + tbl(m)) / 2
End if
End Function
Private Function fivenum(tbl As Variant) As Variant
Sort tbl, UBound(tbl)
Dim l As Integer: l = UBound(tbl)
Dim m As Integer: m = WorksheetFunction.Floor_Precise(l / 2) + l Mod 2
Dim r(5) As String
r(1) = CStr(tbl(1))
r(2) = CStr(median(tbl, 1, m))
r(3) = CStr(median(tbl, 1, l))
r(4) = CStr(median(tbl, m + 1, l))
r(5) = CStr(tbl(l))
fivenum = r
End Function
Public Sub main()
Dim x1 As Variant, x2 As Variant, x3 As Variant
x1 = [{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}]
x2 = [{36, 40, 7, 39, 41, 15}]
x3 = [{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}]
Debug.Print Join(fivenum(x1), " | ")
Debug.Print Join(fivenum(x2), " | ")
Debug.Print Join(fivenum(x3), " | ")
End Sub</syntaxhighlight>{{out}}
<pre>6 | 25,5 | 40 | 43 | 49
7 | 15 | 37,5 | 40 | 41
-1,95059594 | -0,676741205 | 0,23324706 | 0,746070945 | 1,73131507</pre>
 
==={{header|Visual Basic .NET}}===
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
Imports System.Text
 
Module Module1
 
<Extension()>
Function AsString(Of T)(c As ICollection(Of T), Optional format As String = "{0}") As String
Dim sb As New StringBuilder("[")
Dim it = c.GetEnumerator()
If it.MoveNext() Then
sb.AppendFormat(format, it.Current)
End If
While it.MoveNext()
sb.Append(", ")
sb.AppendFormat(format, it.Current)
End While
Return sb.Append("]").ToString()
End Function
 
Function Median(x As Double(), start As Integer, endInclusive As Integer) As Double
Dim size = endInclusive - start + 1
If size <= 0 Then
Throw New ArgumentException("Array slice cannot be empty")
End If
Dim m = start + size \ 2
Return If(size Mod 2 = 1, x(m), (x(m - 1) + x(m)) / 2.0)
End Function
 
Function Fivenum(x As Double()) As Double()
For Each d In x
If Double.IsNaN(d) Then
Throw New ArgumentException("Unable to deal with arrays containing NaN")
End If
Next
 
Array.Sort(x)
Dim result(4) As Double
 
result(0) = x.First()
result(2) = Median(x, 0, x.Length - 1)
result(4) = x.Last()
 
Dim m = x.Length \ 2
Dim lowerEnd = If(x.Length Mod 2 = 1, m, m - 1)
 
result(1) = Median(x, 0, lowerEnd)
result(3) = Median(x, m, x.Length - 1)
 
Return result
End Function
 
Sub Main()
Dim x1 = {
New Double() {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
New Double() {36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
New Double() {
0.14082834, 0.0974879, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.4667597, -0.74621349, -0.72588772, 0.6390516, 0.61501527,
-0.9898378, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
For Each x In x1
Dim result = Fivenum(x)
Console.WriteLine(result.AsString("{0:F8}"))
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000]
[7.00000000, 15.00000000, 37.50000000, 40.00000000, 41.00000000]
[-1.95059594, -0.67674121, 0.23324706, 0.74607095, 1.73131507]</pre>
 
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 154 ⟶ 598:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 167 ⟶ 611:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 230 ⟶ 674:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000]
Line 238 ⟶ 682:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <ostream>
Line 316 ⟶ 760:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>(6, 25.5, 40, 43, 49)
Line 324 ⟶ 768:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.exception;
import std.math;
Line 373 ⟶ 817:
writeln(fivenum(x));
}
}</langsyntaxhighlight>
{{out}}
<pre>[6, 25.5, 40, 43, 49]
Line 382 ⟶ 826:
{{libheader| System.Generics.Collections}}
{{Trans|Java}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Fivenum;
 
Line 451 ⟶ 895:
 
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 460 ⟶ 904:
[-1,9506,-0,6767,0,2332,0,7461,1,7313]
</pre>
 
=={{header|F#|F sharp}}==
=={{header|EasyLang}}==
{{trans|Ring}}
<syntaxhighlight>
func median t[] low high .
l = high - low + 1
m = low + l div 2
if l mod 2 = 1
return t[m]
.
return (t[m - 1] + t[m]) / 2
.
proc sort . d[] .
for i = 1 to len d[] - 1
for j = i + 1 to len d[]
if d[j] < d[i]
swap d[j] d[i]
.
.
.
.
func[] fivenum t[] .
sort t[]
l = len t[]
m = l div 2 + l mod 2
r1 = t[1]
r2 = median t[] 1 m
r3 = median t[] 1 l
r4 = median t[] (m + 1) l
r5 = t[l]
return [ r1 r2 r3 r4 r5 ]
.
print fivenum [ 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 ]
print fivenum [ 36 40 7 39 41 15 ]
</syntaxhighlight>
{{out}}
<pre>
[ -1.95 -0.68 0.23 0.75 1.73 ]
[ 7 15 37.50 40 41 ]
</pre>
 
=={{header|EMal}}==
{{trans|Java}}
<syntaxhighlight lang="emal">
type Fivenum
int ILLEGAL_ARGUMENT = 0
fun median = real by List x, int start, int endInclusive
int size = endInclusive - start + 1
if size <= 0 do Event.error(ILLEGAL_ARGUMENT, "Array slice cannot be empty").raise() end
int m = start + size / 2
return when(size % 2 == 1, x[m], (x[m - 1] + x[m]) / 2.0)
end
fun fivenum = List by List x
List result = real[].with(5)
x.order()
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = x.length / 2
int lowerEnd = when(x.length % 2 == 1, m, m - 1)
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
end
List lists = List[
real[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
real[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
real[ 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] ]
for each List list in lists
writeLine(text!fivenum(list))
end
</syntaxhighlight>
{{out}}
<pre>
[6.0,25.5,40.0,42.5,49.0]
[7.0,15.0,37.5,40.0,41.0]
[-1.95059594,-0.676741205,0.23324706,0.746070945,1.73131507]
</pre>
 
=={{header|F Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
 
// Take from https://stackoverflow.com/a/1175123
Line 503 ⟶ 1,029:
Console.WriteLine("{0}", y);
 
0 // return an integer exit code</langsyntaxhighlight>
{{out}}
<pre>[(6, 25.5, 40, 42.5, 49)]
Line 510 ⟶ 1,036:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
Line 551 ⟶ 1,077:
[ fivenum . ] tri@ ;
 
MAIN: fivenum-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 561 ⟶ 1,087:
=={{header|Go}}==
{{trans|Perl}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 597 ⟶ 1,123:
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 608 ⟶ 1,134:
 
This solution is aimed at handling larger data sets more efficiently. It replaces the O(n log n) sort with O(n) quickselect. It also does not attempt to reproduce the R result exactly, to average values to get a median of an even number of data values, or otherwise estimate quantiles. The quickselect here leaves the input partitioned around the selected value, which allows another small optimization: The first quickselect call partitions the full input around the median. The second call, to get the first quartile, thus only has to process the partition up to the median. The third call, to get the minimum, only has to process the partition up to the first quartile. The 3rd quartile and maximum are obtained similarly.
<langsyntaxhighlight lang="go">package main
 
import (
Line 672 ⟶ 1,198:
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 679 ⟶ 1,205:
[-1.95059594 -0.62759469 0.14082834 0.73438555 1.73131507]
</pre>
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
 
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
 
static void main(String[] args) {
double[][] xl = [
[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
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}</syntaxhighlight>
{{out}}
<pre>[6.0, 25.5, 40.0, 42.5, 49.0]
[7.0, 15.0, 37.5, 40.0, 41.0]
[-1.95059594, -0.676741205, 0.23324706, 0.746070945, 1.73131507]</pre>
 
=={{header|Haskell}}==
{{trans|Python}}
<langsyntaxhighlight lang="haskell">import Data.List (sort)
 
fivenum :: [Double] -> [Double]
:: (Fractional a, Ord a)
=> [a] -> [a]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
((/ 2) . ((+) . (!!) s . floor <*> (!!) s . ceiling) . pred)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
Line 697 ⟶ 1,278:
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
 
testValues :: [Double]
testValues =
[ 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
]
 
main :: IO ()
main =
main = print $ fivenum testValues</lang>
print $
fivenum
[ 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
]</syntaxhighlight>
{{out}}
<pre>[-1.95059594,-0.676741205,0.23324706,0.746070945,1.73131507]</pre>
Line 729 ⟶ 1,309:
=={{header|J}}==
'''Solution'''
<langsyntaxhighlight lang="j">midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #) NB. mid points of y
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@]) NB. quartiles of y
fivenum=: <./ , quartiles , >./ NB. fivenum summary of y</langsyntaxhighlight>
'''Example Usage'''
<langsyntaxhighlight lang="j"> test1=: 15 6 42 41 7 36 49 40 39 47 43
test2=: 36 40 7 39 41 15
test3=: , 0 ". ];._2 noun define
Line 744 ⟶ 1,324:
6 25.5 40 42.5 49
7 15 37.5 40 41
_1.9506 _0.676741 0.233247 0.746071 1.73132</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class Fivenum {
Line 789 ⟶ 1,369:
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}</langsyntaxhighlight>
 
{{out}}
Line 799 ⟶ 1,379:
[-1.95059594, -0.676741205, 0.23324706, 0.746070945, 1.73131507]
</pre>
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">
function median(arr) {
let mid = Math.floor(arr.length / 2);
return (arr.length % 2 == 0) ? (arr[mid-1] + arr[mid]) / 2 : arr[mid];
}
 
Array.prototype.fiveNums = function() {
this.sort(function(a, b) { return a - b} );
let mid = Math.floor(this.length / 2),
loQ = (this.length % 2 == 0) ? this.slice(0, mid) : this.slice(0, mid+1),
hiQ = this.slice(mid);
return [ this[0],
median(loQ),
median(this),
median(hiQ),
this[this.length-1] ];
}
 
// testing
let test = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43];
console.log( test.fiveNums() );
 
test = [0, 0, 1, 2, 63, 61, 27, 13];
console.log( test.fiveNums() );
 
test = [ 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];
console.log( test.fiveNums() );
</syntaxhighlight>
{{out}}<pre>
> Array(5) [ 6, 25.5, 40, 42.5, 49 ]
> Array(5) [ 0, 0.5, 7.5, 44, 63 ]
> Array(5) [ -1.95059594, -0.676741205, 0.23324706, 0.746070945, 1.73131507 ]
</pre>
 
=={{header|jq}}==
{{trans|Wren}}
<syntaxhighlight lang=jq>
def fivenum:
def mid($i):
.[($i - 1)|floor] as $floor
| .[($i - 1)|ceil] as $ceil
| if $ceil == $floor then $ceil else ($floor+$ceil)/2 end;
sort
| length as $n
| (($n + 3) / 2 | floor / 2) as $n4
| [mid(1, $n4, (($n + 1)/2), $n + 1 - $n4, $n)] ;
 
def x1: [36, 40, 7, 39, 41, 15];
def x2: [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43];
def x3: [
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
];
 
[x1, x2, x3][] | fivenum
</syntaxhighlight>
{{Output}}
As for [[#Wren]].
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
Line 830 ⟶ 1,475:
0.75775634, 0.32566578])
println("# ", v, "\n -> ", fivenum(v))
end</langsyntaxhighlight>
 
{{out}}
Line 844 ⟶ 1,489:
 
As arrays containing NaNs and nulls cannot really be dealt with in a sensible fashion in Kotlin, they've been excluded altogether.
<langsyntaxhighlight lang="scala">// version 1.2.21
 
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
Line 879 ⟶ 1,524:
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}</langsyntaxhighlight>
 
{{out}}
Line 891 ⟶ 1,536:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function slice(tbl, low, high)
local copy = {}
 
Line 942 ⟶ 1,587:
for i,x in ipairs(x1) do
print(fivenum(x))
end</langsyntaxhighlight>
{{out}}
<pre>6 25.5 40 43 49
Line 950 ⟶ 1,595:
=={{header|MATLAB}} / {{header|Octave}}==
 
<syntaxhighlight lang="matlab">
<lang Matlab>
function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
</syntaxhighlight>
</lang>
 
 
Line 973 ⟶ 1,618:
 
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]</syntaxhighlight>
{{out}}
<pre>{-3.70325, -0.686977, -0.0087185, 0.652979, 3.67416}</pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Fivenum;
FROM FormatString IMPORT FormatString;
FROM LongStr IMPORT RealToStr;
Line 1,079 ⟶ 1,731:
 
ReadChar
END Fivenum.</langsyntaxhighlight>
{{out}}
<pre>[6.000000000000000, 25.499999999999900, 40.000000000000000, 42.499999999999900, 49.000000000000000, ]
Line 1,086 ⟶ 1,738:
 
[-1.950594000000000, -0.676741205000000, 0.233247060000000, 0.746070945000000, 1.731315070000000, ]</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">import algorithm
 
type FiveNum = array[5, float]
 
template isOdd(n: SomeInteger): bool = (n and 1) != 0
 
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
 
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
 
const Lists = [@[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]]
 
for list in Lists:
echo ""
echo list
echo " → ", list.fivenum</syntaxhighlight>
 
{{out}}
<pre>
@[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0]
→ [6.0, 25.5, 40.0, 42.5, 49.0]
 
@[36.0, 40.0, 7.0, 39.0, 41.0, 15.0]
→ [7.0, 15.0, 37.5, 40.0, 41.0]
 
@[0.14082834, 0.0974879, 1.73131507, 0.87636009, -1.95059594, 0.7343855500000001, -0.03035726, 1.4667597, -0.74621349, -0.72588772, 0.6390516000000001, 0.61501527, -0.9898378, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578]
→ [-1.95059594, -0.676741205, 0.23324706, 0.746070945, 1.73131507]</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">use POSIX qw(ceil floor);
 
sub fivenum {
Line 1,119 ⟶ 1,818:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578);
@tukey = fivenum(\@x);
say join (',', @tukey);</langsyntaxhighlight>
 
{{out}}
Line 1,127 ⟶ 1,826:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function median(sequence tbl, integer lo, hi)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer l = hi-lo+1
<span style="color: #008080;">function</span> <span style="color: #000000;">median</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">lo</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hi</span><span style="color: #0000FF;">)</span>
integer m = lo+floor(l/2)
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hi</span><span style="color: #0000FF;">-</span><span style="color: #000000;">lo</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
if remainder(l,2)=1 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lo</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
return tbl[m]
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span>
return (tbl[m-1]+tbl[m])/2
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">])/</span><span style="color: #000000;">2</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function fivenum(sequence tbl)
tbl = sort(tbl)
<span style="color: #008080;">function</span> <span style="color: #000000;">fivenum</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">)</span>
integer l = length(tbl),
<span style="color: #000000;">tbl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">))</span>
m = floor(l/2)+remainder(l,2)
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
atom r1 = tbl[1],
r2 = median(tbl,1,m),
<span style="color: #004080;">atom</span> <span style="color: #000000;">r1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span>
r3 = median(tbl,1,l),
<span style="color: #000000;">r2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">median</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">),</span>
r4 = median(tbl,m+1,l),
<span style="color: #000000;">r3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">median</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span>
r5 = tbl[l]
<span style="color: #000000;">r4</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">median</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tbl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">r5</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span>
return {r1, r2, r3, r4, r5}
end function
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r5</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant x1 = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43},
x2 = {36, 40, 7, 39, 41, 15},
<span style="color: #008080;">constant</span> <span style="color: #000000;">x1</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">42</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">41</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">36</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">49</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">40</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">39</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">47</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">43</span><span style="color: #0000FF;">},</span>
x3 = {0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
<span style="color: #000000;">x2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">40</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">39</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">41</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15</span><span style="color: #0000FF;">},</span>
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
<span style="color: #000000;">x3</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0.14082834</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.09748790</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.73131507</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.87636009</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.95059594</span><span style="color: #0000FF;">,</span>
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
<span style="color: #000000;">0.73438555</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.03035726</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.46675970</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.74621349</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.72588772</span><span style="color: #0000FF;">,</span>
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
<span style="color: #000000;">0.63905160</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.61501527</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.98983780</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.00447874</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.62759469</span><span style="color: #0000FF;">,</span>
?fivenum(x1)
<span style="color: #000000;">0.66206163</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.04312009</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.10305385</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.75775634</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.32566578</span><span style="color: #0000FF;">}</span>
?fivenum(x2)
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fivenum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">))</span>
?fivenum(x3)</lang>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fivenum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fivenum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x3</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,167 ⟶ 1,869:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de median (Lst)
(let N (length Lst)
(if (bit? 1 N)
Line 1,194 ⟶ 1,896:
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 ) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 1,207 ⟶ 1,909:
 
'''Work with: Python 3'''
<langsyntaxhighlight lang="python">from __future__ import division
import math
import sys
Line 1,234 ⟶ 1,936:
 
y = fivenum(x)
print(y)</langsyntaxhighlight>
 
{{out}}
Line 1,245 ⟶ 1,947:
 
(Though these 25% and 75% values do '''not''' correspond to the Fivenum Tukey quartile values specified in this task)
<langsyntaxhighlight lang="python">import pandas as pd
pd.DataFrame([1, 2, 3, 4, 5, 6]).describe()</langsyntaxhighlight>
 
{{out}}
Line 1,260 ⟶ 1,962:
 
To get the fivenum values asked for, the [https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.quantile.html pandas.DataFrame.quantile] function can be used:
<langsyntaxhighlight lang="python">import pandas as pd
pd.DataFrame([1, 2, 3, 4, 5, 6]).quantile([.0, .25, .50, .75, 1.00], interpolation='nearest')</langsyntaxhighlight>
 
{{out}}
Line 1,275 ⟶ 1,977:
===Python: Functional – without imports===
'''Works with: Python 3'''
<langsyntaxhighlight lang="python"># fiveNums :: [Float] -> (Float, Float, Float, Float, Float)
def fiveNums(xs):
def median(xs):
Line 1,307 ⟶ 2,009:
print(
fiveNums(xs)
)</langsyntaxhighlight>
{{Out}}
<pre>(6, 25.5, 40, 42.5, 49)
Line 1,316 ⟶ 2,018:
The '''fivenum''' function is built-in, see [https://stat.ethz.ch/R-manual/R-devel/library/stats/html/fivenum.html R manual].
 
<langsyntaxhighlight Rlang="r">x <- c(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)
 
fivenum(x)</langsyntaxhighlight>
 
'''Output'''
Line 1,328 ⟶ 2,030:
Racket's =quantile= functions use a different method to Tukey; so a new implementation was made.
 
<langsyntaxhighlight lang="racket">#lang racket/base
(require math/private/statistics/quickselect)
 
Line 1,381 ⟶ 2,083:
-0.98983780 -1.00447874 -0.62759469 0.66206163 1.04312009 -0.10305385
0.75775634 0.32566578))
"Test against Go results x3"))</langsyntaxhighlight>
 
This program passes its tests silently.
Line 1,388 ⟶ 2,090:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>sub fourths ( Int $end ) {
my $end_22 = $end div 2 / 2;
 
Line 1,409 ⟶ 2,111:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
];
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,418 ⟶ 2,120:
=={{header|Relation}}==
Min, median and max are built in, quarter1 and quarter3 calculated.
<syntaxhighlight lang="relation">
<lang Relation>
program fivenum(X)
rename X^ x
Line 1,460 ⟶ 2,162:
insert 8
run fivenum("a")
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,472 ⟶ 2,174:
=={{header|REXX}}==
Programming note: &nbsp; this REXX program uses a unity─based array.
<langsyntaxhighlight lang="rexx">/*REXX program computes the five─number summary (LO─value, p25, medium, p75, HI─value).*/
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43 /*Not specified? Then use the defaults*/
Line 1,482 ⟶ 2,184:
bSort: procedure expose @.; parse arg n; m=n-1 /*N: the number of @ array elements.*/
do m=m for m by -1 until ok; ok= 1 /*keep sorting the @ array 'til done.*/
do j=1 for m; k= j + 1; if @.j<=@.k then/*set iterate K /*In order?to the next item in @ Goodarray.*/
parse valueif @.j <=@.k 0 then iterate with @.k @.j ok /*swapIs 2 elements;@.J flagin asnumerical ¬doneorder? Good. */
parse value @.j @.k 0 with @.k @.j ok /*swap two elements and flag as ¬done.*/
end /*j*/
end /*m*/; return
Line 1,495 ⟶ 2,198:
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end /*j*/ /* [↑] traipse thru array, find min,max*/
call bSort # /*use a bubble sort (easiest to code). */
if #//2 then p25= q2 /*calculate the second quartile number. */
else p25= q2 - 1 /* " " " " " */
return LO med(1, p25) med(1, #) med(q2, #) HI /*return list of 5 numbers.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 15 6 42 41 7 36 49 40 39 47 43 </tt>}}
<pre>
Line 1,512 ⟶ 2,215:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
rem1 = 0
rem2 = 0
Line 1,555 ⟶ 2,258:
next
? "[" + left(svect, len(svect) - 1) + "]"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,561 ⟶ 2,264:
[7,15,37.5,40,41]
[-1.95059594,-0.67674121,0.23324706,0.74607095,1.73131507]
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ '''IF''' DUP SIZE 2 ≥ '''THEN'''
LIST→ → len
≪ len 1 '''FOR''' n
1 n 1 - '''START'''
'''IF''' DUP2 > '''THEN''' SWAP '''END'''
n ROLLD
'''NEXT''' n ROLLD
-1 '''STEP''' len →LIST
≫ '''END'''
≫ ‘'''SORTL'''’ STO
≪ → data n
≪ data SIZE n * 4 n - + 4 /
data OVER FLOOR GET n *
data ROT CEIL GET 4 n - * + 4 /
≫ ≫ ‘'''QTL'''’ STO
≪ '''SORTL''' { }
OVER 1 GET + OVER 1 '''QTL''' + OVER 2 '''QTL''' + OVER 3 '''QTL''' + OVER DUP SIZE GET +
≫ ‘'''SUMR5'''’ STO
{ 6 7 15 36 39 40 41 42 43 47 49 } '''SUMR5'''
{{out}}
<pre>
1: { 6 30.75 40 42.25 49 }
</pre>
 
=={{header|Ruby}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">def fivenum(array)
sorted_arr = array.sort
n = array.size
Line 1,591 ⟶ 2,323:
tukey_array = fivenum(test_array)
p tukey_array
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,597 ⟶ 2,329:
[7.0, 15.0, 36.0, 40.0, 41.0]
[-1.95059594, -0.72588772, 0.14082834, 0.75775634, 1.73131507]</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
#[derive(Debug)]
struct FiveNum {
minimum: f64,
lower_quartile: f64,
median: f64,
upper_quartile: f64,
maximum: f64,
}
 
fn median(samples: &[f64]) -> f64 {
// input is already sorted
let n = samples.len();
let m = n / 2;
if n % 2 == 0 {
(samples[m] + samples[m - 1]) / 2.0
} else {
samples[m]
}
}
 
fn fivenum(samples: &[f64]) -> FiveNum {
let mut xs = samples.to_vec();
xs.sort_by(|x, y| x.partial_cmp(y).unwrap());
 
let m = xs.len() / 2;
 
FiveNum {
minimum: xs[0],
lower_quartile: median(&xs[0..(m + (xs.len() % 2))]),
median: median(&xs),
upper_quartile: median(&xs[m..]),
maximum: xs[xs.len() - 1],
}
}
fn main() {
let inputs = vec![
vec![15., 6., 42., 41., 7., 36., 49., 40., 39., 47., 43.],
vec![36., 40., 7., 39., 41., 15.],
vec![
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,
],
];
 
for input in inputs {
let result = fivenum(&input);
println!("Fivenum",);
println!(" Minumum: {}", result.minimum);
println!(" Lower quartile: {}", result.lower_quartile);
println!(" Median: {}", result.median);
println!(" Upper quartile: {}", result.upper_quartile);
println!(" Maximum: {}", result.maximum);
}
}
</syntaxhighlight>
{{out}}
<pre>
Fivenum
Minumum: 6
Lower quartile: 25.5
Median: 40
Upper quartile: 42.5
Maximum: 49
Fivenum
Minumum: 7
Lower quartile: 15
Median: 37.5
Upper quartile: 40
Maximum: 41
Fivenum
Minumum: -1.95059594
Lower quartile: -0.676741205
Median: 0.23324706
Upper quartile: 0.746070945
Maximum: 1.73131507
</pre>
 
=={{header|SAS}}==
<langsyntaxhighlight lang="sas">/* build a dataset */
data test;
do i=1 to 10000;
Line 1,611 ⟶ 2,440:
proc means data=test min p25 median p75 max;
var x;
run;</langsyntaxhighlight>
 
'''Output'''
Line 1,637 ⟶ 2,466:
=={{header|Scala}}==
===Array based solution===
<langsyntaxhighlight Scalalang="scala">import java.util
 
object Fivenum extends App {
Line 1,673 ⟶ 2,502:
}
 
}</langsyntaxhighlight>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/8s0OdOO/2 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/Ady3dSnoRRKNhCaZYIVbig Scastie (JVM)].
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func fourths(e) {
var t = ((e>>1) / 2)
[0, t, e/2, e - t, e]
Line 1,699 ⟶ 2,528:
]]
 
nums.each { say fivenum(_).join(', ') }</langsyntaxhighlight>
{{out}}
<pre>6, 25.5, 40, 42.5, 49
Line 1,708 ⟶ 2,537:
First build a dataset:
 
<langsyntaxhighlight lang="stata">clear
set seed 17760704
qui set obs 10000
gen x=rnormal()</langsyntaxhighlight>
 
The '''[https://www.stata.com/help.cgi?summarize summarize]''' command produces all the required statistics, and more:
 
<langsyntaxhighlight lang="stata">qui sum x, detail
di r(min),r(p25),r(p50),r(p75),r(max)</langsyntaxhighlight>
 
'''Output'''
Line 1,724 ⟶ 2,553:
It's also possible to use the '''[https://www.stata.com/help.cgi?tabstat tabstat]''' command
 
<langsyntaxhighlight lang="stata">tabstat x, s(mi q ma)</langsyntaxhighlight>
 
'''Output'''
Line 1,735 ⟶ 2,564:
Another example:
 
<langsyntaxhighlight lang="stata">clear
mat a=0.14082834\0.09748790\1.73131507\0.87636009\-1.95059594\ ///
0.73438555\-0.03035726\1.46675970\-0.74621349\-0.72588772\ ///
Line 1,741 ⟶ 2,570:
0.66206163\1.04312009\-0.10305385\0.75775634\0.32566578
svmat a
tabstat a1, s(mi q ma)</langsyntaxhighlight>
 
'''Output'''
Line 1,749 ⟶ 2,578:
a1 | -1.950596 -.6767412 .2332471 .746071 1.731315
----------------------------------------------------------------</pre>
 
=={{header|VBA}}==
Uses [[Sorting_algorithms/Quicksort#VBA|Quicksort]].
{{trans|Phix}}<lang vb>Option Base 1
Private Function median(tbl As Variant, lo As Integer, hi As Integer)
Dim l As Integer: l = hi - lo + 1
Dim m As Integer: m = lo + WorksheetFunction.Floor_Precise(l / 2)
If l Mod 2 = 1 Then
median = tbl(m)
Else
median = (tbl(m - 1) + tbl(m)) / 2
End if
End Function
Private Function fivenum(tbl As Variant) As Variant
Sort tbl, UBound(tbl)
Dim l As Integer: l = UBound(tbl)
Dim m As Integer: m = WorksheetFunction.Floor_Precise(l / 2) + l Mod 2
Dim r(5) As String
r(1) = CStr(tbl(1))
r(2) = CStr(median(tbl, 1, m))
r(3) = CStr(median(tbl, 1, l))
r(4) = CStr(median(tbl, m + 1, l))
r(5) = CStr(tbl(l))
fivenum = r
End Function
Public Sub main()
Dim x1 As Variant, x2 As Variant, x3 As Variant
x1 = [{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}]
x2 = [{36, 40, 7, 39, 41, 15}]
x3 = [{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}]
Debug.Print Join(fivenum(x1), " | ")
Debug.Print Join(fivenum(x2), " | ")
Debug.Print Join(fivenum(x3), " | ")
End Sub</lang>{{out}}
<pre>6 | 25,5 | 40 | 43 | 49
7 | 15 | 37,5 | 40 | 41
-1,95059594 | -0,676741205 | 0,23324706 | 0,746070945 | 1,73131507</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<lang vbnet>Imports System.Runtime.CompilerServices
Imports System.Text
 
Module Module1
 
<Extension()>
Function AsString(Of T)(c As ICollection(Of T), Optional format As String = "{0}") As String
Dim sb As New StringBuilder("[")
Dim it = c.GetEnumerator()
If it.MoveNext() Then
sb.AppendFormat(format, it.Current)
End If
While it.MoveNext()
sb.Append(", ")
sb.AppendFormat(format, it.Current)
End While
Return sb.Append("]").ToString()
End Function
 
Function Median(x As Double(), start As Integer, endInclusive As Integer) As Double
Dim size = endInclusive - start + 1
If size <= 0 Then
Throw New ArgumentException("Array slice cannot be empty")
End If
Dim m = start + size \ 2
Return If(size Mod 2 = 1, x(m), (x(m - 1) + x(m)) / 2.0)
End Function
 
Function Fivenum(x As Double()) As Double()
For Each d In x
If Double.IsNaN(d) Then
Throw New ArgumentException("Unable to deal with arrays containing NaN")
End If
Next
 
Array.Sort(x)
Dim result(4) As Double
 
result(0) = x.First()
result(2) = Median(x, 0, x.Length - 1)
result(4) = x.Last()
 
Dim m = x.Length \ 2
Dim lowerEnd = If(x.Length Mod 2 = 1, m, m - 1)
 
result(1) = Median(x, 0, lowerEnd)
result(3) = Median(x, m, x.Length - 1)
 
Return result
End Function
 
Sub Main()
Dim x1 = {
New Double() {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
New Double() {36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
New Double() {
0.14082834, 0.0974879, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.4667597, -0.74621349, -0.72588772, 0.6390516, 0.61501527,
-0.9898378, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
For Each x In x1
Dim result = Fivenum(x)
Console.WriteLine(result.AsString("{0:F8}"))
Next
End Sub
 
End Module</lang>
{{out}}
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000]
[7.00000000, 15.00000000, 37.50000000, 40.00000000, 41.00000000]
[-1.95059594, -0.67674121, 0.23324706, 0.74607095, 1.73131507]</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-sort}}
<langsyntaxhighlight ecmascriptlang="wren">import "./sort" for Sort
 
var fivenum = Fn.new { |a|
Line 1,892 ⟶ 2,608:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578
]
for (x in [x1, x2, x3]) System.print(fivenum.call(x))</langsyntaxhighlight>
 
{{out}}
Line 1,903 ⟶ 2,619:
=={{header|zkl}}==
Uses GNU GSL library.
<langsyntaxhighlight lang="zkl">var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
fcn fiveNum(v){ // V is a GSL Vector, --> min, 1st qu, median, 3rd qu, max
v.sort();
return(v.min(),v.quantile(0.25),v.median(),v.quantile(0.75),v.max())
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fiveNum(GSL.VectorFromData(
15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0)).println();
println(fiveNum(GSL.VectorFromData(36.0, 40.0, 7.0, 39.0, 41.0, 15.0)));
Line 1,917 ⟶ 2,633:
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578);
println(fiveNum(v));</langsyntaxhighlight>
{{out}}
<pre>
3,022

edits