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,19 @@
proc qselect[T](a: var openarray[T]; k: int, inl = 0, inr = -1): T =
var r = if inr >= 0: inr else: a.high
var st = 0
for i in 0 .. < r:
if a[i] > a[r]: continue
swap a[i], a[st]
inc st
swap a[r], a[st]
if k == st: a[st]
elif st > k: qselect(a, k, 0, st - 1)
else: qselect(a, k, st, inr)
let x = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
for i in 0..9:
var y = x
echo i, ": ", qselect(y, i)

View file

@ -0,0 +1,41 @@
global function quick_select(sequence s, integer k)
integer left = 1, right = length(s), pos
object pivotv, tmp
while left<right do
pivotv = s[k];
-- {s[k], s[right]} = {s[right], s[k]}
tmp = s[k]
s[k] = s[right]
s[right]=tmp
pos = left
for i=left to right do
if s[i]<pivotv then
-- {s[i], s[pos]} = {s[pos], s[i]}
tmp = s[i]
s[i] = s[pos]
s[pos]=tmp
pos += 1
end if
end for
-- {s[right], s[pos]} = {s[pos], s[right]}
tmp = s[right]
s[right] = s[pos]
s[pos]=tmp
if pos==k then exit end if
if pos<k then
left = pos + 1
else
right = pos - 1
end if
end while
return {s,s[k]}
end function
sequence s = {9, 8, 7, 6, 5, 0, 1, 2, 3, 4}
integer r
for i=1 to 10 do
{s,r} = quick_select(s,i)
printf(1," %d",r)
end for
{} = wait_key()

View file

@ -0,0 +1,20 @@
aList = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
see partition(aList, 9, 4, 2) + nl
func partition list, left, right, pivotIndex
pivotValue = list[pivotIndex]
temp = list[pivotIndex]
list[pivotIndex] = list[right]
list[right] = temp
storeIndex = left
for i = left to right-1
if list[i] < pivotValue
temp = list[storeIndex]
list[storeIndex] = list[i]
list[i] = temp
storeIndex++ ok
temp = list[right]
list[right] = list[storeIndex]
list[storeIndex] = temp
next
return storeIndex

View file

@ -0,0 +1,14 @@
func quickselect(a, k) {
var pivot = a.pick;
var left = a.grep{|i| i < pivot};
var right = a.grep{|i| i > pivot};
given(var l = left.len) {
when (k) { pivot }
case (k < l) { __FUNC__(left, k) }
default { __FUNC__(right, k - l - 1) }
}
}
var v = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4];
say v.range.map{|i| quickselect(v, i)};

View file

@ -0,0 +1,20 @@
func select<T where T : Comparable>(var elements: [T], n: Int) -> T {
var r = indices(elements)
while true {
let pivotIndex = partition(&elements, r)
if n == pivotIndex {
return elements[pivotIndex]
} else if n < pivotIndex {
r.endIndex = pivotIndex
} else {
r.startIndex = pivotIndex+1
}
}
}
for i in 0 ..< 10 {
let a = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
print(select(a, i))
if i < 9 { print(", ") }
}
println()

View file

@ -0,0 +1,30 @@
# Emit the k-th smallest item in the input array,
# or nothing if k is too small or too large.
# The smallest corresponds to k==1.
# The input array may hold arbitrary JSON entities, including null.
def quickselect(k):
def partition(pivot):
reduce .[] as $x
# state: [less, other]
( [ [], [] ]; # two empty arrays:
if $x < pivot
then .[0] += [$x] # add x to less
else .[1] += [$x] # add x to other
end
);
# recursive inner function has arity 0 for efficiency
def qs: # state: [kn, array] where kn counts from 0
.[0] as $kn
| .[1] as $a
| $a[0] as $pivot
| ($a[1:] | partition($pivot)) as $p
| $p[0] as $left
| ($left|length) as $ll
| if $kn == $ll then $pivot
elif $kn < $ll then [$kn, $left] | qs
else [$kn - $ll - 1, $p[1] ] | qs
end;
if length < k or k <= 0 then empty else [k-1, .] | qs end;

View file

@ -0,0 +1,3 @@
(0, 12, range(1;11)) as $k
| [9, 8, 7, 6, 5, 0, 1, 2, 3, 4] | quickselect($k)
| "k=\($k) => \(.)"

View file

@ -0,0 +1,12 @@
$ jq -n -r -f quickselect.jq
k=1 => 0
k=2 => 1
k=3 => 2
k=4 => 3
k=5 => 4
k=6 => 5
k=7 => 6
k=8 => 7
k=9 => 8
k=10 => 9
$