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,8 @@
10 DIM A(10): N=9
11 REM GENERATE SOME RANDOM NUMBERS AND PRINT THEM
12 FOR I=0 TO N: A(I)=INT(RND(1)*10)+1: NEXT: GOSUB 50
20 FOR J=1 TO N:KEY=A(J): I=J-1: GOSUB 30: A(I+1)=KEY: NEXT: GOSUB 50: END
30 IFI=-1 THEN RETURN
31 IFA(I)>KEY THEN A(I+1)=A(I):I=I-1: GOTO 30
32 RETURN
50 PRINT: FOR I=0 TO N: PRINTA(I): NEXT: RETURN

View file

@ -0,0 +1,33 @@
PROGRAM INSERTION_SORT
DIM A[9]
PROCEDURE INSERTION_SORT(A[])
LOCAL I,J
FOR I=0 TO UBOUND(A,1) DO
V=A[I]
J=I-1
WHILE J>=0 DO
IF A[J]>V THEN
A[J+1]=A[J]
J=J-1
ELSE
EXIT
END IF
END WHILE
A[J+1]=V
END FOR
END PROCEDURE
BEGIN
A[]=(4,65,2,-31,0,99,2,83,782,1)
FOR I%=0 TO UBOUND(A,1) DO
PRINT(A[I%];)
END FOR
PRINT
INSERTION_SORT(A[])
FOR I%=0 TO UBOUND(A,1) DO
PRINT(A[I%];)
END FOR
PRINT
END PROGRAM

View file

@ -0,0 +1,50 @@
' version 20-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
Sub insertionSort( arr() As Long )
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As Long lb = LBound(arr)
Dim As Long i, j, value
For i = lb +1 To UBound(arr)
value = arr(i)
j = i -1
While j >= lb And arr(j) > value
arr(j +1) = arr(j)
j = j -1
Wend
arr(j +1) = value
Next
End Sub
' ------=< MAIN >=------
Dim As Long i, array(-7 To 7)
Dim As Long a = LBound(array), b = UBound(array)
Randomize Timer
For i = a To b : array(i) = i : Next
For i = a To b ' little shuffle
Swap array(i), array(Int(Rnd * (b - a +1)) + a)
Next
Print "unsort ";
For i = a To b : Print Using "####"; array(i); : Next : Print
insertionSort(array()) ' sort the array
Print " sort ";
For i = a To b : Print Using "####"; array(i); : Next : Print
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,12 @@
proc insertSort[T](a: var openarray[T]) =
for i in 1 .. <a.len:
let value = a[i]
var j = i
while j > 0 and value < a[j-1]:
a[j] = a[j-1]
dec j
a[j] = value
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
insertSort a
echo a

View file

@ -0,0 +1,14 @@
: insertionSort(a)
| l i j v |
a asListBuffer ->l
2 l size for: i [
l at(i) ->v
i 1- ->j
while(j) [
l at(j) dup v <= ifTrue: [ drop break ]
j 1+ swap l put
j 1- ->j
]
l put(j 1 +, v)
]
l ;

View file

@ -0,0 +1,19 @@
function insertion_sort(sequence s)
object temp
integer j
for i=2 to length(s) do
temp = s[i]
j = i-1
while j>=1 and s[j]>temp do
s[j+1] = s[j]
j -= 1
end while
s[j+1] = temp
end for
return s
end function
constant s = {4, 15, "delta", 2, -31, 0, "alpha", 19, "gamma", 2, 13, "beta", 782, 1}
puts(1,"Before: ") ?s
puts(1,"After: ") ?insertion_sort(s)

View file

@ -0,0 +1,14 @@
alist = [7,6,5,9,8,4,3,1,2,0]
see insertionsort(alist)
func insertionsort blist
for i = 1 to len(blist)
value = blist[i]
j = i - 1
while j >= 1 and blist[j] > value
blist[j+1] = blist[j]
j = j - 1
end
blist[j+1] = value
next
return blist

View file

@ -0,0 +1,17 @@
class Array {
method insertion_sort {
{ |i|
var j = i;
var k = self[i];
while ((j > 0) && (k < self[j - 1])) {
self[j] = self[j - 1];
j--;
};
self[j] = k;
} * self.end;
return self;
}
}
var a = 10.of {100.rand.int};
say a.insertion_sort;

View file

@ -0,0 +1,10 @@
func insertionSort<T:Comparable>(inout list:[T]) {
for i in 1..<list.count {
var j = i
while j > 0 && list[j - 1] > list[j] {
swap(&list[j], &list[j - 1])
j--
}
}
}

View file

@ -0,0 +1,2 @@
def insertion_sort:
reduce .[] as $x ([]; insert($x));

View file

@ -0,0 +1,4 @@
# As soon as "condition" is true, then emit . and stop:
def do_until(condition; next):
def u: if condition then . else (next|u) end;
u;

View file

@ -0,0 +1,38 @@
def bsearch(target):
if length == 0 then -1
elif length == 1 then
if target == .[0] then 0 elif target < .[0] then -1 else -2 end
else . as $in
# state variable: [start, end, answer]
# where start and end are the upper and lower offsets to use.
| [0, length-1, null]
| do_until( .[0] > .[1] ;
(if .[2] != null then (.[1] = -1) # i.e. break
else
( ( (.[1] + .[0]) / 2 ) | floor ) as $mid
| $in[$mid] as $monkey
| if $monkey == target then (.[2] = $mid) # success
elif .[0] == .[1] then (.[1] = -1) # failure
elif $monkey < target then (.[0] = ($mid + 1))
else (.[1] = ($mid - 1))
end
end ))
| if .[2] == null then # compute the insertion point
if $in[ .[0] ] < target then (-2 -.[0])
else (-1 -.[0])
end
else .[2]
end
end;
# insert x assuming input is sorted
def insert(x):
if length == 0 then [x]
else
bsearch(x) as $i
| ( if $i < 0 then -(1+$i) else $i end ) as $i
| .[0:$i] + [x] + .[$i:]
end ;
def insertion_sort:
reduce .[] as $x ([]; insert($x));

View file

@ -0,0 +1 @@
[1, 2, 1, 1.1, -1.1, null, [null], {"null":null}] | insertion_sort