Fivenum: Difference between revisions
m
syntax highlighting fixup automation
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:
{{trans|Python}}
<
V n = array.len
V x = sorted(array)
Line 35:
1.04312009, -0.10305385, 0.75775634, 0.32566578]
print(fivenum(x))</
{{out}}
Line 45:
==={{header|Direct C Translation}}===
{{trans|C}}
<
with Ada.Containers.Generic_Array_Sort;
Line 116:
print (Result, 9);
end Main;
</syntaxhighlight>
{{output}}
<pre>
Line 126:
</pre>
==={{header|Using Ada Enumeration}}===
<
with Ada.Containers.Generic_Array_Sort;
Line 206:
print (Result);
end Main;
</syntaxhighlight>
{{output}}
<pre>
Line 223:
Includes additional test cases and adjustment to n4 for odd length array as in a number of other samples.
{{libheader|ALGOL 68-rows}}
<
PR read "rows.incl.a68" PR
Line 253:
)
)
END</
{{out}}
<pre>
Line 263:
=={{header|AppleScript}}==
<
use framework "Foundation"
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, ¬
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)}</
{{output}}
<
=={{header|Arturo}}==
<
lst: sort lst
m: (size lst)/2
Line 331:
print [fivenum l]
print ""
]</
{{out}}
Line 346:
=={{header|C}}==
{{trans|Kotlin}}
<
#include <stdlib.h>
Line 417:
return 0;
}</
{{out}}
Line 430:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
using System.Collections.Generic;
using System.Linq;
Line 493:
}
}
}</
{{out}}
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000]
Line 501:
=={{header|C++}}==
{{trans|D}}
<
#include <iostream>
#include <ostream>
Line 579:
return 0;
}</
{{out}}
<pre>(6, 25.5, 40, 43, 49)
Line 587:
=={{header|D}}==
{{trans|Java}}
<
import std.exception;
import std.math;
Line 636:
writeln(fivenum(x));
}
}</
{{out}}
<pre>[6, 25.5, 40, 43, 49]
Line 645:
{{libheader| System.Generics.Collections}}
{{Trans|Java}}
<syntaxhighlight lang="delphi">
program Fivenum;
Line 714:
readln;
end.</
{{out}}
<pre>
Line 725:
=={{header|F Sharp|F#}}==
{{trans|C#}}
<
// Take from https://stackoverflow.com/a/1175123
Line 766:
Console.WriteLine("{0}", y);
0 // return an integer exit code</
{{out}}
<pre>[(6, 25.5, 40, 42.5, 49)]
Line 773:
=={{header|Factor}}==
<
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
Line 814:
[ fivenum . ] tri@ ;
MAIN: fivenum-demo</
{{out}}
<pre>
Line 824:
=={{header|Go}}==
{{trans|Perl}}
<
import (
Line 860:
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}</
{{out}}
<pre>
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.
<
import (
Line 935:
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}</
{{out}}
<pre>
Line 944:
=={{header|Groovy}}==
{{trans|Java}}
<
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
Line 987:
}
}
}</
{{out}}
<pre>[6.0, 25.5, 40.0, 42.5, 49.0]
Line 995:
=={{header|Haskell}}==
{{trans|Python}}
<
fivenum :: [Double] -> [Double]
Line 1,040:
0.75775634,
0.32566578
]</
{{out}}
<pre>[-1.95059594,-0.676741205,0.23324706,0.746070945,1.73131507]</pre>
Line 1,046:
=={{header|J}}==
'''Solution'''
<
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@]) NB. quartiles of y
fivenum=: <./ , quartiles , >./ NB. fivenum summary of y</
'''Example Usage'''
<
test2=: 36 40 7 39 41 15
test3=: , 0 ". ];._2 noun define
Line 1,061:
6 25.5 40 42.5 49
7 15 37.5 40 41
_1.9506 _0.676741 0.233247 0.746071 1.73132</
=={{header|Java}}==
{{trans|Kotlin}}
<
public class Fivenum {
Line 1,106:
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}</
{{out}}
Line 1,118:
=={{header|JavaScript}}==
<
function median(arr) {
let mid = Math.floor(arr.length / 2);
Line 1,148:
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 ]
Line 1,158:
{{works with|Julia|0.6}}
<
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
Line 1,185:
0.75775634, 0.32566578])
println("# ", v, "\n -> ", fivenum(v))
end</
{{out}}
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.
<
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
Line 1,234:
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}</
{{out}}
Line 1,246:
=={{header|Lua}}==
<
local copy = {}
Line 1,297:
for i,x in ipairs(x1) do
print(fivenum(x))
end</
{{out}}
<pre>6 25.5 40 43 49
Line 1,305:
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
</syntaxhighlight>
Line 1,330:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]</
{{out}}
<pre>{-3.70325, -0.686977, -0.0087185, 0.652979, 3.67416}</pre>
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM LongStr IMPORT RealToStr;
Line 1,441:
ReadChar
END Fivenum.</
{{out}}
<pre>[6.000000000000000, 25.499999999999900, 40.000000000000000, 42.499999999999900, 49.000000000000000, ]
Line 1,451:
=={{header|Nim}}==
{{trans|Kotlin}}
<
type FiveNum = array[5, float]
Line 1,483:
echo ""
echo list
echo " → ", list.fivenum</
{{out}}
Line 1,497:
=={{header|Perl}}==
<
sub fivenum {
Line 1,528:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578);
@tukey = fivenum(\@x);
say join (',', @tukey);</
{{out}}
Line 1,536:
=={{header|Phix}}==
<!--<
<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>
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;">x3</span><span style="color: #0000FF;">))</span>
<!--</
{{out}}
<pre>
Line 1,579:
=={{header|PicoLisp}}==
<
(let N (length Lst)
(if (bit? 1 N)
Line 1,606:
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 ) ) )</
{{out}}
<pre>
Line 1,619:
'''Work with: Python 3'''
<
import math
import sys
Line 1,646:
y = fivenum(x)
print(y)</
{{out}}
Line 1,657:
(Though these 25% and 75% values do '''not''' correspond to the Fivenum Tukey quartile values specified in this task)
<
pd.DataFrame([1, 2, 3, 4, 5, 6]).describe()</
{{out}}
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:
<
pd.DataFrame([1, 2, 3, 4, 5, 6]).quantile([.0, .25, .50, .75, 1.00], interpolation='nearest')</
{{out}}
Line 1,687:
===Python: Functional – without imports===
'''Works with: Python 3'''
<
def fiveNums(xs):
def median(xs):
Line 1,719:
print(
fiveNums(xs)
)</
{{Out}}
<pre>(6, 25.5, 40, 42.5, 49)
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].
<
fivenum(x)</
'''Output'''
Line 1,740:
Racket's =quantile= functions use a different method to Tukey; so a new implementation was made.
<
(require math/private/statistics/quickselect)
Line 1,793:
-0.98983780 -1.00447874 -0.62759469 0.66206163 1.04312009 -0.10305385
0.75775634 0.32566578))
"Test against Go results x3"))</
This program passes its tests silently.
Line 1,800:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku"
my $end_22 = $end div 2 / 2;
Line 1,821:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
];
</syntaxhighlight>
{{out}}
Line 1,830:
=={{header|Relation}}==
Min, median and max are built in, quarter1 and quarter3 calculated.
<syntaxhighlight lang="relation">
program fivenum(X)
rename X^ x
Line 1,872:
insert 8
run fivenum("a")
</syntaxhighlight>
{{out}}
Line 1,884:
=={{header|REXX}}==
Programming note: this REXX program uses a unity─based array.
<
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,912:
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.*/</
{{out|output|text= when using the default input of: <tt> 15 6 42 41 7 36 49 40 39 47 43 </tt>}}
<pre>
Line 1,925:
=={{header|Ring}}==
<
rem1 = 0
rem2 = 0
Line 1,968:
next
? "[" + left(svect, len(svect) - 1) + "]"
</syntaxhighlight>
{{out}}
<pre>
Line 1,978:
=={{header|Ruby}}==
{{trans|Perl}}
<
sorted_arr = array.sort
n = array.size
Line 2,004:
tukey_array = fivenum(test_array)
p tukey_array
</syntaxhighlight>
{{out}}
<pre>
Line 2,012:
=={{header|Rust}}==
<
#[derive(Debug)]
struct FiveNum {
Line 2,085:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,109:
=={{header|SAS}}==
<
data test;
do i=1 to 10000;
Line 2,121:
proc means data=test min p25 median p75 max;
var x;
run;</
'''Output'''
Line 2,147:
=={{header|Scala}}==
===Array based solution===
<
object Fivenum extends App {
Line 2,183:
}
}</
{{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}}
<
var t = ((e>>1) / 2)
[0, t, e/2, e - t, e]
Line 2,209:
]]
nums.each { say fivenum(_).join(', ') }</
{{out}}
<pre>6, 25.5, 40, 42.5, 49
Line 2,218:
First build a dataset:
<
set seed 17760704
qui set obs 10000
gen x=rnormal()</
The '''[https://www.stata.com/help.cgi?summarize summarize]''' command produces all the required statistics, and more:
<
di r(min),r(p25),r(p50),r(p75),r(max)</
'''Output'''
Line 2,234:
It's also possible to use the '''[https://www.stata.com/help.cgi?tabstat tabstat]''' command
<
'''Output'''
Line 2,245:
Another example:
<
mat a=0.14082834\0.09748790\1.73131507\0.87636009\-1.95059594\ ///
0.73438555\-0.03035726\1.46675970\-0.74621349\-0.72588772\ ///
Line 2,251:
0.66206163\1.04312009\-0.10305385\0.75775634\0.32566578
svmat a
tabstat a1, s(mi q ma)</
'''Output'''
Line 2,262:
=={{header|VBA}}==
Uses [[Sorting_algorithms/Quicksort#VBA|Quicksort]].
{{trans|Phix}}<
Private Function median(tbl As Variant, lo As Integer, hi As Integer)
Dim l As Integer: l = hi - lo + 1
Line 2,292:
Debug.Print Join(fivenum(x2), " | ")
Debug.Print Join(fivenum(x3), " | ")
End Sub</
<pre>6 | 25,5 | 40 | 43 | 49
7 | 15 | 37,5 | 40 | 41
Line 2,299:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Imports System.Text
Line 2,367:
End Sub
End Module</
{{out}}
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000]
Line 2,376:
{{trans|Go}}
{{libheader|Wren-sort}}
<
var fivenum = Fn.new { |a|
Line 2,402:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578
]
for (x in [x1, x2, x3]) System.print(fivenum.call(x))</
{{out}}
Line 2,413:
=={{header|zkl}}==
Uses GNU GSL 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())
}</
<
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 2,427:
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578);
println(fiveNum(v));</
{{out}}
<pre>
|