Fivenum: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|F#|F sharp}}: Regularize header markup to recommended on category page)
m (syntax highlighting fixup automation)
Line 19:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F fivenum(array)
V n = array.len
V x = sorted(array)
Line 35:
1.04312009, -0.10305385, 0.75775634, 0.32566578]
 
print(fivenum(x))</langsyntaxhighlight>
 
{{out}}
Line 45:
==={{header|Direct C Translation}}===
{{trans|C}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Generic_Array_Sort;
 
Line 116:
print (Result, 9);
end Main;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 126:
</pre>
==={{header|Using Ada Enumeration}}===
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Generic_Array_Sort;
 
Line 206:
print (Result);
end Main;
</syntaxhighlight>
</lang>
{{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}}
<langsyntaxhighlight lang="algol68">BEGIN # construct an R-style fivenum function #
PR read "rows.incl.a68" PR
 
Line 253:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 263:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- Mac OS X 10.10. (Yosemite) or later.
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)}</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}}==
 
<langsyntaxhighlight lang="rebol">fivenum: function [lst][
lst: sort lst
m: (size lst)/2
Line 331:
print [fivenum l]
print ""
]</langsyntaxhighlight>
 
{{out}}
Line 346:
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 417:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 430:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 493:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000]
Line 501:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <ostream>
Line 579:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>(6, 25.5, 40, 43, 49)
Line 587:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.exception;
import std.math;
Line 636:
writeln(fivenum(x));
}
}</langsyntaxhighlight>
{{out}}
<pre>[6, 25.5, 40, 43, 49]
Line 645:
{{libheader| System.Generics.Collections}}
{{Trans|Java}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Fivenum;
 
Line 714:
 
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 725:
=={{header|F Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
 
// Take from https://stackoverflow.com/a/1175123
Line 766:
Console.WriteLine("{0}", y);
 
0 // return an integer exit code</langsyntaxhighlight>
{{out}}
<pre>[(6, 25.5, 40, 42.5, 49)]
Line 773:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
Line 814:
[ fivenum . ] tri@ ;
 
MAIN: fivenum-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 824:
=={{header|Go}}==
{{trans|Perl}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 860:
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 935:
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 944:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
Line 987:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[6.0, 25.5, 40.0, 42.5, 49.0]
Line 995:
=={{header|Haskell}}==
{{trans|Python}}
<langsyntaxhighlight lang="haskell">import Data.List (sort)
 
fivenum :: [Double] -> [Double]
Line 1,040:
0.75775634,
0.32566578
]</langsyntaxhighlight>
{{out}}
<pre>[-1.95059594,-0.676741205,0.23324706,0.746070945,1.73131507]</pre>
Line 1,046:
=={{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 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</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class Fivenum {
Line 1,106:
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,118:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="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>
</lang>
{{out}}<pre>
> Array(5) [ 6, 25.5, 40, 42.5, 49 ]
Line 1,158:
{{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 1,185:
0.75775634, 0.32566578])
println("# ", v, "\n -> ", fivenum(v))
end</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="scala">// version 1.2.21
 
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
Line 1,234:
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}</langsyntaxhighlight>
 
{{out}}
Line 1,246:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function slice(tbl, low, high)
local copy = {}
 
Line 1,297:
for i,x in ipairs(x1) do
print(fivenum(x))
end</langsyntaxhighlight>
{{out}}
<pre>6 25.5 40 43 49
Line 1,305:
=={{header|MATLAB}} / {{header|Octave}}==
 
<syntaxhighlight lang="matlab">
<lang Matlab>
function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
</syntaxhighlight>
</lang>
 
 
Line 1,330:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]</langsyntaxhighlight>
{{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,441:
 
ReadChar
END Fivenum.</langsyntaxhighlight>
{{out}}
<pre>[6.000000000000000, 25.499999999999900, 40.000000000000000, 42.499999999999900, 49.000000000000000, ]
Line 1,451:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import algorithm
 
type FiveNum = array[5, float]
Line 1,483:
echo ""
echo list
echo " → ", list.fivenum</langsyntaxhighlight>
 
{{out}}
Line 1,497:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">use POSIX qw(ceil floor);
 
sub fivenum {
Line 1,528:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578);
@tukey = fivenum(\@x);
say join (',', @tukey);</langsyntaxhighlight>
 
{{out}}
Line 1,536:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,579:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de median (Lst)
(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 ) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 1,619:
 
'''Work with: Python 3'''
<langsyntaxhighlight lang="python">from __future__ import division
import math
import sys
Line 1,646:
 
y = fivenum(x)
print(y)</langsyntaxhighlight>
 
{{out}}
Line 1,657:
 
(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,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:
<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,687:
===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,719:
print(
fiveNums(xs)
)</langsyntaxhighlight>
{{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].
 
<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,740:
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,793:
-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,800:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>sub fourths ( Int $end ) {
my $end_22 = $end div 2 / 2;
 
Line 1,821:
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
];
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,830:
=={{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,872:
insert 8
run fivenum("a")
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,884:
=={{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,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.*/</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,925:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
rem1 = 0
rem2 = 0
Line 1,968:
next
? "[" + left(svect, len(svect) - 1) + "]"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,978:
=={{header|Ruby}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">def fivenum(array)
sorted_arr = array.sort
n = array.size
Line 2,004:
tukey_array = fivenum(test_array)
p tukey_array
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,012:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
#[derive(Debug)]
struct FiveNum {
Line 2,085:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,109:
 
=={{header|SAS}}==
<langsyntaxhighlight lang="sas">/* build a dataset */
data test;
do i=1 to 10000;
Line 2,121:
proc means data=test min p25 median p75 max;
var x;
run;</langsyntaxhighlight>
 
'''Output'''
Line 2,147:
=={{header|Scala}}==
===Array based solution===
<langsyntaxhighlight Scalalang="scala">import java.util
 
object Fivenum extends App {
Line 2,183:
}
 
}</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 2,209:
]]
 
nums.each { say fivenum(_).join(', ') }</langsyntaxhighlight>
{{out}}
<pre>6, 25.5, 40, 42.5, 49
Line 2,218:
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 2,234:
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 2,245:
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 2,251:
0.66206163\1.04312009\-0.10305385\0.75775634\0.32566578
svmat a
tabstat a1, s(mi q ma)</langsyntaxhighlight>
 
'''Output'''
Line 2,262:
=={{header|VBA}}==
Uses [[Sorting_algorithms/Quicksort#VBA|Quicksort]].
{{trans|Phix}}<langsyntaxhighlight 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
Line 2,292:
Debug.Print Join(fivenum(x2), " | ")
Debug.Print Join(fivenum(x3), " | ")
End Sub</langsyntaxhighlight>{{out}}
<pre>6 | 25,5 | 40 | 43 | 49
7 | 15 | 37,5 | 40 | 41
Line 2,299:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
Imports System.Text
 
Line 2,367:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>[6.00000000, 25.50000000, 40.00000000, 42.50000000, 49.00000000]
Line 2,376:
{{trans|Go}}
{{libheader|Wren-sort}}
<langsyntaxhighlight lang="ecmascript">import "/sort" for 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))</langsyntaxhighlight>
 
{{out}}
Line 2,413:
=={{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 2,427:
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578);
println(fiveNum(v));</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits