[Community Puzzle] Place the Parenthesis

https://www.codingame.com/training/easy/place-the-parenthesis

Created by @dolmen1234,validated by @ToshiTuringMachine,@Klemek and @alze.
If you have any issues, feel free to ping them.

a couple of parenthesis

I read that as if I can place multiple pairs of parentheses. Doesnâ€™t seem to be the case (or at least isnâ€™t required to solve the problem).

Validator 3 has 2 valid solutions, namely:

(-4+1)*2=-6
-(4+1*2)=-6

Testing all possibilities and thus finding both of them gave me 75% on submit.

1 Like

in this case 4 is the facto negative

Although this is easy in many dynamic languages, this is definitely not an â€śeasyâ€ť level puzzle in languages where you have to write fractional math and the expression evaluator yourself. Although integer-only math passes all the test cases, an input like â€ś2*3/4*2=3â€ť would have complicated things significantly and there was no way knowing if a validator had such a case without submitting a solution.

Even in dynamic languages that allow expression evaluation at runtime, such functionality should never be used for arbitrary input due to it being a massive security hole. Thus, the puzzle teaches bad programming habits.

1 Like

Agreed itâ€™s easy when you have an eval function at your disposal, but if you donâ€™t then the puzzle jumps up in difficulty. Also looking at the authorâ€™s solution, some stuff is hardcoded and would fail in many situations, like the following +2 offset to see where to place parenthesis :

``````for (let i = 0 ; i < left.length-2 ; i++)
for (let j = i+2 ; j <= left.length ; j++)
``````

And there are only 4 tests cases which is probably half of what it should have.

Letâ€™s modify that code:
`for (let i = left.length-3; i>=0; i--) {`
now another solution gets found for `-4+1*2=-6`.

I changed validator 3, now all validators have only one solution.

I guess people who tested the problem used some sort of early exit (a return in a function for example) if you encounter a solution, and the expected solution of validator 3 was the first one you get so they didnâ€™t notice the problem.

1 Like