September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,5 @@
# In case your jq does not have "until" defined:
def until(cond; next):
def _until:
if cond then . else (next|_until) end;
_until;

View file

@ -0,0 +1,23 @@
def cocktailSort:
def swap(i;j): .[i] as $t | .[i] = .[j] | .[j] = $t;
def shake(stream):
reduce stream as $i
(.[0]=false;
.[1] as $A
| if $A[ $i ] > $A[ $i+1 ] then
[true, ($A|swap( $i; $i+1 ))]
else .
end);
(length - 2) as $lm2
# state: [swapped, A]
| [true, .]
| until( .[0]|not;
shake(range(0; $lm2 + 1))
| if .[0] then
# for each i in length( A ) - 2 down to 0
shake( $lm2 - range(0; $lm2 + 1))
else .
end )
| .[1];

View file

@ -0,0 +1,13 @@
def verify: if cocktailSort == sort then empty else . end;
([],
[1],
[1,1],
[3, 14],
[33, 14],
[3, 14, 1, 5, 9, 2, 6, 3],
[23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4],
[88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1],
[1.5, -1.5],
["cocktail", ["sort"], null, {}]
) | verify

View file

@ -0,0 +1,2 @@
$ jq -n -c -f cocktail_sort.jq
$