7
A twist on a recently asked question, you are given a list of operators and a result and you need to find integers which, when operated on by those operators, generate the result.
Example 1
input
+ + * / 13
possible output
4 8 2 2 4
which means that 4 + 8 + 2 * 2 / 4 = 13
.
More specifically, you are given a sequence of N operators (between 1 and 5 inclusive) and an integer result, space separated. You must print N+1 integers, space separated, in the range 1 through 9, inclusive, that when alternated with the operators produce the result.
The operators are +,-,*,/
. They have the natural precedence order (*
and /
before +
and -
, left associative). Divides must be integral divides, a divide with a fractional result is not allowed. Operators must appear in the order given.
Most problems will have more than one solution. Printing any of them, or any subset of them, is fine. If there is no solution, print NO SOLUTION
.
Example 2
input
/ + - - 16
output
9 1 9 1 1
Example 3
input
+ 5
output
1 4
2 3
3 2
4 1
(or any nonempty subset of those lines)
Example 4
input
+ + 30
output
NO SOLUTION
Example 5
input
/ + / 3
output
6 3 5 5
note that 3 2 3 2
is not a solution, as 3/2+3/2=3
is true but the individual divides have fractional values.
Standard code golf, shortest answer wins.
Just to clarify something for me: is
9 2
a valid solution if the input is/ 4
? – Gareth – 2012-06-22T17:00:09.740@Gareth, no it isn't. That is a divide with a fractional result. – Keith Randall – 2012-06-22T19:07:16.933
Any chance the result can be printed without spaces? – Steven Rumbalski – 2012-06-22T19:13:44.063
@Steven: nope, format as specified. – Keith Randall – 2012-06-22T19:42:02.893