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,58 @@
' version 21-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
Sub cocktailsort(bs() 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(bs)
Dim As Long ub = UBound(bs) -1
Dim As Long done, i
Do
done = 0 ' going up
For i = lb To ub
If bs(i) > bs(i +1) Then
Swap bs(i), bs(i +1)
done = 1
End If
Next
ub = ub -1
If done = 0 Then Exit Do ' 0 means the array is sorted
done = 0 ' going down
For i = ub To lb Step -1
If bs(i) > bs(i +1) Then
Swap bs(i), bs(i +1)
done = 1
End If
Next
lb = lb +1
Loop Until done = 0 ' 0 means the array is sorted
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 "unsorted ";
For i = a To b : Print Using "####"; array(i); : Next : Print
cocktailsort(array()) ' sort the array
Print " sorted ";
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,17 @@
template trySwap(): stmt {.immediate.} =
if a[i] < a[i-1]:
swap a[i], a[i-1]
t = false
proc cocktailSort[T](a: var openarray[T]) =
var t = false
var l = a.len
while not t:
t = true
for i in 1 .. <l: trySwap
if t: break
for i in countdown(l-1, 1): trySwap
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
cocktailSort a
echo a

View file

@ -0,0 +1,20 @@
function cocktail_sort(sequence s)
integer swapped = 1, f = 1, t = length(s)-1, d = 1
while swapped do
swapped = 0
for i=f to t by d do
if s[i]>s[i+1] then
{s[i],s[i+1],swapped} = {s[i+1],s[i],1}
end if
end for
-- swap to and from, and flip direction.
-- additionally, we can reduce one element to be
-- examined, depending on which way we just went.
{f,t,d} = {t-(d=1),f-(d=-1),-d}
end while
return s
end function
constant s = sq_rand(repeat(1000,10))
? s
? cocktail_sort(s)

View file

@ -0,0 +1,20 @@
aList = [ 5, 6, 1, 2, 9, 14, 2, 15, 6, 7, 8, 97]
flag = 0
cocktailSort(aList)
for i=1 to len(aList)
see "" + aList[i] + " "
next
func cocktailSort A
n = len(A)
while flag = 0
flag = 1
for i = 1 to n-1
if A[i] > A[i+1]
temp = A[i]
A[i] = A[i+1]
A [i+1] = temp
flag = 0
ok
next
end

View file

@ -0,0 +1,16 @@
func cocktailsort(a) {
var swapped = false;
func cmpsw(i) {
if (a[i] > a[i+1]) {
a[i, i+1] = a[i+1, i];
swapped = true;
}
}
var max = a.end;
do {
{ |i| cmpsw(i-1) } * max;
swapped.not! && break;
{ |i| cmpsw(max-i) } * max;
} while (swapped);
return a;
}

View file

@ -0,0 +1,5 @@
var numbers = [7,6,5,9,8,4,3,1,2,0];
say cocktailsort(numbers).dump;
var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"];
say cocktailsort(strs).dump;

View file

@ -0,0 +1,5 @@
# In case your jq does not have "until" defined:
def until(cond; next):
def _until:
if cond then . else (next|_until) end;
_until;

View file

@ -0,0 +1,23 @@
def cocktailSort:
def swap(i;j): .[i] as $t | .[i] = .[j] | .[j] = $t;
def shake(stream):
reduce stream as $i
(.[0]=false;
.[1] as $A
| if $A[ $i ] > $A[ $i+1 ] then
[true, ($A|swap( $i; $i+1 ))]
else .
end);
(length - 2) as $lm2
# state: [swapped, A]
| [true, .]
| until( .[0]|not;
shake(range(0; $lm2 + 1))
| if .[0] then
# for each i in length( A ) - 2 down to 0
shake( $lm2 - range(0; $lm2 + 1))
else .
end )
| .[1];

View file

@ -0,0 +1,13 @@
def verify: if cocktailSort == sort then empty else . end;
([],
[1],
[1,1],
[3, 14],
[33, 14],
[3, 14, 1, 5, 9, 2, 6, 3],
[23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4],
[88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1],
[1.5, -1.5],
["cocktail", ["sort"], null, {}]
) | verify

View file

@ -0,0 +1,2 @@
$ jq -n -c -f cocktail_sort.jq
$