Skip to main content
added 857 characters in body
Source Link
Dom Hastings
  • 24.4k
  • 4
  • 56
  • 93

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>

Try it online!

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 evaluated as code (eval works on $_ by default) and if it matches $n is output (say - which prints $_ by default).

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>

Try it online!

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>

Try it online!

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 evaluated as code (eval works on $_ by default) and if it matches $n is output (say - which prints $_ by default).

Source Link
Dom Hastings
  • 24.4k
  • 4
  • 56
  • 93

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>

Try it online!