| Bytes | Lang | Time | Link |
|---|---|---|---|
| 071 | Ruby | 250522T061032Z | Value In |
| 1290 | TSQL | 240508T032328Z | CrSb0001 |
| 059 | Raku Perl 6 rakudo | 240804T185926Z | bb94 |
| 064 | StackControl | 240804T182101Z | CREAsTIV |
| 014 | 05AB1E | 240430T072922Z | Kevin Cr |
| 064 | Python 3 | 240428T224239Z | Jonathan |
| 064 | Haskell | 240429T112003Z | matteo_c |
| 055 | R | 240429T093416Z | ovs |
| 015 | Brachylog | 240429T084254Z | Fatalize |
| 014 | K ngn/k | 240426T175304Z | att |
| 132 | Python | 240428T213236Z | corvus_1 |
| 007 | Jelly | 240428T201420Z | Jonathan |
| 091 | Python | 240427T221429Z | math sca |
| 082 | R | 240427T184307Z | pajonk |
| 275 | Google Sheets | 240427T091844Z | doubleun |
| 209 | Python | 240427T111818Z | Rhaixer |
| 012 | Uiua 0.11.0 | 240426T161549Z | Tbw |
| 066 | JavaScript ES6 | 240426T155509Z | Arnauld |
| 032 | Charcoal | 240427T063921Z | Neil |
| 024 | J | 240426T222855Z | Jonah |
| 975 | Vyxal | 240427T041036Z | lyxal |
| 043 | Perl 5 + a M5.10.0 | 240426T160545Z | Dom Hast |
Ruby, 71 bytes
->n,r{(o=%w'+ -').product(*[o]*(n.size-2)).find{eval(n.zip(_1)*'')==r}}
T-SQL, 3378 1450 1290 bytes
2025 May 22 (00:20 UTC): -1928 bytes
2025 May 22 (02:50 UTC): -160 bytes
Update May 22, 2025 (00:20 UTC): Arrgh, so close to sub-1K, and so close to saving 2K bytes!
Update May 22, 2025 (02:50 UTC): Yay!
I saw this a few days ago, and was thinking of languages I could use to solve this problem. Ended up deciding to write the entire thing in a query on data.stackexchange.com because I had a lot of free time today and was willing to hardcode each and every one of the cases.
This runs in ~3ms on my Chromebook so I think it is as optimized runtime-wise as it gets.
Also note that loops don't exactly work in T-SQL, so I ended up having to brute-force all 33 cases (32 different ways to arrange +/- and the case where nothing works).
Although it is cool that it actually rounds down if one of the inputs is a decimal, I was afraid that might not happen. Last year me was stupid, of course it does! It's an integer type!
DECLARE @ INT=##a##,@b INT=##b##,@c INT=##c##,@d INT=##d##,@e INT=##e##,@f INT=##f##,@g INT=##g##
IF @+@b+@c+@d+@e+@f=@g SELECT'+++++'
IF @+@b+@c+@d+@e-@f=@g SELECT'++++-'
IF @+@b+@c+@d-@e+@f=@g SELECT'+++-+'
IF @+@b+@c+@d-@e-@f=@g SELECT'+++--'
IF @+@b+@c-@d+@e+@f=@g SELECT'++-++'
IF @+@b+@c-@d+@e-@f=@g SELECT'++-+-'
IF @+@b+@c-@d-@e+@f=@g SELECT'++--+'
IF @+@b+@c-@d-@e-@f=@g SELECT'++---'
IF @+@b-@c+@d+@e+@f=@g SELECT'+-+++'
IF @+@b-@c+@d+@e-@f=@g SELECT'+-++-'
IF @+@b-@c+@d-@e+@f=@g SELECT'+-+-+'
IF @+@b-@c+@d-@e-@f=@g SELECT'+-+--'
IF @+@b-@c-@d+@e+@f=@g SELECT'+--++'
IF @+@b-@c-@d+@e-@f=@g SELECT'+--+-'
IF @+@b-@c-@d-@e+@f=@g SELECT'+---+'
IF @+@b-@c-@d-@e-@f=@g SELECT'+----'
IF @-@b+@c+@d+@e+@f=@g SELECT'-++++'
IF @-@b+@c+@d+@e-@f=@g SELECT'-+++-'
IF @-@b+@c+@d-@e+@f=@g SELECT'-++-+'
IF @-@b+@c+@d-@e-@f=@g SELECT'-++--'
IF @-@b+@c-@d+@e+@f=@g SELECT'-+-++'
IF @-@b+@c-@d+@e-@f=@g SELECT'-+-+-'
IF @-@b+@c-@d-@e+@f=@g SELECT'-+--+'
IF @-@b+@c-@d-@e-@f=@g SELECT'-+---'
IF @-@b-@c+@d+@e+@f=@g SELECT'--+++'
IF @-@b-@c+@d+@e-@f=@g SELECT'--++-'
IF @-@b-@c+@d-@e+@f=@g SELECT'--+-+'
IF @-@b-@c+@d-@e-@f=@g SELECT'--+--'
IF @-@b-@c-@d+@e+@f=@g SELECT'---++'
IF @-@b-@c-@d+@e-@f=@g SELECT'---+-'
IF @-@b-@c-@d-@e+@f=@g SELECT'----+'
IF @-@b-@c-@d-@e-@f=@g SELECT'-----'
SELECT''
Try it online (also commented out version) (3378 bytes)
Try it online (no comments, 2025-05-22 version 1) (1450 bytes)
Try it online (no comments, 2025-05-22 version 2) (1290 bytes)
Raku (Perl 6) (rakudo), 59 bytes
Outputs a list of all valid solutions, using 1 for + and -1 for -.
{grep {.[*-1]-.[0]==[+] .[1..*]Z*@^a},[X,] ((-1,1)xx$_-2,)}
StackControl, 64 characters
Pretty bad solution, but i m too lazy to think more sooo
works only on the last commit since ∸ and one bug
btw can work with any operations (add them onto this: [#+#∸] list)
⟄←:←⬌1∸:←[#+#∸]⇆∏(→→:←⇆←⦽→:(⇆←)⟲←⇆→(→⇆⟃⇆←!→)⟲,→:⬌1⇆-⇆→:3(⇆←)⟲=)⊚
try this:
[1 2 4 3 1 1]⟄←:←⬌1∸:←[#+#∸]⇆∏(→→:←⇆←⦽→:(⇆←)⟲←⇆→(→⇆⟃⇆←!→)⟲,→:⬌1⇆-⇆→:3(⇆←)⟲=)⊚
Explanation
⟄ - extracting last value since this is answer
←:←⬌1∸:← - get length + sum cursor setup
[#+#∸]⇆∏ make product of those elements based on the length
(for [2 5 7] it generates product of operations + and - with len of 3-1: [+ +] [+ -] [- +] [- -])
(...)⊚ - store values, only where function returns non zero
Now what inside:
→→:←⇆←⦽→ - setup cursor and unpack all our values
:(⇆←)⟲ - move our operation list to the begining of unpacked values
←⇆→(→⇆⟃⇆←!→)⟲ - [length] times repeat {move our operation list; pop first operation; execute it}
,→:⬌1⇆-⇆→: - delete empty list and setup same values for next iteration
3(⇆←)⟲ - return cursor
= - check if our value is equals
05AB1E, 14 bytes
®X‚Ig<ãʒ1š¹*OQ
Two separated inputs \$[a_0,...,a_{n-1}]\$ and \$a_n\$. Output as a list of all possible results, with -1 for - and 1 for +.
Try it online or verify all test cases.
Explanation:
®X‚ # Push pair [-1,1]
Ig< # Push the length-1 of the first input-list
ã # Take the cartesian product of the two
ʒ # Filter this list:
1š # Prepend a 1
¹* # Multiply the values at the same positions to the first input-list
O # Sum them together
Q # Check whether it equals the (implicit) second input-integer
# (after which the filtered list is output implicitly)
Python 3, 75 67 64 bytes
-8 bytes thanks to Jitse (by placing the output in r - a very smart golf!).
f=lambda a,b,*r:f(a+b,*r,-2)or f(a-b,*r,-3)if-1<r[0]else(a==b)*r
A recursive function that accepts the full sequence of non-negative integers as arguments in order \$a_1, a_2, \cdots, a_N\$ and returns a tuple of instructions as integers, where -2 means add and -3 means subtract, representing one way that works, or an empty tuple if not possible.
How?
The first two terms of the sequence are a and b, the rest of the sequence is r.
If r's first element is greater than -1 the function is called with the first two terms added and -2 "instruction" is appended to r but if that results in an empty tuple then the function is called with the second term subtracted from the first along an additional -3 "instruction".
However, if r's first element is -2 or -3 then r is returned if a==b otherwise an empty tuple is returned.
R, 55 bytes
Takes \$a_N\$ as a separate argument, returns a matrix of all solutions with 1/2 for -/+.
\(a,b)which(Reduce(\(x,y)x%o%c(1/y,y),2^a)==2^b,T)[,-1]
The size of numbers this can handle is fairly limited by the powers of 2 a double value can represents.
Brachylog, 15 bytes
{+.∧1w₄|-.∧w₄}ˡ
Outputs + as 1 and - as 0. Empty output for impossible test cases.
The first n-1 numbers are given as an input list, and the expected result is given as the output variable.
Explanation
{ }ˡ Left fold on the input list:
Either…
+. Add the 2 numbers together
∧1w₄ And delayed write a 1 to stdout
| Or…
-. Subtract the 2 numbers together
∧w₄ And delayed write an unknown variable (defaults to 0) to stdout
The output of the left fold must match the given output
This predicate will try different combinations of + and - in a left fold over the input list until the result is equal to the expected result given as output.
Once this predicate succeeds, the delayed writes w₄ will be printed out to stdout. If we were to use normal write commands, then it would write everytime it tries a choice, instead of writing the final correct choices.
If the test case is impossible, the predicate will eventually fail after trying everything, and the delayed writes will not be called. Nothing is thus printed to stdout.
K (ngn/k), 14 bytes
-1 thanks to naffetS
&~*{x-/:y,-y}/
Return a 2d list with solutions in reverse order. -+ are denoted by 01.
{x-/:y,-y}/ all values obtainable by inserting -/+
* enforce last op is -
&~ locate 0s
Python, 132 bytes
lambda e,f,*a:{p for p in permutations("+-"*len(a),len(a))if f+eval("".join(x+str(y)for x,y in zip(p,a)))==e}
from itertools import*
Jelly, 7 bytes
_,+ɗ/œẹ
A dyadic Link that accepts a list of non-negative* integers*, the summands \$[a_1, a_2, \cdots, a_{N-1}]\$, on the left and a non-negative* integer*, the goal \$a_N\$, on the right and yields a list of reversed lists of instructions. 1 means subtract while 2 means add.
Try it online! Or see the test-suite.
* Not a necessary restriction, any numbers will do.
How?
_,+ɗ/œẹ - Link: list of numbers, Summands; number, Goal
/ - reduce {Summands} by:
ɗ - last three links as a dyad - f(L, R):
_ - {L} subtract {R} (vectorises) -> L-R
+ - {L} add {R} (vectorises) -> L+R
, - {L-R} pair {L+R} -> [L-R, L+R]
œẹ - all multidimensional indices of {Goal}
Python, 105 91 bytes
- 14 bytes thanks to xnor
def f(l,n,o=[]):
if[n]==l:print(o)
if l[1:]:c,d,*r=l;[f([c+a*d]+r,n,o+[a])for a in[1,-1]]
I have very little experience of golfing in Python, so any tips would be appreciated.
R, 85 82 bytes
\(a,s=expand.grid(rep(list(c(-1,1)),sum(a|1)-2)))s[apply(s,1,\(x)!c(1,x,-1)%*%a),]
Takes input as a vector \$[a_1, a_2, \ldots, a_{N-1}, a_N]\$ and returns a matrix of ±1s with solutions in rows. For "none", the function outputs numeric(0).
Google Sheets, 275 bytes
=let(d,tocol(A:A,1),ø,tocol(,1),regexreplace(reduce(d,index(mod(sequence(6^5),rows(d)-1)+2),lambda(a,i,if(a=a&"",a,let(r,{chooserows(a,sequence(i-1));index(a,i)*(2*(rand()<0.5)-1);iferror(chooserows(a,sequence(rows(a)-i,1,i+1)),ø)},if(sum(r)=B1,join("+",r),r))))),"\+-","-"))
Put operands in column A, the expected result in cell B1 and the formula in cell B2.
Gives the solution as in 3-2+4-1, or an error when a solution was not found. Works probabilistically up to 7776 rounds which seems plenty with up to 100 small operands or several dozen operands of any size.
Ungolfed:
=regexreplace(
reduce(tocol(A1:A, 1), index(mod(sequence(6^5), rows(tocol(A1:A, 1)) - 1) + 2), lambda(a, i,
if(istext(a), a, let(
null, tocol(æ, 2),
head, chooserows(a, sequence(i - 1)),
try, index(a, i) * (2 * (rand() < 0.5) - 1),
tail, iferror(chooserows(a, sequence(rows(a) - i, 1, i + 1)), null),
result, vstack(head, try, tail),
if(sum(result) = B1, join("+", result), result)
)
))),
"\+-", "-"
)
Python, 215 209 bytes
-6 bytes by using another input format.
lambda*n,p,r=range,t=len:(x:=[bin(i)[2:].rjust(t(n)-1,'0').replace('1','-').replace('0','+')for i in r(2**~-t(n))],[x[j]for j in r(t(x))if eval(n[0]+"".join([x[j][l]+n[1:][l]for l in r(t(n)-1)])+"==%s"%p)])[1]
The first part of the lambda creates a list with the signs, and the second part evaluates them. Returns [] if there is no solution. Takes input as parameters of stringified ints, followed by a keyword argument taking the RHS value.
Uiua 0.11.0, 2712 bytes SBCS
≡⇌⊚=/(⊟⊃-+:)
Similar to @att's K answer, so upvote that one too. Takes the list and \$a_N\$ on stack and returns all possible +/- choices as an array of 1s and 0s, with an empty list if not possible.
Explanation
≡⇌⊚=/(⊟⊃-+:)
/( ) # fold the list by
⊃-+: # a-b and a+b
⊟ # combine as two rows of a new array
= # test equal to a_N element-wise
⊚ # get coordinates of 1s
≡⇌ # reverse each row
Old solution
⊡⊗:/+⍉⊙:⊃×∘¬×2☇1⇡×2⊃±¤⊙-:°⊂
Explanation
°⊂ # take first element off of list
: # swap the top two of stack
⊙- # subtract a1 from aN
⊃±¤ # get the signs of the list (all 1s)
# and a copy with an axis added
⇡×2 # *2 (all 2s) and range: all binary lists
☇1 # make it rank 2
¬×2 # not(x*2), changes 0,1 to 1,-1
⊃×∘ # multiply by list, keeping 1,-1 array
⊙: # flip positions 2 and 3 on stack
/+⍉ # sum each row
⊗: # find the index of the first aN-a1
⊡ # pick that from the 1,-1 array, error if no solution
JavaScript (ES6), 66 bytes
Returns either a binary string (with 0 for - / 1 for +) or undefined if there's no solution.
f=([n,...a],s=0,o)=>a+a?f(a,s+n,o?o+1:[])||o&&f(a,s-n,o+0):s==n&&o
Commented
f = ( // f is a recursive function taking:
[ n, // n = next value from input array
...a ], // a[] = remaining values
s = 0, // s = sum
o // o = output (initially undefined)
) => //
a + a ? // if a[] is not empty:
f( // do a 1st recursive call:
a, // pass a[]
s + n, // add n to s
o ? // if o is defined:
o + 1 // append '1' for '+' to o
: // else:
[] // initialize o to [] (coerced to
// an empty string, but truthy)
) || // end of recursive call
o && // if o is defined,
f( // do a 2nd recursive call:
a, // pass a[]
s - n, // subtract n from s
o + 0 // append '0' for '-' to o
) // end of recursive call
: // else:
s == n && o // if s = n, return o
Charcoal, 32 bytes
IEΦE…X²⁻Lθ²X²⊖Lθ⊖⊗↨⊗鲬Σ×ιθ✂ι¹±¹
Attempt This Online! Link is to verbose version of code. Outputs a list of 1s and -1s for the signs with each solution double-spaced. Explanation:
² Literal integer `2`
X Raised to power
θ Input list
L Length
⁻ Subtract
² Literal integer `2`
… Range to
² Literal integer `2`
X Raised to power
θ Input list
L Length
⊖ Decremented
E Map over values
ι Current value
⊗ Doubled
↨ Converted to base
² Literal integer `2`
⊗ Vectorised doubled
⊖ Vectorised decremented
Φ Filtered where
ι Current list
Σ× Dot product with
θ Input list
¬ Is zero
E Map over solutions
✂ι¹±¹ Slice off leading and trailing signs
I Cast to string
Implicitly print
J, 26 24 bytes
(##:@i.@#)@(=]F..(+,-~))
Outputs all solutions or an empty list when none exists. 1 = -, 0 = + .
]F..(+,-~)Fold forward +/-, catting both options to the accumulated result each time=Where does that equal the left input? Returns boolean list.#Use that 0-1 list to filter...#:@i.@#The numbers0 1 2 ... <len of list - 1>converted to binary
Worth noting this is a similar idea to att's nicer K solution... what makes that more elegant is K's ability to "find" 1s in a multidimensional list, return a lookup path directly to that list as a list of indexes for each axis. This can also be thought of as the result of a binary search algorithm.
J's I., on the other hand, only works for 1 dimensional lists, so we have to deal with some boring mechanics manually to accomplish the same thing.
Vyxal, 78 bitsv2, 9.75 bytes
L‹k+↔'1p¹*∑⁰=
Bitstring:
011000010011100110100110111001010111110001011011110111001100010101101101001001
A silly little answer that might be golfable with a better algorithm. Outputs all possible solutions, or an empty list for no solutions. Represents - as -1 and + as 1.
Explained
L‹k+↔'1p¹*∑⁰=
L‹ ↔ # Combinations with replacement of length (a0...a(n-1) length - 1) of
k+ # the list [1, -1]
' # Filtered to keep combinations where:
1p # Prepending 1
¹* # and transferring the signs of the combination to a0...a(n-1)
∑ = # sums to
⁰ # a(n)
💎
Created with the help of Luminespire.
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 evaluated as code (eval works on $_ by default) and if it matches $n is output (say - which prints $_ by default).
