September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
20
Task/Topswops/Jq/topswops-1.jq
Normal file
20
Task/Topswops/Jq/topswops-1.jq
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# "while" as defined here is included in recent versions (>1.4) of jq:
|
||||
def until(cond; next):
|
||||
def _until:
|
||||
if cond then . else (next|_until) end;
|
||||
_until;
|
||||
|
||||
# Generate a stream of permutations of [1, ... n].
|
||||
# This implementation uses arity-0 filters for speed.
|
||||
def permutations:
|
||||
# Given a single array, insert generates a stream by inserting (length+1) at different positions
|
||||
def insert: # state: [m, array]
|
||||
.[0] as $m | (1+(.[1]|length)) as $n
|
||||
| .[1]
|
||||
| if $m >= 0 then (.[0:$m] + [$n] + .[$m:]), ([$m-1, .] | insert) else empty end;
|
||||
|
||||
if .==0 then []
|
||||
elif . == 1 then [1]
|
||||
else
|
||||
. as $n | ($n-1) | permutations | [$n-1, .] | insert
|
||||
end;
|
||||
13
Task/Topswops/Jq/topswops-2.jq
Normal file
13
Task/Topswops/Jq/topswops-2.jq
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# Input: a permutation; output: an integer
|
||||
def flips:
|
||||
# state: [i, array]
|
||||
[0, .]
|
||||
| until( .[1][0] == 1;
|
||||
.[1] as $p | $p[0] as $p0
|
||||
| [.[0] + 1, ($p[:$p0] | reverse) + $p[$p0:] ] )
|
||||
| .[0];
|
||||
|
||||
# input: n, the number of items
|
||||
def fannkuch:
|
||||
reduce permutations as $p
|
||||
(0; [., ($p|flips) ] | max);
|
||||
1
Task/Topswops/Jq/topswops-3.jq
Normal file
1
Task/Topswops/Jq/topswops-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
range(1; 11) | [., fannkuch ]
|
||||
11
Task/Topswops/Jq/topswops-4.jq
Normal file
11
Task/Topswops/Jq/topswops-4.jq
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
$ jq -n -c -f topswops.jq
|
||||
[1,0]
|
||||
[2,1]
|
||||
[3,2]
|
||||
[4,4]
|
||||
[5,7]
|
||||
[6,10]
|
||||
[7,16]
|
||||
[8,22]
|
||||
[9,30]
|
||||
[10,38]
|
||||
Loading…
Add table
Add a link
Reference in a new issue