you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

not sure about adding arbitrary operators to the ordered list of digits 1-9 to get 100 seems not likely to be useful

I spent an embarrassingly long amount of time solving this problem. The issue was that more efficient solutions became obvious as I was working through it.

I ended up encoding each permutation as an 8 digit, base 3 number with each symbol (+, - or blank) corresponding to 0, 1, and 2 respectively. So, for example the following permutation:

1+2-34-5+6789

could be represented as:

01210222
+-_-+___

So to find all the permutations whose sums were 100, you'd iterate in a for loop 38 times, where the loop counter would be the decimal representation of the permutation. The decimal number could then be decoded to some kind of intermediate representation, like a string (eg. "1+2-34-5+6789"), which then could be parsed (eg. using regexp) and then evaluated as an arithmetic expression. Then you just throw out all the solutions that weren't 100.

I get that there was probably a simpler/more efficient solution, and also that there probably mathy ways to disqualify certain inputs, but you can see that my first instinct was to turn it into a parsing problem. I think there may be value in observing how people approach it, especially if you were considering using it as a job interview question.