Fivenum: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|F#|F sharp}}: Regularize header markup to recommended on category page) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 19: | Line 19: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F fivenum(array) |
||
V n = array.len |
V n = array.len |
||
V x = sorted(array) |
V x = sorted(array) |
||
Line 35: | Line 35: | ||
1.04312009, -0.10305385, 0.75775634, 0.32566578] |
1.04312009, -0.10305385, 0.75775634, 0.32566578] |
||
print(fivenum(x))</ |
print(fivenum(x))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 45: | Line 45: | ||
==={{header|Direct C Translation}}=== |
==={{header|Direct C Translation}}=== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Containers.Generic_Array_Sort; |
with Ada.Containers.Generic_Array_Sort; |
||
Line 116: | Line 116: | ||
print (Result, 9); |
print (Result, 9); |
||
end Main; |
end Main; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
Line 126: | Line 126: | ||
</pre> |
</pre> |
||
==={{header|Using Ada Enumeration}}=== |
==={{header|Using Ada Enumeration}}=== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Containers.Generic_Array_Sort; |
with Ada.Containers.Generic_Array_Sort; |
||
Line 206: | Line 206: | ||
print (Result); |
print (Result); |
||
end Main; |
end Main; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
Line 223: | Line 223: | ||
Includes additional test cases and adjustment to n4 for odd length array as in a number of other samples. |
Includes additional test cases and adjustment to n4 for odd length array as in a number of other samples. |
||
{{libheader|ALGOL 68-rows}} |
{{libheader|ALGOL 68-rows}} |
||
< |
<syntaxhighlight lang="algol68">BEGIN # construct an R-style fivenum function # |
||
PR read "rows.incl.a68" PR |
PR read "rows.incl.a68" PR |
||
Line 253: | Line 253: | ||
) |
) |
||
) |
) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 263: | Line 263: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- Mac OS X 10.10. (Yosemite) or later. |
||
use framework "Foundation" |
use framework "Foundation" |
||
Line 297: | Line 297: | ||
set z to {0.14082834, 0.0974879, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.4667597, -0.74621349, -0.72588772, ¬ |
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} |
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)}</ |
return {fivenum(x, 1, count x), fivenum(y, 1, count y), fivenum(z, 1, count z)}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight 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}}</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">fivenum: function [lst][ |
||
lst: sort lst |
lst: sort lst |
||
m: (size lst)/2 |
m: (size lst)/2 |
||
Line 331: | Line 331: | ||
print [fivenum l] |
print [fivenum l] |
||
print "" |
print "" |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 346: | Line 346: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 417: | Line 417: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 430: | Line 430: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 493: | Line 493: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000] |
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000] |
||
Line 501: | Line 501: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <iostream> |
#include <iostream> |
||
#include <ostream> |
#include <ostream> |
||
Line 579: | Line 579: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(6, 25.5, 40, 43, 49) |
<pre>(6, 25.5, 40, 43, 49) |
||
Line 587: | Line 587: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="d">import std.algorithm; |
||
import std.exception; |
import std.exception; |
||
import std.math; |
import std.math; |
||
Line 636: | Line 636: | ||
writeln(fivenum(x)); |
writeln(fivenum(x)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[6, 25.5, 40, 43, 49] |
<pre>[6, 25.5, 40, 43, 49] |
||
Line 645: | Line 645: | ||
{{libheader| System.Generics.Collections}} |
{{libheader| System.Generics.Collections}} |
||
{{Trans|Java}} |
{{Trans|Java}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Fivenum; |
program Fivenum; |
||
Line 714: | Line 714: | ||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 725: | Line 725: | ||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
// Take from https://stackoverflow.com/a/1175123 |
// Take from https://stackoverflow.com/a/1175123 |
||
Line 766: | Line 766: | ||
Console.WriteLine("{0}", y); |
Console.WriteLine("{0}", y); |
||
0 // return an integer exit code</ |
0 // return an integer exit code</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[(6, 25.5, 40, 42.5, 49)] |
<pre>[(6, 25.5, 40, 42.5, 49)] |
||
Line 773: | Line 773: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: combinators combinators.smart kernel math |
||
math.statistics prettyprint sequences sorting ; |
math.statistics prettyprint sequences sorting ; |
||
IN: rosetta-code.five-number |
IN: rosetta-code.five-number |
||
Line 814: | Line 814: | ||
[ fivenum . ] tri@ ; |
[ fivenum . ] tri@ ; |
||
MAIN: fivenum-demo</ |
MAIN: fivenum-demo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 824: | Line 824: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 860: | Line 860: | ||
fmt.Println(fivenum(x2)) |
fmt.Println(fivenum(x2)) |
||
fmt.Println(fivenum(x3)) |
fmt.Println(fivenum(x3)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 871: | Line 871: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 935: | Line 935: | ||
fmt.Println(fivenum(x2)) |
fmt.Println(fivenum(x2)) |
||
fmt.Println(fivenum(x3)) |
fmt.Println(fivenum(x3)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 944: | Line 944: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="groovy">class Fivenum { |
||
static double median(double[] x, int start, int endInclusive) { |
static double median(double[] x, int start, int endInclusive) { |
||
int size = endInclusive - start + 1 |
int size = endInclusive - start + 1 |
||
Line 987: | Line 987: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[6.0, 25.5, 40.0, 42.5, 49.0] |
<pre>[6.0, 25.5, 40.0, 42.5, 49.0] |
||
Line 995: | Line 995: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="haskell">import Data.List (sort) |
||
fivenum :: [Double] -> [Double] |
fivenum :: [Double] -> [Double] |
||
Line 1,040: | Line 1,040: | ||
0.75775634, |
0.75775634, |
||
0.32566578 |
0.32566578 |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[-1.95059594,-0.676741205,0.23324706,0.746070945,1.73131507]</pre> |
<pre>[-1.95059594,-0.676741205,0.23324706,0.746070945,1.73131507]</pre> |
||
Line 1,046: | Line 1,046: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution''' |
'''Solution''' |
||
< |
<syntaxhighlight lang="j">midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #) NB. mid points of y |
||
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@]) NB. quartiles of y |
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@]) NB. quartiles of y |
||
fivenum=: <./ , quartiles , >./ NB. fivenum summary of y</ |
fivenum=: <./ , quartiles , >./ NB. fivenum summary of y</syntaxhighlight> |
||
'''Example Usage''' |
'''Example Usage''' |
||
< |
<syntaxhighlight lang="j"> test1=: 15 6 42 41 7 36 49 40 39 47 43 |
||
test2=: 36 40 7 39 41 15 |
test2=: 36 40 7 39 41 15 |
||
test3=: , 0 ". ];._2 noun define |
test3=: , 0 ". ];._2 noun define |
||
Line 1,061: | Line 1,061: | ||
6 25.5 40 42.5 49 |
6 25.5 40 42.5 49 |
||
7 15 37.5 40 41 |
7 15 37.5 40 41 |
||
_1.9506 _0.676741 0.233247 0.746071 1.73132</ |
_1.9506 _0.676741 0.233247 0.746071 1.73132</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
public class Fivenum { |
public class Fivenum { |
||
Line 1,106: | Line 1,106: | ||
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x))); |
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x))); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,118: | Line 1,118: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript"> |
||
function median(arr) { |
function median(arr) { |
||
let mid = Math.floor(arr.length / 2); |
let mid = Math.floor(arr.length / 2); |
||
Line 1,148: | Line 1,148: | ||
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578]; |
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578]; |
||
console.log( test.fiveNums() ); |
console.log( test.fiveNums() ); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre> |
{{out}}<pre> |
||
> Array(5) [ 6, 25.5, 40, 42.5, 49 ] |
> Array(5) [ 6, 25.5, 40, 42.5, 49 ] |
||
Line 1,158: | Line 1,158: | ||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang="julia">function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T |
||
len = l - i + 1 |
len = l - i + 1 |
||
len > zero(len) || throw(ArgumentError("Array slice cannot be empty.")) |
len > zero(len) || throw(ArgumentError("Array slice cannot be empty.")) |
||
Line 1,185: | Line 1,185: | ||
0.75775634, 0.32566578]) |
0.75775634, 0.32566578]) |
||
println("# ", v, "\n -> ", fivenum(v)) |
println("# ", v, "\n -> ", fivenum(v)) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,199: | Line 1,199: | ||
As arrays containing NaNs and nulls cannot really be dealt with in a sensible fashion in Kotlin, they've been excluded altogether. |
As arrays containing NaNs and nulls cannot really be dealt with in a sensible fashion in Kotlin, they've been excluded altogether. |
||
< |
<syntaxhighlight lang="scala">// version 1.2.21 |
||
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double { |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double { |
||
Line 1,234: | Line 1,234: | ||
) |
) |
||
xl.forEach { println("${fivenum(it).asList()}\n") } |
xl.forEach { println("${fivenum(it).asList()}\n") } |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,246: | Line 1,246: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function slice(tbl, low, high) |
||
local copy = {} |
local copy = {} |
||
Line 1,297: | Line 1,297: | ||
for i,x in ipairs(x1) do |
for i,x in ipairs(x1) do |
||
print(fivenum(x)) |
print(fivenum(x)) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>6 25.5 40 43 49 |
<pre>6 25.5 40 43 49 |
||
Line 1,305: | Line 1,305: | ||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
<syntaxhighlight lang="matlab"> |
|||
<lang Matlab> |
|||
function r = fivenum(x) |
function r = fivenum(x) |
||
r = quantile(x,[0:4]/4); |
r = quantile(x,[0:4]/4); |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 1,330: | Line 1,330: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[FiveNum] |
||
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]] |
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]] |
||
FiveNum[RandomVariate[NormalDistribution[], 10000]]</ |
FiveNum[RandomVariate[NormalDistribution[], 10000]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{-3.70325, -0.686977, -0.0087185, 0.652979, 3.67416}</pre> |
<pre>{-3.70325, -0.686977, -0.0087185, 0.652979, 3.67416}</pre> |
||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang="modula2">MODULE Fivenum; |
||
FROM FormatString IMPORT FormatString; |
FROM FormatString IMPORT FormatString; |
||
FROM LongStr IMPORT RealToStr; |
FROM LongStr IMPORT RealToStr; |
||
Line 1,441: | Line 1,441: | ||
ReadChar |
ReadChar |
||
END Fivenum.</ |
END Fivenum.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[6.000000000000000, 25.499999999999900, 40.000000000000000, 42.499999999999900, 49.000000000000000, ] |
<pre>[6.000000000000000, 25.499999999999900, 40.000000000000000, 42.499999999999900, 49.000000000000000, ] |
||
Line 1,451: | Line 1,451: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="nim">import algorithm |
||
type FiveNum = array[5, float] |
type FiveNum = array[5, float] |
||
Line 1,483: | Line 1,483: | ||
echo "" |
echo "" |
||
echo list |
echo list |
||
echo " → ", list.fivenum</ |
echo " → ", list.fivenum</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,497: | Line 1,497: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use POSIX qw(ceil floor); |
||
sub fivenum { |
sub fivenum { |
||
Line 1,528: | Line 1,528: | ||
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578); |
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578); |
||
@tukey = fivenum(\@x); |
@tukey = fivenum(\@x); |
||
say join (',', @tukey);</ |
say join (',', @tukey);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,536: | Line 1,536: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<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> |
<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> |
||
Line 1,570: | Line 1,570: | ||
<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;">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> |
<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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,579: | Line 1,579: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de median (Lst) |
||
(let N (length Lst) |
(let N (length Lst) |
||
(if (bit? 1 N) |
(if (bit? 1 N) |
||
Line 1,606: | Line 1,606: | ||
0.73438555 -0.03035726 1.46675970 -0.74621349 -0.72588772 |
0.73438555 -0.03035726 1.46675970 -0.74621349 -0.72588772 |
||
0.63905160 0.61501527 -0.98983780 -1.00447874 -0.62759469 |
0.63905160 0.61501527 -0.98983780 -1.00447874 -0.62759469 |
||
0.66206163 1.04312009 -0.10305385 0.75775634 0.32566578 ) ) )</ |
0.66206163 1.04312009 -0.10305385 0.75775634 0.32566578 ) ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,619: | Line 1,619: | ||
'''Work with: Python 3''' |
'''Work with: Python 3''' |
||
< |
<syntaxhighlight lang="python">from __future__ import division |
||
import math |
import math |
||
import sys |
import sys |
||
Line 1,646: | Line 1,646: | ||
y = fivenum(x) |
y = fivenum(x) |
||
print(y)</ |
print(y)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,657: | Line 1,657: | ||
(Though these 25% and 75% values do '''not''' correspond to the Fivenum Tukey quartile values specified in this task) |
(Though these 25% and 75% values do '''not''' correspond to the Fivenum Tukey quartile values specified in this task) |
||
< |
<syntaxhighlight lang="python">import pandas as pd |
||
pd.DataFrame([1, 2, 3, 4, 5, 6]).describe()</ |
pd.DataFrame([1, 2, 3, 4, 5, 6]).describe()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,672: | Line 1,672: | ||
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: |
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: |
||
< |
<syntaxhighlight lang="python">import pandas as pd |
||
pd.DataFrame([1, 2, 3, 4, 5, 6]).quantile([.0, .25, .50, .75, 1.00], interpolation='nearest')</ |
pd.DataFrame([1, 2, 3, 4, 5, 6]).quantile([.0, .25, .50, .75, 1.00], interpolation='nearest')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,687: | Line 1,687: | ||
===Python: Functional – without imports=== |
===Python: Functional – without imports=== |
||
'''Works with: Python 3''' |
'''Works with: Python 3''' |
||
< |
<syntaxhighlight lang="python"># fiveNums :: [Float] -> (Float, Float, Float, Float, Float) |
||
def fiveNums(xs): |
def fiveNums(xs): |
||
def median(xs): |
def median(xs): |
||
Line 1,719: | Line 1,719: | ||
print( |
print( |
||
fiveNums(xs) |
fiveNums(xs) |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>(6, 25.5, 40, 42.5, 49) |
<pre>(6, 25.5, 40, 42.5, 49) |
||
Line 1,728: | Line 1,728: | ||
The '''fivenum''' function is built-in, see [https://stat.ethz.ch/R-manual/R-devel/library/stats/html/fivenum.html R manual]. |
The '''fivenum''' function is built-in, see [https://stat.ethz.ch/R-manual/R-devel/library/stats/html/fivenum.html R manual]. |
||
< |
<syntaxhighlight lang="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)</ |
fivenum(x)</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 1,740: | Line 1,740: | ||
Racket's =quantile= functions use a different method to Tukey; so a new implementation was made. |
Racket's =quantile= functions use a different method to Tukey; so a new implementation was made. |
||
< |
<syntaxhighlight lang="racket">#lang racket/base |
||
(require math/private/statistics/quickselect) |
(require math/private/statistics/quickselect) |
||
Line 1,793: | Line 1,793: | ||
-0.98983780 -1.00447874 -0.62759469 0.66206163 1.04312009 -0.10305385 |
-0.98983780 -1.00447874 -0.62759469 0.66206163 1.04312009 -0.10305385 |
||
0.75775634 0.32566578)) |
0.75775634 0.32566578)) |
||
"Test against Go results x3"))</ |
"Test against Go results x3"))</syntaxhighlight> |
||
This program passes its tests silently. |
This program passes its tests silently. |
||
Line 1,800: | Line 1,800: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub fourths ( Int $end ) { |
||
my $end_22 = $end div 2 / 2; |
my $end_22 = $end div 2 / 2; |
||
Line 1,821: | Line 1,821: | ||
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578, |
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578, |
||
]; |
]; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,830: | Line 1,830: | ||
=={{header|Relation}}== |
=={{header|Relation}}== |
||
Min, median and max are built in, quarter1 and quarter3 calculated. |
Min, median and max are built in, quarter1 and quarter3 calculated. |
||
<syntaxhighlight lang="relation"> |
|||
<lang Relation> |
|||
program fivenum(X) |
program fivenum(X) |
||
rename X^ x |
rename X^ x |
||
Line 1,872: | Line 1,872: | ||
insert 8 |
insert 8 |
||
run fivenum("a") |
run fivenum("a") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,884: | Line 1,884: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Programming note: this REXX program uses a unity─based array. |
Programming note: this REXX program uses a unity─based array. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program computes the five─number summary (LO─value, p25, medium, p75, HI─value).*/ |
||
parse arg x |
parse arg x |
||
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43 /*Not specified? Then use the defaults*/ |
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43 /*Not specified? Then use the defaults*/ |
||
Line 1,912: | Line 1,912: | ||
if #//2 then p25= q2 /*calculate the second quartile number.*/ |
if #//2 then p25= q2 /*calculate the second quartile number.*/ |
||
else p25= q2 - 1 /* " " " " " */ |
else p25= q2 - 1 /* " " " " " */ |
||
return LO med(1, p25) med(1, #) med(q2, #) HI /*return list of 5 numbers.*/</ |
return LO med(1, p25) med(1, #) med(q2, #) HI /*return list of 5 numbers.*/</syntaxhighlight> |
||
{{out|output|text= when using the default input of: <tt> 15 6 42 41 7 36 49 40 39 47 43 </tt>}} |
{{out|output|text= when using the default input of: <tt> 15 6 42 41 7 36 49 40 39 47 43 </tt>}} |
||
<pre> |
<pre> |
||
Line 1,925: | Line 1,925: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
rem1 = 0 |
rem1 = 0 |
||
rem2 = 0 |
rem2 = 0 |
||
Line 1,968: | Line 1,968: | ||
next |
next |
||
? "[" + left(svect, len(svect) - 1) + "]" |
? "[" + left(svect, len(svect) - 1) + "]" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,978: | Line 1,978: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">def fivenum(array) |
||
sorted_arr = array.sort |
sorted_arr = array.sort |
||
n = array.size |
n = array.size |
||
Line 2,004: | Line 2,004: | ||
tukey_array = fivenum(test_array) |
tukey_array = fivenum(test_array) |
||
p tukey_array |
p tukey_array |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,012: | Line 2,012: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
#[derive(Debug)] |
#[derive(Debug)] |
||
struct FiveNum { |
struct FiveNum { |
||
Line 2,085: | Line 2,085: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,109: | Line 2,109: | ||
=={{header|SAS}}== |
=={{header|SAS}}== |
||
< |
<syntaxhighlight lang="sas">/* build a dataset */ |
||
data test; |
data test; |
||
do i=1 to 10000; |
do i=1 to 10000; |
||
Line 2,121: | Line 2,121: | ||
proc means data=test min p25 median p75 max; |
proc means data=test min p25 median p75 max; |
||
var x; |
var x; |
||
run;</ |
run;</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 2,147: | Line 2,147: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===Array based solution=== |
===Array based solution=== |
||
< |
<syntaxhighlight lang="scala">import java.util |
||
object Fivenum extends App { |
object Fivenum extends App { |
||
Line 2,183: | Line 2,183: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{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)]. |
{{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}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">func fourths(e) { |
||
var t = ((e>>1) / 2) |
var t = ((e>>1) / 2) |
||
[0, t, e/2, e - t, e] |
[0, t, e/2, e - t, e] |
||
Line 2,209: | Line 2,209: | ||
]] |
]] |
||
nums.each { say fivenum(_).join(', ') }</ |
nums.each { say fivenum(_).join(', ') }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>6, 25.5, 40, 42.5, 49 |
<pre>6, 25.5, 40, 42.5, 49 |
||
Line 2,218: | Line 2,218: | ||
First build a dataset: |
First build a dataset: |
||
< |
<syntaxhighlight lang="stata">clear |
||
set seed 17760704 |
set seed 17760704 |
||
qui set obs 10000 |
qui set obs 10000 |
||
gen x=rnormal()</ |
gen x=rnormal()</syntaxhighlight> |
||
The '''[https://www.stata.com/help.cgi?summarize summarize]''' command produces all the required statistics, and more: |
The '''[https://www.stata.com/help.cgi?summarize summarize]''' command produces all the required statistics, and more: |
||
< |
<syntaxhighlight lang="stata">qui sum x, detail |
||
di r(min),r(p25),r(p50),r(p75),r(max)</ |
di r(min),r(p25),r(p50),r(p75),r(max)</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 2,234: | Line 2,234: | ||
It's also possible to use the '''[https://www.stata.com/help.cgi?tabstat tabstat]''' command |
It's also possible to use the '''[https://www.stata.com/help.cgi?tabstat tabstat]''' command |
||
< |
<syntaxhighlight lang="stata">tabstat x, s(mi q ma)</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 2,245: | Line 2,245: | ||
Another example: |
Another example: |
||
< |
<syntaxhighlight lang="stata">clear |
||
mat a=0.14082834\0.09748790\1.73131507\0.87636009\-1.95059594\ /// |
mat a=0.14082834\0.09748790\1.73131507\0.87636009\-1.95059594\ /// |
||
0.73438555\-0.03035726\1.46675970\-0.74621349\-0.72588772\ /// |
0.73438555\-0.03035726\1.46675970\-0.74621349\-0.72588772\ /// |
||
Line 2,251: | Line 2,251: | ||
0.66206163\1.04312009\-0.10305385\0.75775634\0.32566578 |
0.66206163\1.04312009\-0.10305385\0.75775634\0.32566578 |
||
svmat a |
svmat a |
||
tabstat a1, s(mi q ma)</ |
tabstat a1, s(mi q ma)</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 2,262: | Line 2,262: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
Uses [[Sorting_algorithms/Quicksort#VBA|Quicksort]]. |
Uses [[Sorting_algorithms/Quicksort#VBA|Quicksort]]. |
||
{{trans|Phix}}< |
{{trans|Phix}}<syntaxhighlight lang="vb">Option Base 1 |
||
Private Function median(tbl As Variant, lo As Integer, hi As Integer) |
Private Function median(tbl As Variant, lo As Integer, hi As Integer) |
||
Dim l As Integer: l = hi - lo + 1 |
Dim l As Integer: l = hi - lo + 1 |
||
Line 2,292: | Line 2,292: | ||
Debug.Print Join(fivenum(x2), " | ") |
Debug.Print Join(fivenum(x2), " | ") |
||
Debug.Print Join(fivenum(x3), " | ") |
Debug.Print Join(fivenum(x3), " | ") |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre>6 | 25,5 | 40 | 43 | 49 |
<pre>6 | 25,5 | 40 | 43 | 49 |
||
7 | 15 | 37,5 | 40 | 41 |
7 | 15 | 37,5 | 40 | 41 |
||
Line 2,299: | Line 2,299: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices |
||
Imports System.Text |
Imports System.Text |
||
Line 2,367: | Line 2,367: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000] |
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000] |
||
Line 2,376: | Line 2,376: | ||
{{trans|Go}} |
{{trans|Go}} |
||
{{libheader|Wren-sort}} |
{{libheader|Wren-sort}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/sort" for Sort |
||
var fivenum = Fn.new { |a| |
var fivenum = Fn.new { |a| |
||
Line 2,402: | Line 2,402: | ||
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578 |
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578 |
||
] |
] |
||
for (x in [x1, x2, x3]) System.print(fivenum.call(x))</ |
for (x in [x1, x2, x3]) System.print(fivenum.call(x))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,413: | Line 2,413: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Uses GNU GSL library. |
Uses GNU GSL library. |
||
< |
<syntaxhighlight 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 |
fcn fiveNum(v){ // V is a GSL Vector, --> min, 1st qu, median, 3rd qu, max |
||
v.sort(); |
v.sort(); |
||
return(v.min(),v.quantile(0.25),v.median(),v.quantile(0.75),v.max()) |
return(v.min(),v.quantile(0.25),v.median(),v.quantile(0.75),v.max()) |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight 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(); |
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))); |
println(fiveNum(GSL.VectorFromData(36.0, 40.0, 7.0, 39.0, 41.0, 15.0))); |
||
Line 2,427: | Line 2,427: | ||
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, |
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, |
||
0.75775634, 0.32566578); |
0.75775634, 0.32566578); |
||
println(fiveNum(v));</ |
println(fiveNum(v));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |