59 comments

[ 4.9 ms ] story [ 127 ms ] thread
The EnterpriseQualityCoding version of FizzBuzz[0] is the ultimate (hilarious) version of this. Though, there is still some room for development of node, js, etc versions.

[0] https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...

I don't know. Java seems like the one true language for the Enterprise version of FizzBuzz.
The Node version could be pretty great anyway. Huge list of modules required, multiple implementations of the same basic thing in use, separate modules for "fizz" and "buzz". And MongoDB used for something, for no good reason.
"fizz", "buzz", and "fizzbuzz" should be separate microservices each with their own failover and redundancy systems.
Many years ago I stumbled on a programming golf contest on the old Something Awful forum. The challenge was to convert a byte string to a hex dump. I wrote it as a distributed JNI system with CORBA based RPC. The boilerplate stuff alone was like 500 lines. This is what I did for fun in high school. I wasted my youth.
Agreed, but it needs a designation besides Enterprise. My mind is drawing a blank, though.
You can write Java in any language.
I shudder to agree. Doing a code review from someone who wrote an entire program in Java formatted Python was one of the low points of my career.

Trees upon trees of nearly empty directories, one sparse class per file (with the requisite getters and setters), factories to make factories... the horror.

I created a xxxxFactoryFactory in a recent project. Partly for practical reasons and partly for satire.
Wow. Just... wow. That is more amazing than most of the code I see on a daily basis.
I dont know needs more tests :)
No factory factories. 0/10, next application please.
It doesn't look like it was implemented following TDD.

No recruiters would accept it.

I've tried overthinking FizzBuzz myself, but from the architectural point of view: https://medium.com/@sbichenko/the-wrong-fizzbuzz-81cb8e67ef4...
I agree that string concatenation is the optimal solution and always take the chance to bring out a properly generic solution.

    let fbg s l u=unlines[case concat[w|(w,c)<-s,mod i c==0]of[]->show i;m->m|i<-[l..u]]
    let fizzbuzz = fbg [("Fizz",3),("Buzz",5)] 1 100
    main = putStrLn fizzbuzz
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
There are two conditionals in that code.
indeed :) it should be:

words[c] || i

I focused on the bitwise thing and I overlooked that part

|| is a conditional.

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

sure, what other conditional are you talking about? the for condition?
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.

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) { })

(comment deleted)
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]));
    }
So at what input value does it overflow the stack on your machine?
261876 here.

'0' and no number input also crash it.

Believe it or not its a weird palindrome for me: 65059
First I thought 'that's a palindrome?', then I saw what you meant. Is it repeatable?
Never on -O3 / Oz :-)

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

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.

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!

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)
im gonna learn the random number seed by heart and put it as an anwser every time im asked to do fizz buzz from now on
You're going to be so in for a surprise when the RNG used on the machine you're showing off your skills on has a different underlying implementation than the one that you suspect!
I worked for a while for an IT services company. For our entry level tech support position, I put together a quick interview document that we emailed applicants, asking them 4 questions. They had to help someone fix their printer via email, fix their email client, write a powershell script to copy files from one directory to another, and solve FizzBuzz.

We honestly didn't care if they blatantly plagiarized the answers (especially on the last two), because the point was the right answer as much to see if they knew enough vocabulary to know how to Google the question.

Out of 30 responses, including recent college graduates with good GPAs, people claiming 20+ years of IT experience, owning their own IT consulting firm, etc, not one of them got all four right. Some even said, "I don't know how to solve this" on FizzBuzz.

The person who actually got them all correct? My wife, an ESL teacher who I gave the questions as a control, and told her to not spend more than 15 minutes on them.

Anyone who overthinks FizzBuzz is an automatic hire in my book. (yes, I know about http://www.joelonsoftware.com/items/2005/01/27.html )

In fairness, someone with 20+ years of IT experience could very legitimately not have needed to do any significant amount programming. Maybe they built servers all day.
Ok but they were emailed these questions (in what appears to be a take-home format).

1) Know how to use the internet 2) Know how to problem solve by obtaining information from external sources.

I'd rather answer "no clue" in an interview than fake knowledge and wind up in a job where I'm lacking basic required skills.
In general, I would agree with you. In that particular case (which I remember because the person with 20+ years of experience was recommended by a client), that person claimed they had written their own CRM system.

(Way, way off topic, but are you the ceejayoz who used to be a Homeworld webmaster? I spent a month or so of my life on Homeworld in late 2000, fun times.)

That definitely changes things.

And yes, I am. :-)

Yeah, we only gave that candidate the document because we were had just come up with it, we figured he would completely ace it. When he came back with really bad answers for all of the questions (including, "I've never heard of Powershell and I'm not sure how I would use it to do this" in 2012 with claiming the last 10 years as a Windows admin), my boss called me into his office and we just stared at the answers in shock.

Awesome. I was a bigger Total Annihilation fan/community member than I was for Homeworld, but still, it tickles me when I see names I recognize from gaming being 'real people' somewhere on the internet years later.

What this article misses is my favourite bit: maintainability and extensibility.

If this were real code two things would happen;

1. Someone would refactor the `fizzbuzz_word(n)` method and this would all break

2. Your PM would ask for a new feature "multiples of 7 should be said 'Wuzz'"

So that 2 minute test becomes a 10 minute venture into real world problem solving.

Ok, now we need a ridiculously simple version in C, that will probably fit in 64 characters and require a page of explanation :-)
I like the fact that the information theory adds up. To specify 30 bits he used a 26 bit number and then specified one of 24 permutaions, and this is just right because

26 + log(24) = 26 + 4.58... > 30

Even if the number he specifies has leading zeroes, it's still one out of the set of 32 bit numbers.
Having leading zeros does make the length of the source code itself smaller though.
And this suggests a path for lowering the magic number. We can lower the number of bits needed by noting that the required outputs for 8 through 14 (mod 15) are the same as the outputs for 1 through 7 (mod 15), only reversed.
This is awesome. And funny. I can imagine how this would piss off people interviewing you who think they are smart.

I think I will use it.