Comment blocks

A forum for good logic/math puzzles.

Moderators: jestingrabbit, Prelates, Moderators General

Comment blocks

Postby warriorness » Tue Apr 10, 2007 7:53 pm UTC

In C, C++, Java, etc, comments (lines of code that are completely ignored by the computer) can be denoted in one of two ways:

Single-line comments, denoted by "//": Everything from the comment marker to the end of the line is ignored.
Code: Select all
line of code that does stuff //this is a comment


Multi-line comments, denoted at the beginning by "/* " and at the end by " */". Everything in between is ignored.
Code: Select all
line of code /*this is a comment
this is also a comment*/ line of code


One thing I like to do is set up comment blocks so that adding or removing one character toggles whether the line is a comment.
Before:
Code: Select all
/* // */ line of code

After (delete the first apostrophe):
Code: Select all
///*/ now this is a comment

(whitespace doesn't matter - it's used here for clarity)

The challenge: Design a setup of comment markers around two lines of code such that adding or removing a single character will switch which line is commented. (Exactly one of the two must be commented.)

If this is impossible (prove it!), show how to do it with only two characters.

Now (just in case this is too easy), design a setup for THREE lines of code, such that inserting/removing a character at various positions or something - a change of only one character - switches between which line is activated. Repeat for four, five, etc until you get stuck.

I don't have a solution, not even for the two-line problem. I tried to do it today in programming class and couldn't figure it out.
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby genevabeagle » Tue Apr 10, 2007 9:36 pm UTC

Although my programming experience is almost nil, I think the code below might work. Unfortunately, it occupies three (or more) lines instead of two, and requires the second segment of code to be compressed to fit on one line.

Add/remove the first character to toggle between segments.
Code: Select all
//*
codey wodey
// */ buggy wuggy
genevabeagle
 
Posts: 42
Joined: Tue Dec 12, 2006 11:32 pm UTC

Postby warriorness » Tue Apr 10, 2007 9:40 pm UTC

That works! New challenge (in addition to those previously stated): Make it work for two multi-line blocks of code.
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby genevabeagle » Tue Apr 10, 2007 10:32 pm UTC

Two multi-line blocks (I think):
Code: Select all
//*
no raptors
/*/
lotsa raptors
/* */

Edit: much simplified.
genevabeagle
 
Posts: 42
Joined: Tue Dec 12, 2006 11:32 pm UTC

Postby Meran » Tue Apr 10, 2007 11:07 pm UTC

GCC will actually throw a warning if you have a /* inside of a comment block, like (/* /* */ */)

and warnings as errors is your friend.
Meran
 
Posts: 4
Joined: Fri Mar 30, 2007 10:27 pm UTC

Postby warriorness » Tue Apr 10, 2007 11:08 pm UTC

genevabeagle wrote:Two multi-line blocks (I think):
Code: Select all
//*
no raptors
/*/
lotsa raptors
/* */

Edit: much simplified.


How does this work? Which characters should be deleted? (obviously the asterisk on the middle line - what else)
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby OmenPigeon » Tue Apr 10, 2007 11:18 pm UTC

warriorness wrote:
genevabeagle wrote:Two multi-line blocks (I think):
Code: Select all
//*
no raptors
/*/
lotsa raptors
/* */

Edit: much simplified.


How does this work? Which characters should be deleted? (obviously the asterisk on the middle line - what else)


I think just the first '/' on the first line.

As it stands in the quote the first line is commented out. 'no raptors' is executed. '/*/' begins a comment block, 'lotsa raptors' is commented out and '/* */' ends the comment block.

If you remove the first '/' then the initial '/*' starts a comment block, 'no raptors' is commented out, '/*/' ends the comment block, 'lotsa raptors' is executed and '/* */' does nothing.
As long as I am alive and well I will continue to feel strongly about prose style, to love the surface of the earth, and to take pleasure in scraps of useless information.
~ George Orwell
User avatar
OmenPigeon
Peddler of Gossamer Lies
 
Posts: 673
Joined: Mon Sep 25, 2006 6:08 am UTC

Postby warriorness » Tue Apr 10, 2007 11:24 pm UTC

Oh ho, very clever! Using the asterisk in /*/ bi-directionally.

So how about that three-section challenge, now?
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby genevabeagle » Tue Apr 10, 2007 11:28 pm UTC

Meran wrote:GCC will actually throw a warning if you have a /* inside of a comment block, like (/* /* */ */)

and warnings as errors is your friend.

D'oh, sorry, not a C programmer, but definitely a good thing to keep in mind. Out of curiosity, does the compiler throw warnings for all comment-like embeds (such as /* /* */), or does it only show warnings for nested comments (like your example of /* /* */ */)?

Edit: typos.
genevabeagle
 
Posts: 42
Joined: Tue Dec 12, 2006 11:32 pm UTC

Postby Meran » Tue Apr 10, 2007 11:33 pm UTC

It'll throw an error for any comment-like embeds, kind of annoying and GCC is really picky (you need a new line at the end of all your files too...)

/***********************************
/*
* My Function documentation
*
***********************************/

will throw an error :/ Had to fix like 234087234987234 of these warnings.
Meran
 
Posts: 4
Joined: Fri Mar 30, 2007 10:27 pm UTC

Postby warriorness » Wed Apr 11, 2007 12:04 am UTC

Strange. As a Java programmer, I've only worked with javac, not GCC, so embedded comment markers are legal in this puzzle :D
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby fynch » Wed Apr 11, 2007 1:29 am UTC

A simpler implementation of the original problem:

Code: Select all
//some code
//some more


Just delete one slash... Dunno if that will work, but it is much simpler.
User avatar
fynch
 
Posts: 69
Joined: Tue Mar 27, 2007 6:30 pm UTC

Postby warriorness » Wed Apr 11, 2007 1:43 am UTC

fynch wrote:A simpler implementation of the original problem:

Code: Select all
//some code
//some more


Just delete one slash... Dunno if that will work, but it is much simpler.


Is there a magical slash there that I'm missing? The goal of the puzzle is to find a way to avoid having to do this - i.e. typing/deleting four characters each time you want to toggle a line of code.
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby Yakk » Wed Apr 11, 2007 12:48 pm UTC

warriorness wrote:Oh ho, very clever! Using the asterisk in /*/ bi-directionally.

So how about that three-section challenge, now?


Undoable by deleting a character. Might be doable by changing a character.
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10448
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Postby warriorness » Wed Apr 11, 2007 4:18 pm UTC

Well, yes, as deleting/adding a character can only have two possible states. Changing a character, or adding/moving/deleting a character in more than one location is what I'm looking for.
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby EvanED » Wed Apr 11, 2007 6:04 pm UTC

warriorness wrote:Strange. As a Java programmer, I've only worked with javac, not GCC, so embedded comment markers are legal in this puzzle :D


Embedded comment markers are legal in C too, but many compilers will throw a warning. For instance, Apple's old MPW C compiler said "Call me paranoid but finding `/*' inside this comment makes me suspicious".

(The idea of course being that if you have /* a /* b */ c */, the code at c is not commented out while this is very possibly not what you meant.)
EvanED
 
Posts: 4141
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Postby Yakk » Wed Apr 11, 2007 6:25 pm UTC

More:
To effect the state of the first line, one must change something before the first line, because C++ can be parsed in a one-pass algorithm: the meaning of tokens does not change

The meaning of [^/]/* changes depending if you are in a C comment. If you are in a comment, it is an open comment. If you are not, it is meaningless.

*/ is illegal outside of a comment.

Are there any character sequences that are legal both inside and outside of a comment, and have different meaning depending on which?

//* forms a comment start switch, and toggles between being a C++ comment blank line and the start of a C comment.

...

Ok, here goes an attempt:
Code: Select all
// first comment marker
#define which 1
#if which==1
// a comment marker
no raptors
#endif
#if which==2
some chickens
// another comment marker
#endif
#if which==3
lotsa raptors
#endif
// more comment markers


which is cheating. :)
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10448
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Postby warriorness » Wed Apr 11, 2007 7:22 pm UTC

Yakk wrote:Ok, here goes an attempt:
Code: Select all
// first comment marker
#define which 1
#if which==1
// a comment marker
no raptors
#endif
#if which==2
some chickens
// another comment marker
#endif
#if which==3
lotsa raptors
#endif
// more comment markers


which is cheating. :)


That's stupid. You have to modify the comment markers to work the puzzle.

But that gave me this idea for the three-block code (although this takes advantage of code - you couldn't use it to toggle between whole functions or classes):
Code: Select all
int x
//*
=0
/*/ //*
=1
/**/
;

if (x==null)
     do some stuff;
else if (x==0)
     do other stuff;
else
     do different stuff;

As it is, x is zero. Delete the first slash (on line 2), and x is assigned as 1. Delete (in addition) the third (second-to-last) slash on line four, and x remains null.
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby jobriath » Wed Apr 11, 2007 7:54 pm UTC

Won't work in C++, C or Java. In Java, an int can't take a null, and in /C(\+\+)?/ I think null/NULL is defined as 0--indistinguishable from the value 0.

Might work in Java 5 if you change it to an Integer. Then, it'd be null unless it was assigned, and maybe Integer x = number would be legal because of the autowidening/boxing. I haven't played around enough to know off top of head.

Eitherway, thanks for starting this thread: that two line toggle thing is handy!
jobriath
 
Posts: 210
Joined: Wed Apr 04, 2007 2:11 pm UTC
Location: The North

Postby fynch » Thu Apr 12, 2007 1:45 am UTC

warriorness wrote:
fynch wrote:
Code: Select all
//some code
//some more




Is there a magical slash there that I'm missing? The goal of the puzzle is to find a way to avoid having to do this - i.e. typing/deleting four characters each time you want to toggle a line of code.

Delete a slash on the line you want:
Code: Select all
/ some code you want
//some more

or vice versa
Code: Select all
// some code
/ some more that you want
User avatar
fynch
 
Posts: 69
Joined: Tue Mar 27, 2007 6:30 pm UTC

Postby OmenPigeon » Thu Apr 12, 2007 2:11 am UTC

fynch wrote:
Code: Select all
// some code
/ some more that you want


That... doesn't compile. A single '/' before a line of code makes gcc say "syntax error before '/' token." Hence all the trickery.
As long as I am alive and well I will continue to feel strongly about prose style, to love the surface of the earth, and to take pleasure in scraps of useless information.
~ George Orwell
User avatar
OmenPigeon
Peddler of Gossamer Lies
 
Posts: 673
Joined: Mon Sep 25, 2006 6:08 am UTC

Postby notzeb » Thu Apr 12, 2007 5:04 am UTC

I do this all the time! I'm currently trying to construct an esolang entirely out of interacting comments. Still wondering how to get input (then again, who needs input?)

My most common method (uses two chars to toggle, but is much easier to code, and also fits in the middle of a for loop!)
Code: Select all
/* stuff /* */  // (without)
 stuff /* */    // (with)


Edit: yes, I am aware that this doesn't solve the problem. It solves a different problem. I most sincerely apologize.
Zµ«V­jÕ«ZµjÖ­Zµ«VµjÕ­ZµkV­ZÕ«VµjÖ­Zµ«V­jÕ«ZµjÖ­ZÕ«VµjÕ­ZµkV­ZÕ«VµjÖ­Zµ«V­jÕ«ZµjÖ­ZÕ«VµjÕ­ZµkV­ZÕ«ZµjÖ­Zµ«V­jÕ«ZµjÖ­ZÕ«VµjÕ­Z
User avatar
notzeb
Without Warning
 
Posts: 571
Joined: Thu Mar 08, 2007 5:44 am UTC
Location: a series of tubes

Postby tendays » Thu Apr 12, 2007 2:23 pm UTC

EvanED wrote:
warriorness wrote:Strange. As a Java programmer, I've only worked with javac, not GCC, so embedded comment markers are legal in this puzzle :D


Embedded comment markers are legal in C too, but many compilers will throw a warning. For instance, Apple's old MPW C compiler said "Call me paranoid but finding `/*' inside this comment makes me suspicious".

(The idea of course being that if you have /* a /* b */ c */, the code at c is not commented out while this is very possibly not what you meant.)


Embedded /*-*/ comment markers are valid neither in java nor in C! (I just tested both javac and gcc). Read carefully the definition:

warriorness wrote:Multi-line comments, denoted at the beginning by "/* " and at the end by " */". Everything in between is ignored.


More specifically, the first "*/" ends a comment started with "/*"

So
Code: Select all
/* hello /* world */

is a valid comment but
Code: Select all
/* hello /* nested */ world */

is not (as "world */" is outside the comment, and doesn't make sense on its own)

EDIT: I just realised that actually was what you meant ... never mind, then.
Last edited by tendays on Thu Apr 12, 2007 2:43 pm UTC, edited 1 time in total.
User avatar
tendays
 
Posts: 957
Joined: Sat Feb 17, 2007 6:21 pm UTC
Location: HCMC

Postby genevabeagle » Thu Apr 12, 2007 2:37 pm UTC

While I'm fairly convinced that comment-style switching (as described) isn't possible for more than two segments of code, I don't have time right now to write it up. As it is, the margins are too small.

Anyway, the two-block switch can be semantically fixed by changing the last /* to //, like this:
Code: Select all
//*
99 red balloons
/*/
99 luftballoons
// */
genevabeagle
 
Posts: 42
Joined: Tue Dec 12, 2006 11:32 pm UTC

Postby BlackNine » Thu Apr 12, 2007 2:42 pm UTC

This reminds me of the CSS comment hacks :P

Code: Select all
/* \*/
something_for_non_ie5mac_browsers
/* */
/*\*//*/
ie5mac_only
/**/
/* \*/
something_for_non_ns4win_browsers
/* */
/*/*//*/
ns4win_only
/**/
BlackNine
 
Posts: 12
Joined: Thu Apr 12, 2007 2:30 pm UTC

Postby ekim » Thu Apr 12, 2007 10:48 pm UTC

Code: Select all
//              n-block 2-character toggle
//*
//*/  x=0;  //  remove the first forward-slash on the line
//*/  x=1;  //  immediately preceding the one we want to execute
//*/  x=2;  //  (gcc -Wall -pedantic only complains about the c++ style
//    ...        commenting)

  code_block_ary[x]();  //now exec the code-block corresponding to x


So this should be functional for two characters (add a slash to return it to the neutral state above, and get rid of whichever corresponds to the block you want) -- but I'm pretty sure it's doable with just one-character changes.
ekim
 
Posts: 82
Joined: Mon Dec 18, 2006 12:40 pm UTC
Location: Seattle

Postby ekim » Fri Apr 13, 2007 5:40 am UTC

ok. one character changes to move from any code block to any other.
(among three code blocks)

Code: Select all
//  this is what I'm thinking:
//  +1    0    -1      value of x right before multiplying by y and adding 1
//  110  101  011      given this on/off pattern of the three commented segments
//    100   001  010   (0 is commented, 1 is uncommented)
//     -1    +1   0
//we start with setup 100

  x=0; y=1;
  /*      <--  this first one (code next line) is 'uncommented' in the init'l setup
  //*/ x+=1; y*=-1;
  //*     <--  toggle the first char of this line to toggle 'bit 2'
  //*/ y*=-1;
  //*     <--  and here for bit 3
  //*/ x-=1; y*=-1;

  x=x*y+1;

  function_block_ary[x]();

ekim
 
Posts: 82
Joined: Mon Dec 18, 2006 12:40 pm UTC
Location: Seattle


Return to Logic Puzzles

Who is online

Users browsing this forum: No registered users and 2 guests