September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
20
Task/Ordered-Partitions/Jq/ordered-partitions-1.jq
Normal file
20
Task/Ordered-Partitions/Jq/ordered-partitions-1.jq
Normal 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))] );
|
||||
3
Task/Ordered-Partitions/Jq/ordered-partitions-2.jq
Normal file
3
Task/Ordered-Partitions/Jq/ordered-partitions-2.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
([],[0,0,0],[1,1,1],[2,0,2])
|
||||
| . as $test_case
|
||||
| "partitions \($test_case):" , ($test_case | partition), ""
|
||||
23
Task/Ordered-Partitions/Jq/ordered-partitions-3.jq
Normal file
23
Task/Ordered-Partitions/Jq/ordered-partitions-3.jq
Normal 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]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue