Implement the createExpression(int number) method. The createExpression method has one parameter called number. This parameter is a number from 1 to 3000 inclusive. You need to display an arithmetic expression whose result is number.
Derive a specific number
You must be signed in to leave a comment
25 July 2022, 14:53
I ended up solving this by recursion. I used a TreeMap with keys 1, 3, 9, 27... and values -1, 0, 1. I was feeling pretty good because it's a tricky problem and recursion requires folding your brain into a mobius strip, and I passed on the first try. I shouldn't have looked at the solution afterwards, because now I don't feel as good, haha. I have about twice as many lines as the official solution and I'm pretty sure it takes a lot longer to run (although it still was an immediate response from my perspective). Even having read online about balanced ternary systems, I still would never have come up with the official solution (I thought that my values in the TreeMap were as far as it went relating to that system). For one thing, when you look up how to use a balanced ternary system, they explain a two-step process, first converting the base-10 number to regular ternary, and then converting regular ternary to balanced ternary. The official solution appears to use a process to go straight from base-10 to balanced ternary with less work than either of the two separate steps. My first response looking at the solution was "what witchcraft is this?" I'm still not really sure I get it. I get what it does, I'm just not sure I get why it works.