Subset sum problem: Difference between revisions

→‎{{header|jq}}: def selections:
(→‎{{header|jq}}: def selections:)
Line 1,197:
'''Works with gojq, the Go implementation of jq''' (provided `keys_unsorted` is replaced by `keys`)
<lang jq>
# Input: an array of n arrays
# Output: a stream of arrays of length n
def combinations:
if length == 0 then []
else
.[0][] as $x
| (.[1:] | combinations) as $y
| [$x] + $y
end ;
 
# Input: an array of n elements, each of which is either in or out
# Output: a stream of the 2^n possible selections
def all_combinationsselections:
# map( [null, .] ) | combinations | map(select(.));
if length == 0 then []
then .
else .[0][] as $x
| .[1:] | selections | ., ([$x] + .)
end ;
 
# input: a JSON object giving the weights
Line 1,218 ⟶ 1,213:
. as $dict
| keyskeys_unsorted | all_combinationsselections | select( sum($dict; 0));
</lang>
'''An Example'''
Line 1,260 ⟶ 1,255:
<pre>
[
"flatwormarchbishop",
"infrabalm",
"lindholmbonnet",
"mincemeatcentipede",
"pluggingcobol",
"smokescreencovariate",
"speakeasydeploy",
"veinefferent",
"elysee"
]
 
</pre>
 
 
=={{header|Julia}}==
2,456

edits