News for hackers. Stuff that matters.
Re:Printing 1 to 1000 without loop or conditionals by tptacek on Sunday January 02, 2011 @11:46PM (#2062116)
The ctor trick is genius. Compile-time loops (recursion counts as one) seems like a cheat. The dispatch table keyed on (i < 1000) is a straight-up cheat, as (obviously) is using the ternary or short-circuiting.
Re:Printing 1 to 1000 without loop or conditionals by juiceandjuice on Monday January 03, 2011 @12:31AM (#2062220)
The task never said it had to stop at 1000.

    void f(int n){
       printf("%d\n",n);
       f(n+1);
    }

    int main(){
       f(1);
    }
Re:Printing 1 to 1000 without loop or conditionals by eps on Monday January 03, 2011 @01:26AM (#2062340)
Nor did it say all numbers from 1 to 1000:

  int main()
  {
    printf("13 47\n");
    return 0;
  }
:D
Re:Printing 1 to 1000 without loop or conditionals by jws on Monday January 03, 2011 @01:03AM (#2062290)
My favorite uses a divide by zero to terminate an otherwise infinite recursion.
Re:Printing 1 to 1000 without loop or conditionals by juiceandjuice on Monday January 03, 2011 @01:47AM (#2062398)
Actually the one that uses function pointers is by far the best, simplest way, and it doesn't segfault. Since x/1000 will always be floor(x/1000), once it reaches 1000 it calls nothing and exits cleanly.

This one (not mine):

    #include <stdio.h>

    void nothing(int);
    void next(int);
    void (*dispatch[2])(int) = {next, nothing};

    void nothing(int x) { }
    void next(int x)
    {
        printf("%i\n", x);
        dispatch[x/1000](x+1);
    }

    int main()
    {
        next(1);
        return 0;
    }
Re:Printing 1 to 1000 without loop or conditionals by tzs on Monday January 03, 2011 @02:37AM (#2062501)
> The dispatch table keyed on (i < 1000) is a straight-up cheat

How so?

Re:Printing 1 to 1000 without loop or conditionals by tptacek on Monday January 03, 2011 @02:51AM (#2062523)
It's an if statement written differently.
Re:Printing 1 to 1000 without loop or conditionals by mfukar on Monday January 03, 2011 @05:23AM (#2062744)
If you want to get into language-lawyer mode, expressions are most certainly not conditional statements [in C]. But what's the use of arguing? "More gimmicky" answers don't count into solving a gimmicky problem? Who even keeps score?

edit: clarification, in true language-lawyer fashion.

Re:Printing 1 to 1000 without loop or conditionals by tptacek on Monday January 03, 2011 @01:45PM (#2064155)
The preprocessor hack does not compile to code that would decompile back to an if() statement. The i/1000 code does not compile to code that would decompile back to an if() statement. The ctor hack does not compile to code that would decompile back to a loop and an if statement. The i<1000 code, on the other hand, does compile down to code that decompiles back up to an if() statement; that's because it's just a compact way of writing an if() statement.

I'm not being a "language lawyer".

Re:Printing 1 to 1000 without loop or conditionals by mfukar on Monday January 03, 2011 @02:08PM (#2064262)
Well, neither C nor C++ are assembly, and vice versa. That's my usual response to this kind of "arguments", in every such question; boy do they come up a lot.

Regardless, I'll repeat it; maybe it'll stick this time:

  i < 0
is a conditional expression,

  if (i < 0) {}
is a conditional statement.

Also, while the above could be (mis)taken for C code;

  cmp eax, 0
  jl  foo
this one surely wouldn't.

And at this point I'll stop beating this particular horse.

Re:Printing 1 to 1000 without loop or conditionals by tzs on Monday January 03, 2011 @07:22AM (#2062911)
How about if it was keyed on i/1000 instead of i<1000? Would that still be a hidden conditional?
Re:Printing 1 to 1000 without loop or conditionals by tptacek on Monday January 03, 2011 @08:31AM (#2063057)
No. Think about the generated assembly. One has a conditional branch, the other doesn't.
Re:Printing 1 to 1000 without loop or conditionals by Raphael_Amiard on Monday January 03, 2011 @06:35AM (#2062847)
I strongly disagree on the ctor trick. While it's certainly ingenious, you're just reusing a loop, be it one implemented in the C++ runtime. It's little more than a glorified map.
Re:Printing 1 to 1000 without loop or conditionals by mfukar on Monday January 03, 2011 @06:45AM (#2062863)
I swear to god, I was tired of this argument before I even finished reading it. If you take your example to the extreme, you should consider that a processor also operates in a loop. Maybe every solution is a glorified series of iterations.

Nothing personal, but your argument is always used in these situations, for no good reason. Please learn where to draw the line.

Re:Printing 1 to 1000 without loop or conditionals by Raphael_Amiard on Monday January 03, 2011 @02:59PM (#2064450)
> I swear to god, I was tired of this argument before I even finished reading it

I'm sorry if i tire you, but i think you misinterpreted what i said. Every solution will boil down to a loop, necessarily, so my point is it's useless to make distinctions like , "this solution is a bit cheating, but this one isn't". As long as it respects the requirements, eg no loop or conditional statements, it is no cheating.

> Nothing personal, but your argument is always used in these situations

I realize my original comment was oversimplifying things, but i fail to see which situations you are talking about. My argument, or what i originally wanted it to be, is that the line you draw between map and the constructor trick is extremely subjective. In such case, the only way to distinguish valid or invalid solutions is to refer to the original requirements.

> Please learn where to draw the line.

And please keep your condescending attitude to yourself. I perfectly know where to draw the line, and i still consider the limit tptacek made to be ridiculous. If you have any real argument to oppose me instead of sheer oversimplification and exaggeration, please proceed, but from what i see you just needed to vent, and in a rather unpleasant manner.

Re:Printing 1 to 1000 without loop or conditionals by mfukar on Monday January 03, 2011 @03:11PM (#2064496)
The way you phrased your original post gave me the impression you were dismissing the constructor solution for the same reason tptacek dismissed the rest. I certainly agree with your point, as you may have read elsewhere in the thread.

Other than that, I don't assume a patronizing attitude when I say that arbitrarily labeling solutions to such problems as "cheating" is an infuriatingly useless argument over semantics, and nothing more. Particularly so when such questions are used in the context of a job interview; how would you feel if an interviewer wrongly rejected your answer based on what he read on SO, and whose answer she trusted more?

PS. The situations I was referring to is the myriad of such questions on SO; a problem with absurd constraints imposed. They're fun, until 5 people come along and decide going around labeling answers as "cheating". The Stack Overflow meta site has quite a few comments on that phenomenon.

Re:Printing 1 to 1000 without loop or conditionals by Raphael_Amiard on Monday January 03, 2011 @03:20PM (#2064528)
Okay well, so i feel like we're in a case of violent agreement, and it's probably my fault because i didn't take the time to write a proper comment in the first time.

My apologies.

Re:Printing 1 to 1000 without loop or conditionals by mfukar on Monday January 03, 2011 @03:27PM (#2064546)
Mine as well, I guess my comment was over the line.