Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
30
Task/Quickselect-algorithm/jq/quickselect-algorithm-1.jq
Normal file
30
Task/Quickselect-algorithm/jq/quickselect-algorithm-1.jq
Normal 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;
|
||||
3
Task/Quickselect-algorithm/jq/quickselect-algorithm-2.jq
Normal file
3
Task/Quickselect-algorithm/jq/quickselect-algorithm-2.jq
Normal 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) => \(.)"
|
||||
12
Task/Quickselect-algorithm/jq/quickselect-algorithm-3.jq
Normal file
12
Task/Quickselect-algorithm/jq/quickselect-algorithm-3.jq
Normal 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
|
||||
$
|
||||
Loading…
Add table
Add a link
Reference in a new issue