Giter Site home page Giter Site logo

fizzbuzz's Introduction

fizzbuzz

Fizzbuzz is a fascinating meta-phenomenon. As a quick test to see if a job candidate can code at all, it's a rapid-fire way to weed out the estimated thirty to forty percent of people whose résumés look impressive but can't actually program.1 It's an extremely elementary problem, solvable with nothing more than arithmetic and basic control structures, and it really doesn't serve the same purpose as asking programmers to write a binary search. That “other” classic experiment (Jon Bentley's) was all about boundary conditions and clear thinking and simulating code in your head. Fizzbuzz is a different animal. It's a “you must be this tall to ride” gate at the entrance to the interview; deal with it. Some people misunderstand it and get upset.

I am always surprised when I run across people who have never heard of it. Of course, the boss at my last job had never heard of GitHub.

Watch this!

I have watched skilled programmers vie to out-do each other on this seemingly trivial problem. Asked sheepishly by an interviewer to code up a solution ("please don't be offended"), any of them could do it straightforwardly in a minute. Yet on a forum like HN, they put forth elegant, beautiful, bizarre, or terrifying implementations whose only purpose is to insert joy into the soul of a programmer. Nobody thinks it's a serious demonstration of ability; it's more like doing crossword puzzles. There are a over thousand fizzbuzz repositories on GitHub.2 I haven't solved it in Haskell, or Verilog, or by running a DFA over a circularly linked list, or by bit-twiddling simulated flip-flops in C, but here is my solution, one that follows good software engineering practice. It's extensible, but mindful of YAGNI. It compiles with no warnings. It has documentation, and tests, and a build procedure. "My God — it even has a watermark license."

There are all kinds of other ways I can think of to do this, using pointer aliasing side effects in C, self-modifying code in machine language, maybe using the bit mask counting trick in HAKMEM 169....

My solution

A good solution to the problem is to define a function that returns the product of the modular residues, then print the number if that function returns anything other than zero.

int fizzbuzz(int i) {
	if (!(i % 3)) printf("fizz");
	if (!(i % 5)) printf("buzz");
	return i%3 * i%5;
}

int n;

for (n=1; n<=100; n++) {
	if (fizzbuzz(n)) printf ("%d", n);
	printf ("\n");
}

I have not seen this solution published elsewhere.

Enterprise Edition

Good software engineering practice defends against failure by checking all return codes, using appropriate data types (such as unsigned and long to enforce limits and avoid overflows), employing assertions to validate preconditions and postconditions, and providing expected features such as help, undo, or mobile versions.

Compare the sizes of fizzbuzz.c with fizzbuzz_enterprise.c (and fizzbuzz_enterprise.h to get an idea of how much larger the resulting source code is.

References

  1. Jeff Atwood. "Why Can't Programmers...Program?". The Coding Horror blog (2007). http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html

  2. Dave Fecak. "Hiring Indicators, OSS, and the Value of GitHub". Job Tips for Geeks blog (2013). http://jobtipsforgeeks.com/2013/11/19/githubhiring/

fizzbuzz's People

Contributors

jloughry avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.