Hitting a brick wall on Code Fights!

For the past two weeks I have been cruising through the Code Fights Arcade Mode Challenges. As I complete each challenge I get a small ego boost knowing that only 8-900 other C++ coders have solved the same problems.

Most of the time I don't understand their code and it looks like they were trying to put as few lines as possible. I don't see this as "efficient" since I can't understand their logic, nor is their code readable. Sometimes I come across another solution that is close to mine and it helps me see what changes I can make to my code to make it more effective. For example, I will spot a more logical way to accomplish the same task, so I change mine. This normally shortens my code while keeping it readable. Sometimes, I realize that I have entire lines of code which don't matter so I can delete it with the same results.

The current challenge I am working on is a doozy! Only 200 other C++ coders have solved it. I am given the following string:

The ((quick (brown) (fox) jumps over the lazy) dog)

The challenge is to take each set of (parenthesis) and reverse what is inside. Logically, if you start from the center and work your way out, the last flip will give you the desired outcome. In the case above, you are looking for an output of:

The god quick nworb xof jumps over the lazy

I haven't got the solution, but my logical process is pretty solid.
1. Get the highest number of parenthesis in the string. For the example above (3).
2. Loop through the string and get the element position of the 3rd '(' and the 3rd ')'.
3. Loop through that section reversing the word. example )nworb(.
4. Replace parenthesis with NULL space \0.
5. Continue until parenthesis count equals 0.
6. Copy the string over to an array without copying the NULL \0 spaces.

As you can see, this is going to take a few more than 10 lines of code. The problem I am running into is the manipulation of the string and the arrays.

I have seen other people write separate functions inside of Code Fights solutions, though I felt they were unnecessary. I think in this case, I am going to have to write an entire program in order to solve this problem. I could cram it all into a single function, but then someone reading my code might not be able to follow the logic which would render it (IMO) bad code.

I'll keep you up to date with my progress.

Comments

Popular posts from this blog

What's For Lunch App!