June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,12 @@
DECLARE a[] = { 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 } TYPE FLOATING
DECLARE b[] = { 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 } TYPE FLOATING
DEF FN Dim(x) = SIZEOF(x) / SIZEOF(double)
DEF FN Median(x) = IIF(ODD(Dim(x)), x[(Dim(x)-1)/2], (x[Dim(x)/2-1]+x[Dim(x)/2])/2 )
SORT a
PRINT "Median of a: ", Median(a)
SORT b
PRINT "Median of b: ", Median(b)

View file

@ -20,7 +20,7 @@ extension op
]
}
program =
public program =
[
var a1 := (4.1r, 5.6r, 7.2r, 1.7r, 9.3r, 4.4r, 3.2r).
var a2 := (4.1r, 7.2r, 1.7r, 9.3r, 4.4r, 3.2r).

View file

@ -0,0 +1,31 @@
package main
import "fmt"
func main() {
fmt.Println(median([]float64{3, 1, 4, 1})) // prints 2
fmt.Println(median([]float64{3, 1, 4, 1, 5})) // prints 3
}
func median(a []float64) float64 {
half := len(a) / 2
med := sel(a, half)
if len(a)%2 == 0 {
return (med + a[half-1]) / 2
}
return med
}
func sel(list []float64, k int) float64 {
for i, minValue := range list[:k+1] {
minIndex := i
for j := i + 1; j < len(list); j++ {
if list[j] < minValue {
minIndex = j
minValue = list[j]
list[i], list[minIndex] = minValue, list[i]
}
}
}
return list[k]
}

View file

@ -0,0 +1,52 @@
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println(median([]float64{3, 1, 4, 1})) // prints 2
fmt.Println(median([]float64{3, 1, 4, 1, 5})) // prints 3
}
func median(list []float64) float64 {
half := len(list) / 2
med := qsel(list, half)
if len(list)%2 == 0 {
return (med + qsel(list, half-1)) / 2
}
return med
}
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++
}
}
if px == k {
return pv
}
if k < px {
a = a[:px]
} else {
// swap elements. simply assigning a[last] would be enough to
// allow qsel to return the correct result but it would leave slice
// "a" unusable for subsequent use. we want this full swap so that
// we can make two successive qsel calls in the case of median
// of an even number of elements.
a[px], a[last] = pv, a[px]
a = a[px+1:]
k -= px + 1
}
}
return a[0]
}

View file

@ -0,0 +1,9 @@
on median (numlist)
-- numlist = numlist.duplicate() -- if input list should not be altered
numlist.sort()
if numlist.count mod 2 then
return numlist[numlist.count/2+1]
else
return (numlist[numlist.count/2]+numlist[numlist.count/2+1])/2.0
end if
end

View file

@ -1,25 +1,24 @@
/*REXX program finds the median of a vector (and displays the vector and median).*/
/* ══════════vector════════════ ══show vector═══ ════════show result═══════════ */
v= '1 9 2 4 '; say 'vector:' v; say 'median' median(v); say
v= '3 1 4 1 5 9 7 6 '; say 'vector:' v; say 'median' median(v); say
v= 1 9 2 4 ; say 'vector:' v; say 'median' median(v); say
v= 3 1 4 1 5 9 7 6 ; say 'vector:' v; say 'median' median(v); say
v= '3 4 1 -8.4 7.2 4 1 1.2'; say 'vector:' v; say 'median' median(v); say
v= '-1.2345678e99 2.3e700'; say 'vector:' v; say 'median' median(v); say
v= -1.2345678e99 2.3e700 ; say 'vector:' v; say 'median' median(v); say
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
eSORT: procedure expose @. #; parse arg $; #=words($) /*$: is the vector. */
do g=1 for #; @.g=word($,g); end /*g*/ /*convert list──►array*/
eSORT: procedure expose @. #; parse arg $; #=words($) /*$: is the vector. */
do g=1 for #; @.g=word($, g); end /*g*/ /*convert list──►array*/
h=# /*#: number elements.*/
do while h>1; h=h % 2 /*cut entries by half.*/
do i=1 for #-h; j=i; k=h+i /*sort lower section. */
do i=1 for #-h; j=i; k=h + i /*sort lower section. */
do while @.k<@.j; parse value @.j @.k with @.k @.j /*swap.*/
if h>=j then leave; j=j-h; k=k-h /*diminish J and K.*/
if h>=j then leave; j=j - h; k=k - h /*diminish J and K.*/
end /*while @.k<@.j*/
end /*i*/
end /*while h>l*/ /*end of exchange sort*/
end /*while h>1*/ /*end of exchange sort*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
median: procedure; call eSORT arg(1) /*obtain the elements of the vector.*/
m=# % 2 /* % is REXX's integer division.*/
n=m+1 /*N: the next element after M. */
if #//2 then return @.n /*(odd?) // is REXX's ÷ remainder.*/
return (@.m + @.n) / 2 /*process an even─element vector. */
median: procedure; call eSORT arg(1); m=# % 2 /* % is REXX's integer division.*/
n=m + 1 /*N: the next element after M. */
if # // 2 then return @.n /*[odd?] // ◄───REXX's ÷ remainder*/
return (@.m + @.n) / 2 /*process an even─element vector. */