Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1 @@
median[list]

View file

@ -0,0 +1,36 @@
PROGRAM MEDIAN
DIM X[10]
PROCEDURE QUICK_SELECT
LT=0 RT=L-1
J=LT
REPEAT
PT=X[K]
SWAP(X[K],X[RT])
P=LT
FOR I=P TO RT-1 DO
IF X[I]<PT THEN SWAP(X[I],X[P]) P=P+1 END IF
END FOR
SWAP(X[RT],X[P])
IF P=K THEN EXIT PROCEDURE END IF
IF P<K THEN LT=P+1 END IF
IF P>=K THEN RT=P-1 END IF
UNTIL J>RT
END PROCEDURE
PROCEDURE MEDIAN
K=INT(L/2)
QUICK_SELECT
R=X[K]
IF L-2*INT(L/2)<>0 THEN R=(R+X[K+1])/2 END IF
END PROCEDURE
BEGIN
PRINT(CHR$(12);) !CLS
X[0]=4.4 X[1]=2.3 X[2]=-1.7 X[3]=7.5 X[4]=6.6 X[5]=0
X[6]=1.9 X[7]=8.2 X[8]=9.3 X[9]=4.5 X[10]=-11.7
L=11
MEDIAN
PRINT(R)
END PROGRAM

View file

@ -0,0 +1,15 @@
(define (median L) ;; O(n log(n))
(set! L (vector-sort! < (list->vector L)))
(define dim (// (vector-length L) 2))
(if (integer? dim)
(// (+ [L dim] [L (1- dim)]) 2)
[L (floor dim)]))
(median '( 3 4 5))
→ 4
(median '(6 5 4 3))
→ 4.5
(median (iota 10000))
→ 4999.5
(median (iota 10001))
→ 5000

View file

@ -0,0 +1,46 @@
' FB 1.05.0 Win64
Sub quicksort(a() As Double, first As Integer, last As Integer)
Dim As Integer length = last - first + 1
If length < 2 Then Return
Dim pivot As Double = a(first + length\ 2)
Dim lft As Integer = first
Dim rgt As Integer = last
While lft <= rgt
While a(lft) < pivot
lft +=1
Wend
While a(rgt) > pivot
rgt -= 1
Wend
If lft <= rgt Then
Swap a(lft), a(rgt)
lft += 1
rgt -= 1
End If
Wend
quicksort(a(), first, rgt)
quicksort(a(), lft, last)
End Sub
Function median(a() As Double) As Double
Dim lb As Integer = LBound(a)
Dim ub As Integer = UBound(a)
Dim length As Integer = ub - lb + 1
If length = 0 Then Return 0.0/0.0 '' NaN
If length = 1 Then Return a(ub)
Dim mb As Integer = (lb + ub) \2
If length Mod 2 = 1 Then Return a(mb)
Return (a(mb) + a(mb + 1))/2.0
End Function
Dim a(0 To 9) As Double = {4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5}
quicksort(a(), 0, 9)
Print "Median for all 10 elements : "; median(a())
' now get rid of final element
Dim b(0 To 8) As Double = {4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3}
quicksort(b(), 0, 8)
Print "Median for first 9 elements : "; median(b())
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,15 @@
define median_ext(a::array) => {
#a->sort
if(#a->size % 2) => {
// odd numbered element array, pick middle
return #a->get(#a->size / 2 + 1)
else
// even number elements in array
return (#a->get(#a->size / 2) + #a->get(#a->size / 2 + 1)) / 2.0
}
}
median_ext(array(3,2,7,6)) // 4.5
median_ext(array(3,2,9,7,6)) // 6

View file

@ -0,0 +1,2 @@
put median("4.1,5.6,7.2,1.7,9.3,4.4,3.2") & "," & median("4.1,7.2,1.7,9.3,4.4,3.2")
returns 4.4, 4.25

View file

@ -0,0 +1,24 @@
function floor n
if n < 0 then
return (trunc(n) - 1)
else
return trunc(n)
end if
end floor
function median2 x
local n, m
set itemdelimiter to comma
sort items of x ascending numeric
put the number of items of x into n
put floor(n / 2) into m
if n mod 2 is 0 then
return (item m of x + item (m + 1) of x) / 2
else
return item (m + 1) of x
end if
end median2
returns the same as the built-in median, viz.
put median2("4.1,5.6,7.2,1.7,9.3,4.4,3.2") & "," & median2("4.1,7.2,1.7,9.3,4.4,3.2")
4.4,4.25

View file

@ -0,0 +1,11 @@
import algorithm, strutils
proc median(xs): float =
var ys = xs
sort(ys, system.cmp[float])
0.5 * (ys[ys.high div 2] + ys[ys.len div 2])
var a = @[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2]
echo formatFloat(median(a), precision = 0)
a = @[4.1, 7.2, 1.7, 9.3, 4.4, 3.2]
echo formatFloat(median(a), precision = 0)

View file

@ -0,0 +1,12 @@
function median(sequence s)
atom res=0
integer l = length(s), k = floor((l+1)/2)
if l then
s = sort(s)
res = s[k]
if remainder(l,2)=0 then
res = (res+s[k+1])/2
end if
end if
return res
end function

View file

@ -0,0 +1,12 @@
function medianq(sequence s)
atom res=0, tmp
integer l = length(s), k = floor((l+1)/2)
if l then
{s,res} = quick_select(s,k)
if remainder(l,2)=0 then
{s,tmp} = quick_select(s,k+1)
res = (res+tmp)/2
end if
end if
return res -- (or perhaps return {s,res})
end function

View file

@ -0,0 +1,9 @@
aList = [5,4,2,3]
see "medium : " + median(aList) + nl
func median aray
srtd = sort(aray)
alen = len(srtd)
if alen % 2 = 0
return (srtd[alen/2] + srtd[alen/2 + 1]) / 2.0
else return srtd[ceil(alen/2)] ok

View file

@ -0,0 +1,5 @@
func median(arry) {
var srtd = arry.sort;
var alen = srtd.length;
srtd[(alen-1)/2]+srtd[alen/2] / 2;
}

View file

@ -0,0 +1,14 @@
@let {
; iterative
med1 &l @let {a @sort l s #a i @/s 2 ?{%%s 2 ~/ 2 +`-i 1 a `i a `i a}}
; tacit
med2 ^(\~/2 @sum @(^(\&![#~f #~c] \~/2 \~-1 #) @` @id) @sort)
[[
!med1 [4 2 5 2 1]
!med1 [4 5 2 1]
!med2 [4 2 5 2 1]
!med2 [4 5 2 1]
]]
}

View file

@ -0,0 +1,10 @@
def median:
length as $length
| sort as $s
| if $length == 0 then null
else ($length / 2 | floor) as $l2
| if ($length % 2) == 0 then
($s[$l2 - 1] + $s[$l2]) / 2
else $s[$l2]
end
end ;

View file

@ -0,0 +1,2 @@
[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2]
[4.1, 7.2, 1.7, 9.3, 4.4, 3.2]

View file

@ -0,0 +1,3 @@
$ jq -f median.jq in.dat
4.4
4.25