3
\$\begingroup\$

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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Or use the pre-processor to do the job it was designed for! :-) \$\endgroup\$ Commented Aug 14, 2013 at 0:05

1 Answer 1

10
\$\begingroup\$

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
/**/
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Double-bonus: The last line in each case may be replaced by //*/, giving four distinct solutions :) \$\endgroup\$
    – bobthecow
    Commented Aug 3, 2013 at 17:07
  • \$\begingroup\$ 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! :) \$\endgroup\$
    – Timwi
    Commented Aug 29, 2013 at 11:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.