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,52 @@
function radixSortn(sequence s, integer n)
sequence buckets = repeat({},10)
sequence res = {}
for i=1 to length(s) do
integer digit = remainder(floor(s[i]/power(10,n-1)),10)+1
buckets[digit] = append(buckets[digit],s[i])
end for
for i=1 to length(buckets) do
integer len = length(buckets[i])
if len!=0 then
if len=1 or n=1 then
res &= buckets[i]
else
res &= radixSortn(buckets[i],n-1)
end if
end if
end for
return res
end function
function split_by_sign(sequence s)
sequence buckets = {{},{}}
for i=1 to length(s) do
integer si = s[i]
if si<0 then
buckets[1] = append(buckets[1],-si)
else
buckets[2] = append(buckets[2],si)
end if
end for
return buckets
end function
function radixSort(sequence s)
integer mins = min(s)
integer passes = max(max(s),abs(mins))
passes = floor(log10(passes))+1
if mins<0 then
sequence buckets = split_by_sign(s)
buckets[1] = reverse(sq_uminus(radixSortn(buckets[1],passes)))
buckets[2] = radixSortn(buckets[2],passes)
s = buckets[1]&buckets[2]
else
s = radixSortn(s,passes)
end if
return s
end function
?radixSort({1, 3, 8, 9, 0, 0, 8, 7, 1, 6})
?radixSort({170, 45, 75, 90, 2, 24, 802, 66})
?radixSort({170, 45, 75, 90, 2, 24, -802, -66})
?radixSort({100000, -10000, 400, 23, 10000})

View file

@ -0,0 +1,26 @@
class Array {
method radix_sort(base=10) {
var arr = self.clone;
var rounds = ([arr.minmax].map{.abs}.max -> log(base).floor + 1);
for i in ^rounds {
var buckets = (2*base -> of {[]});
var base_i = base**i;
for n in arr {
var digit = (n/base_i % base);
digit += base if (0 <= n);
buckets[digit].append(n);
}
arr = buckets.flatten;
}
return arr;
}
}
for arr in [
[1, 3, 8, 9, 0, 0, 8, 7, 1, 6],
[170, 45, 75, 90, 2, 24, 802, 66],
[170, 45, 75, 90, 2, 24, -802, -66],
[100000, -10000, 400, 23, 10000],
] {
say arr.radix_sort;
}

View file

@ -0,0 +1,23 @@
# Sort the input array;
# "base" must be an integer greater than 1
def radix_sort(base):
# We only need the ceiling of non-negatives:
def ceil: if . == floor then . else (. + 1 | floor) end;
min as $min
| map(. - $min)
| ((( max|log) / (base|log)) | ceil) as $rounds
| reduce range(0; $rounds) as $i
# state: [ base^i, buckets ]
( [1, .];
.[0] as $base_i
| reduce .[1][] as $n
([];
(($n/$base_i) % base) as $digit
| .[$digit] += [$n] )
| [($base_i * base), (map(select(. != null)) | flatten)] )
| .[1]
| map(. + $min) ;
def radix_sort:
radix_sort(10);

View file

@ -0,0 +1,5 @@
# Verify that radix_sort agrees with sort
( [1, 3, 8, 9, 0, 0, 8, 7, 1, 6],
[170, 45, 75, 90, 2, 24, 802, 66],
[170, 45, 75, 90, 2, 24, -802, -66] )
| (radix_sort == sort)