27
Given a string of the characters +=-
where there is at least one =
, insert positive integers between all the symbols and at the start and the end such that the math equations are satisfied.
For example, given the input
+-=-=
you need to insert positive integers A through F like this
A+B-C=D-E=F
such that the equations are all satisfied, i.e. A + B - C
and D - E
and F
are all the same number.
There are many possible ways to do this since, as long as the equations work out, any set of positive integers may be used. Each line here is a possible valid output to input +-=-=
:
2+3-4=6-5=1
1+1-1=2-1=1
4+2-4=4-2=2
100+1-10=182-91=91
89+231-77=1024-781=243
Note that the value of the expressions is not required to be a positive integer like the inserted numbers are. For example, given input -=-
the outputs 1-10=8-17
(evals to -9) and 10-1=17-8
(evals to 9) are both equally valid. Of course for some inputs such as =
it's impossible to have a negative as the expression since only positive numbers like 5=5
can be inserted.
Note also that zero is not a positive integer.
The shortest code in bytes wins.
You may output the numbers as a list instead of inserting them directly into the string. If you do output the string there may be spaces separating symbols and numbers. So, for input +-=-=
, outputting
2, 3, 4, 6, 5, 1
or
2 + 3 - 4 = 6 - 5 = 1
is equivalent to outputting
2+3-4=6-5=1
Test Cases
Input | One Possible Output
= | 1=1
== | 2=2=2
+= | 1+3=4
=+ | 2=1+1
-= | 30-10=20
=- | 1=2-1
=-= | 3=7-4=3
=+= | 2=1+1=2
=== | 100=100=100=100
+=- | 3+2=7-2
-=+ | 7-2=3+2
+=+ | 3+3=3+3
-=- | 1-10=8-17
--= | 60-1-1=58
++= | 60+1+1=62
-+= | 60-9+1=52
+-= | 60+9-1=68
+-=-= | 2+3-4=6-5=1
--=-- | 2-1-1=2-1-1
==-== | 47=47=50-3=47=47
=++=+-=-+=--= | 3=1+1+1=3+1-1=1-1+3=5-1-1=3
+--++-=-+-+- | 35+10-16-29+20+107-1000=5-4+3-2+1-876
====== | 8=8=8=8=8=8=8
Related. – Martin Ender – 2017-03-09T19:15:10.590
Can we assume any upper bound on the length of the formula? – xnor – 2017-03-09T19:41:44.190
2@xnor You may assume the input is less than 2^16 characters long if that helps. – Calvin's Hobbies – 2017-03-09T19:44:04.383