Perl 5 + -a -M5.10.0
, 43 bytes
Outputs all valid solutions. Outputs nothing if there's no solution.
$n=<>;$"="{-,+}";eval==$n&&say/\D/g for<@F>
Explanation
Initial list of numbers is implicitly stored in @F
thanks to -a
, target value is then stored in $n
and the field separator ($"
) is set to "{-,+}"
. For each value in the glob
<@F>
(when interpolating a list into a string, the field separator is used which expands 3 2 4 1
into 3{-,+}2{-,+}4{-,+}1
which as a glob
returns all permutations interpolating -
and +
. The string is then eval
uated as code (eval
works on $_
by default) and if it matches $n
is output (say
- which prints $_
by default).