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,51 @@
' version 21-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
Sub gnomesort(gnome() 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(gnome)
Dim As Long ub = UBound(gnome)
Dim As Long i = lb +1, j = lb +2
While i < (ub +1)
' replace "<=" with ">=" for downwards sort
If gnome(i -1) <= gnome(i) Then
i = j
j += 1
Else
Swap gnome(i -1), gnome(i)
i -= 1
If i = lb Then
i = j
j += 1
End If
End If
Wend
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
gnomesort(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,16 @@
proc gnomeSort[T](a: var openarray[T]) =
var
n = a.len
i = 1
j = 2
while i < n:
if a[i-1] > a[i]:
swap a[i-1], a[i]
dec i
if i > 0: continue
i = j
inc j
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
gnomeSort a
echo a

View file

@ -0,0 +1,19 @@
function gnomeSort(sequence s)
integer i = 1, j = 2
while i<length(s) do
if s[i]<=s[i+1] then
i = j
j += 1
else
{s[i],s[i+1]} = {s[i+1],s[i]}
i -= 1
if i = 0 then
i = j
j += 1
end if
end if
end while
return s
end function
?gnomeSort(shuffle(tagset(10)))

View file

@ -0,0 +1,21 @@
aList = [ 5, 6, 1, 2, 9, 14, 15, 7, 8, 97]
gnomeSort(aList)
for i=1 to len(aList)
see "" + aList[i] + " "
next
func gnomeSort a
i = 2
j = 3
while i < len(a)
if a[i-1] <= a[i]
i = j
j = j + 1
else
temp = a[i-1]
a[i-1] = a[i]
a[i] = temp
i = i - 1
if i = 1
i = j
j = j + 1 ok ok end

View file

@ -0,0 +1,21 @@
class Array {
method gnomesort {
var (i=1, j=2);
var len = self.len;
while (i < len) {
if (self[i-1] <= self[i]) {
(i, j) = (j, j+1);
}
else {
self[i-1, i] = self[i, i-1];
if (--i == 0) {
(i, j) = (j, j+1);
}
}
}
return self;
}
}
var ary = [7,6,5,9,8,4,3,1,2,0];
say ary.gnomesort;

View file

@ -0,0 +1,24 @@
# 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;
# input: an array
def gnomeSort:
def swap(i;j): .[i] as $x | .[i]=.[j] | .[j]=$x;
length as $length
# state: [i, j, ary]
| [1, 2, .]
| do_until( .[0] >= $length;
.[0] as $i | .[1] as $j
| .[2]
# for descending sort, use >= for comparison
| if .[$i-1] <= .[$i] then [$j, $j + 1, .]
else swap( $i-1; $i)
| ($i - 1) as $i
| if $i == 0 then [$j, $j + 1, .]
else [$i, $j, .]
end
end )
| .[2];

View file

@ -0,0 +1 @@
[(2|sqrt), [1], null, 1, 0.5, 2, 1, -3, {"a": "A"}] | gnomeSort

View file

@ -0,0 +1,16 @@
$ jq -M -n -f Gnome_sort.jq
[
null,
-3,
0.5,
1,
1,
1.4142135623730951,
2,
[
1
],
{
"a": "A"
}
]