Fivenum: Difference between revisions

3,408 bytes added ,  6 years ago
Go solution
m (Python before R)
(Go solution)
Line 96:
 
[-1.950595940, -0.676741205, 0.233247060, 0.746070945, 1.731315070]
</pre>
 
=={{header|Go}}==
{{trans|Perl}}
<lang go>package main
 
import (
"fmt"
"math"
"sort"
)
 
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
 
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
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,
}
)
 
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}</lang>
{{out}}
<pre>
[7 15 37.5 40 41]
[6 25.5 40 42.5 49]
[-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507]
</pre>
 
'''Alternate:'''
 
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.
<lang go>package main
 
import (
"fmt"
"math/rand"
)
 
func fivenum(a []float64) (n [5]float64) {
last := len(a) - 1
m := last / 2
n[2] = qsel(a, m)
q1 := len(a) / 4
n[1] = qsel(a[:m], q1)
n[0] = qsel(a[:q1], 0)
a = a[m:]
q3 := last - m - q1
n[3] = qsel(a, q3)
a = a[q3:]
n[4] = qsel(a, len(a)-1)
return
}
 
func qsel(a []float64, k int) float64 {
for len(a) > 1 {
px := rand.Intn(len(a))
pv := a[px]
last := len(a) - 1
a[px], a[last] = a[last], pv
px = 0
for i, v := range a[:last] {
if v < pv {
a[px], a[i] = v, a[px]
px++
}
}
a[px], a[last] = pv, a[px]
if px == k {
return pv
}
if k < px {
a = a[:px]
} else {
a = a[px+1:]
k -= px + 1
}
}
return a[0]
}
 
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
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,
}
)
 
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}</lang>
{{out}}
<pre>
[7 15 36 40 41]
[6 15 40 43 49]
[-1.95059594 -0.62759469 0.14082834 0.73438555 1.73131507]
</pre>
 
1,707

edits