News for hackers. Stuff that matters.
Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by ggurgone on Thursday November 19, 2015 @02:15PM (#10596869)
A girl on irc once asked if there is a way to solve FizzBuzz with just one comparison. Yours is indeed one and it is pretty clever :) Here is another one https://gist.github.com/giuseppeg/5939177
Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by cronin101 on Thursday November 19, 2015 @03:03PM (#10597175)
FizzBuzz from 1 to N (supplied as command-line argument) and exiting gracefully without using loops, conditionals, or segfaults - in C:

    #include <stdio.h>
    #include <stdlib.h>
    
    int i(const int i) { printf("%d\n", i); return i; }
    int f(const int i) { printf("Fizz\n"); return i; }
    int b(const int i) { printf("Buzz\n"); return i; }
    int fb(const int i) { printf("FizzBuzz\n"); return i; }
    int (* PC[15])(int i) = { i,i,f,i,b,f,i,i,f,b,i,f,i,i,fb }; // Printer Cycle
    
    int go(const int arg) {
        return PC[(arg - 1) % 15](arg) + 1;
    }
    
    int stop(const int arg) {
        PC[(arg - 1) % 15](arg);
        exit(0);
    }
    
    int (* LC[2]) (int index) = { go, stop }; // Logic Cycle
    
    void recurse(const int i, const int kill_num) {
        recurse(LC[!(i % kill_num)](i), kill_num);
    }
    
    int main(const int argc, const char *argv[]) {
        recurse(1, atoi(argv[1]));
    }
Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by ilurk on Thursday November 19, 2015 @04:00PM (#10597625)
Could you explain these lines:

> int (* PC[15])(int i) = { i,i,f,i,b,f,i,i,f,b,i,f,i,i,fb }; // Printer Cycle

> int (* LC[2]) (int index) = { go, stop }; // Logic Cycle

I can see they behave as functions, but I don't understand what exactly are they or the logic that makes then work.

Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by cronin101 on Thursday November 19, 2015 @04:14PM (#10597730)
I can try!

They are both an array of pointers to functions.

Go(int i) is a function that finds the correct "printer" for a number, i, within the cycle of 15. `Go` then calls the printer function with the argument and prints the corresponding line.

Stop(int i) has similar side-effect but exits immediately after.

Since all of the printers return the value of "i", you can recursively call them in cycle-sequence through `go` until the return value lines up with the "kill number" and causes `stop` to be called instead (this works as `i % n > 0` for all numbers below n and you can force this to be zero, and thus index the first element of the array, by negating it).

Sorry for the scatterbrained explanation, the code wasn't designed to be particularly comprehensible!

Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by adrae5df on Thursday November 19, 2015 @03:29PM (#10597388)
So at what input value does it overflow the stack on your machine?
Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by cronin101 on Thursday November 19, 2015 @04:07PM (#10597668)
Never on -O3 / Oz :-)

(Obviously it transforms it to a loop, so it sorta breaks the rules at compile time)

Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by jacquesm on Thursday November 19, 2015 @03:33PM (#10597411)
261876 here.

'0' and no number input also crash it.

Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by adrae5df on Thursday November 19, 2015 @03:40PM (#10597471)
Believe it or not its a weird palindrome for me: 65059
Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by jacquesm on Thursday November 19, 2015 @04:09PM (#10597680)
First I thought 'that's a palindrome?', then I saw what you meant. Is it repeatable?
Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by jacquesm on Thursday November 19, 2015 @03:30PM (#10597393)
Neat!
Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by eCa on Thursday November 19, 2015 @03:25PM (#10597352)
Yes, a Perl one-liner:

    perl -E 'map { say } map { { 3 => "Fizz", 5 => "Buzz", 15 => "FizzBuzz" }->{ sub { my $val = shift; (grep { $val % $_ == 0 } (3, 5, 15))[-1] }->($_) } || $_ } (1..100)'
Or a little more readable:

    map { say }
    map {
        { 3 => "Fizz", 5 => "Buzz", 15 => "FizzBuzz" }->{
            sub {
                my $val = shift;
                (grep { $val % $_ == 0 } (3, 5, 15))[-1]
            }->($_)
        } || $_ 
    } (1..100)
Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by adrae5df on Thursday November 19, 2015 @02:22PM (#10596912)
There are two conditionals in that code.
Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by ggurgone on Thursday November 19, 2015 @02:27PM (#10596944)
indeed :) it should be:

words[c] || i

I focused on the bitwise thing and I overlooked that part

Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by adrae5df on Thursday November 19, 2015 @02:32PM (#10596971)
|| is a conditional.

Is first expression true? If it isn't evaluate the second expression.

Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by ggurgone on Thursday November 19, 2015 @02:38PM (#10597007)
sure, what other conditional are you talking about? the for condition?
Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by adrae5df on Thursday November 19, 2015 @02:42PM (#10597029)
Second expression in the for loop and ||, both behave as conditionals, i.e. are equivalent to an if statement and the generated instruction will be a branch.

At least it would in C. Of course I'm ignoring the fact that JS is interpreted as will cause a lot of additional bloat, but were not counting that.

Re:Oh, Go Ahead, Overthink FizzBuzz (2008) by ggurgone on Thursday November 19, 2015 @02:52PM (#10597097)
No doubt about it. The question was about avoiding the if/else within the for loop body.

In js the for could be replaced with an array of 100 elems and a forEach:

Array.apply(null, { length: 100 }).forEach(function (_, i) { })