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,32 @@
proc mergeList[T](a, b: var seq[T]): seq[T] =
result = @[]
while a.len > 0 and b.len > 0:
if a[0] < b[0]:
result.add a[0]
a.delete 0
else:
result.add b[0]
b.delete 0
result.add a
result.add b
proc strand[T](a: var seq[T]): seq[T] =
var i = 0
result = @[a[0]]
a.delete 0
while i < a.len:
if a[i] > result[result.high]:
result.add a[i]
a.delete i
else:
inc i
proc strandSort[T](a: seq[T]): seq[T] =
var a = a
result = a.strand
while a.len > 0:
var s = a.strand
result = mergeList(result, s)
var a = @[1, 6, 3, 2, 1, 7, 5, 3]
echo a.strandSort

View file

@ -0,0 +1,34 @@
function merge(sequence left, sequence right)
sequence result = {}
while length(left)>0 and length(right)>0 do
if left[$]<=right[1] then
exit
elsif right[$]<=left[1] then
return result & right & left
elsif left[1]<right[1] then
result = append(result,left[1])
left = left[2..$]
else
result = append(result,right[1])
right = right[2..$]
end if
end while
return result & left & right
end function
function strand_sort(sequence s)
integer j
sequence result = {}
while length(s)>0 do
j = length(s)
for i=1 to length(s)-1 do
if s[i]>s[i+1] then
j = i
exit
end if
end for
result = merge(result,s[1..j])
s = s[j+1..$]
end while
return result
end function

View file

@ -0,0 +1,36 @@
func merge(x, y) {
var out = [];
while (x && y) {
given (x[-1] <=> y[-1]) {
when ( 1) { out.prepend(x.pop) }
when (-1) { out.prepend(y.pop) }
default { out.prepend(x.pop, y.pop) }
}
}
x + y + out;
}
func strand(x) {
x || return [];
var out = [x.shift];
if (x.len) {
for i in (-x.len .. -1) {
if (x[i] >= out[-1]) {
out.append(x.pop_at(i));
}
}
}
return out;
}
func strand_sort(x) {
var out = [];
while (var strd = strand(x)) {
out = merge(out, strd);
}
return out;
}
var a = 10.of { 100.irand };
say "Before: #{a}";
say "After: #{strand_sort(a)}";

View file

@ -0,0 +1,38 @@
# merge input array with array x by comparing the heads of the arrays
# in turn; # if both arrays are sorted, the result will be sorted:
def merge(x):
length as $length
| (x|length) as $xl
| if $length == 0 then x
elif $xl == 0 then .
else
. as $in
| reduce range(0; $xl + $length) as $z
# state [ix, xix, ans]
( [0, 0, []];
if .[0] < $length and
((.[1] < $xl and $in[.[0]] <= x[.[1]]) or .[1] == $xl)
then [(.[0] + 1), .[1], (.[2] + [$in[.[0]]]) ]
else [.[0], (.[1] + 1), (.[2] + [x[.[1]]]) ]
end
) | .[2]
end ;
def strand_sort:
# The inner function emits [strand, remainder]
def strand:
if length <= 1 then .
else
reduce .[] as $x
# state: [strand, remainder]
([ [], [] ];
if ((.[0]|length) == 0) or .[0][-1] <= $x
then [ (.[0] + [$x]), .[1] ]
else [ .[0], (.[1] + [$x]) ]
end )
end ;
if length <= 1 then .
else strand as $s
| ($s[0] | merge( $s[1] | strand_sort))
end ;