The original problem goes like this:
N.B. It turns out my strategy is completey wrong, but read on for an experiment with using
parse to generate code on the fly. An explanation and computational solution are available at the original site mentioned above.
That being said, my first guess was to look for some set of operators (+,-,*,/) between each of the digits that would produce the correct response. For example,9313=1could be calculated as9 / 3 + 1 – 3 = 1, giving a solution of/+-. In order to try every combination of the four basic arithmetic operators in three different positions, I had to generate some R code on the fly.
R writes R
One of the things I like about R is that not only can I write R code, but R can write R code, too. We can create a line of code and store it as a variable. Then R will evaluate it whenever we like.
I can create the code dynamically by making a string representation of the code, then parsing it into an expression that can be evaluated as above.
Now R can simply write the code for me:
Clearly it doesn’t give us the right answer.
But it was fun to code in R with R.