Flip the commented-out code

3

2

This puzzle works for any programming language that has the same // single-line and /* ... */ multi-line comments that C++, C# and Java do.

You are debugging an application. You find a fishy block of code, which you comment out and replace with some new code:

/*
code block A
*/
code block B

After some debugging, you find that you need to step through block A again to see how it worked. This requires quite a lot of cursor movement:

                ← remove “/*” here
code block A
/*              ← change “*/” to “/*” here
code block B
*/              ← add “*/” here

This is very laborious if you need to keep switching between block A and block B. So you decide to find a better way.

Place combinations of asterisks and slashes before block A, between the blocks, and after block B, such that you can flip between block A and block B by adding/removing only one character in one place. You may assume that the code blocks A and B do not contain any /* or */.

There are two possible solutions.

Timwi

Posted 2013-08-03T09:13:46.777

Reputation: 12 158

Question was closed 2014-01-10T17:00:59.957

2Or use the pre-processor to do the job it was designed for! :-) – Martin York – 2013-08-14T00:05:06.660

Answers

10

Toggle commented out blocks by adding/removing the last slash on the first line:

/**/
code block A
/*/
code block B
/**/

Bonus solution, toggle this one by adding/removing the first slash on the first line:

//*
code block A
/*/
code block B
/**/

bobthecow

Posted 2013-08-03T09:13:46.777

Reputation: 216

1Double-bonus: The last line in each case may be replaced by //*/, giving four distinct solutions :) – bobthecow – 2013-08-03T17:07:32.160

It is your second solution that I’ve always used, and bobthecow’s variation on it that I knew of. Congratulations for finding another solution I hadn’t yet encountered! :) – Timwi – 2013-08-29T11:39:39.697