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,20 @@
# Generate a stream of the distinct combinations of r items taken from the input array.
def combination(r):
if r > length or r < 0 then empty
elif r == length then .
else ( [.[0]] + (.[1:]|combination(r-1))),
( .[1:]|combination(r))
end;
# Input: a mask, that is, an array of lengths.
# Output: a stream of the distinct partitions defined by the mask.
def partition:
# partition an array of entities, s, according to a mask presented as input:
def p(s):
if length == 0 then []
else . as $mask
| (s | combination($mask[0])) as $c
| [$c] + ($mask[1:] | p(s - $c))
end;
. as $mask | p( [range(1; 1 + ($mask|add))] );

View file

@ -0,0 +1,3 @@
([],[0,0,0],[1,1,1],[2,0,2])
| . as $test_case
| "partitions \($test_case):" , ($test_case | partition), ""

View file

@ -0,0 +1,23 @@
$ jq -M -n -c -r -f Ordered_partitions.jq
partitions []:
[]
partitions [0,0,0]:
[[],[],[]]
partitions [1,1,1]:
[[1],[2],[3]]
[[1],[3],[2]]
[[2],[1],[3]]
[[2],[3],[1]]
[[3],[1],[2]]
[[3],[2],[1]]
partitions [2,0,2]:
[[1,2],[],[3,4]]
[[1,3],[],[2,4]]
[[1,4],[],[2,3]]
[[2,3],[],[1,4]]
[[2,4],[],[1,3]]
[[3,4],[],[1,2]]