FizzBuzz: the Programmer's Stairway to Heaven

“Real programmers adapt to the workplace to a certain extent, and are comfortable working with languages that aren’t necessarily their ultimate favorite, in order to Get The Job Done ™.”

I agree, 11 years here, and have had to work on many VB, MTS COM+, perl, ATG Dynamo migrations. Not fun stuff. My problem was that this was their first shot off the hip. If it was C#, C++, C, Java even Python or Ruby but they chose WSH and VBA?!?

Do “Real Programmers™” when they choose to write something choose the most hideous language of choice?

Play some golf! a href="http://golf.shinh.org/p.rb?FizzBuzz"http://golf.shinh.org/p.rb?FizzBuzz/a

“Guitarpocalypse” Ha! Classic.

Hey, I noticed you prepended “www.” to my domain. Touche my friend. Touche.

On a side topic

At least two posts yesterday mentioned that the swap using arithmetic operations a=a+b; b=a-b; a=a-b; fails in case of overflow.

This is incorrect, it is a property of 2’s complement arithmetic that if a sequence of additions (that include subtractions) has a final result that ends up in the representation range, then the result will be correct irrespective of the presence of intermediary underflow or overflow. As most processors use 2’s complement representation, the swap using arithmetic operations mostly works.

Strange but true, you can use the program below to convince yourself that it is indeed working in the case of a swap, irrespective of overflow or underflow (done on short so that the loops finish in a reasonable time).

#include "limits.h"

int main() {
  
        int i ;
        int j ;
  short int a, aswapped ;
  short int b, bswapped ;

  for (i=SHRT_MIN; i = SHRT_MAX ; i++) {
    for (j=SHRT_MIN; j= SHRT_MAX ; j++) {
      a = i ; b = j ; aswapped = b ; bswapped = a ;
      a = a+b ; b = a-b ; a = a-b ;
      if ((aswapped != a) || (bswapped != b)) {
        printf ("Swap failed for %d %d\n", aswapped, bswapped) ;
      }
    }
  }
  return 1 ;
}

Speaking for myself, I went off and implemented FizzBuzz after I read your previous post (but before I’d read the comments), and was satisfied that it worked correctly (I didn’t post my implementation as a comment). That’s partly because it looked like a fun little challenge that wouldn’t take long, and partly because I think that this would be a good litmus test when I’m recruiting, and I want to be fair about it. Basically, I won’t ask other people to do things that I’m not willing to do myself, so if I’m going to say to someone “You took 15 minutes to work it out? You suck!” then I want to be sure that it’s a fair assessment by trying it myself first. In the same way, I’ve been doing some certifications that are way below my current skill level (e.g. ECDL) just so that I know what they measure, and how much attention I should pay to them if I see them on a CV.

I’d say no - there’s plenty of other (admittedly sub-optimal) ways to do it. I think the point is that they can come up with something that works. You can always optimize later. :)[/quote]

Whatcha talking about! Modular arithmetic is slow slow slow! Your counting method is much much better!

I’d put some serious thought into improving it though, and I reckon I’ve come up with the required improvements to optimize further…

StringBuilder line = new StringBuilder();
StringBuilder lines = new StringBuilder();
for (uint i = 1, fizz = 1, buzz = 3; i = 100; ++i, --fizz, --buzz)
{
    if (fizz  2)
        line.Append("Fizz");
    fizz = Math.Min(fizz, 2);

    if (buzz  4)
        line.Append("Buzz");
    buzz = Math.Min(buzz, 4);

    if (line.Length == 0)
        line.Append(i);

    lines.AppendLine(line.ToString());
    line.Remove(0, line.Length);
}
Console.WriteLine(lines);

(and come on, when you say you can’t play Stairway, what’s the first thing someone’s gonna do…)

{grin}

Did anybody write this in Assembler? :slight_smile:

Dana: Forget the TI-83! Your post has inspired me to write the HP48 version.

 1 100 FOR i i 5 MOD i 3 MOD IF DUP2 AND THEN DROP2 i -STR ELSE "" "Fizz" IFTE SWAP "" "Buzz" IFTE + END NEXT 

One of these days I should just write an RPL compiler for x86…

I too wrote a solution, though i didn’t post it. I think John C. Kirk hit it on the head – I wasn’t trying to see if I could solve the problem, it was obvious. I wanted to see how quickly I could do it myself. Out of the people who posted solutions, I bet most of them timed themselves. It’s not the solution that mattered, it was the race.

And then following the race, it’s a matter of elegance – how short can I make it? How fast can I make it? Does my favorite pet language beat the others?

Pete, regarding hardcoded constants:

That’s funny–I saw some similar complaints in the original posting, and my thought was that I’d be more wary of these silly buggers complaining about hardcoded constants.

That’s cargo cult programming: following rules that originally had a good purpopose–“avoid hardcoded constants”–without understanding why they are good rules, what problems they are intended to avoid, or when they do or don’t apply. Taken to extremes, it’s responsible for a good chunk of thedailywtf’s horrors.

In this case, the problem description has the 3 and 5 right in it. Doing otherwise in the code that solves the problem is just obfuscation. It doesn’t help with maintenance, because you don’t yet know what direction the problem is going to go. Will the numbers change? Will a third case get added? Will we need to add support for output in French (“Le Fizzbuzz”) and UK English (“Fizzbouzz”)? Keep the code as simple as possible and maintenance is always easier.

If you use fizzbuzz at a litmus test for PHP programmers, look for something like

while($a101){ echo (($a%3)!=0) ? $a : "Fizz"; $a++;}

vrs the 1,000 responses of looped if/when/while/for stuff that was posted.

This will actually play a part when I am looking at future coders, though we generally ask for site examples and extensive code samples as well as references.

:slight_smile:

sry your Fizz has no Buzz

That’s cargo cult programming: following rules that originally had a good purpopose–“avoid hardcoded constants”–without understanding why they are good rules

Avoiding hard coded constants is almost always a good idea, as it makes your code more self-documenting and allows easier changes when the constant is used in multiple places (as many FizzBuzz solutions do with three special cases and two constants). If you think things like this rule and making code as readable as possible are bad ideas then you probably haven’t had the same nightmare experiences working with other people’s code that I have.

I work with embedded systems where people frequently litter the code with (hex) constants to set certain in registers (which can do anything in hardware – no way to tell from a number). Even with comments (which may no longer be in sync with the code) it’s virtually impossible to tell what they’re trying to accomplish just by looking at it – I spend literally hours each week pouring over data sheets for different devices trying to work out what each arcane constant is for.

Following such a rule is a (very) minor inconvenience to you, but can be a HUGE assistance to your colleagues (even the ones you will never meet).

Based on the number of incorrect programs posted to the original article, I’d have to say the FizzBuzz test works. It’s a shame such a test is needed.

Not surprised about the comments in the last one. I myself had put it all together in C++ in my head moments after reading the problem.

Didn’t read most comments on the other pages, but was I the only one to question the wording of the question in that output should perhaps be(separate lines for readability, although the problem never mentioned newlines:

1
2
Fizz
4
5Buzz
Fizz
...
14
FizzFizzBuzz
16

Oddly, English is harder to understand and interpret than the code it asks for. Personally, I’m more satisfied with my answer than the most common one. But who wouldn’t like their own answer more?

Is a test like FizzBuzz necessary? I’m a Junior in college working for dual majors in CompSci and Information Systems, and I’m disappointed with the quality of the students I see in my classes, especially my programming classes. Problems that “just make sense” to me seem like they’re hard to grasp by other CS majors at my school. Am I tooting my own horn? I hope not, but it seems that where computers were once an area that only enthusiasts pursued there is now a lot of people going into CS/IS that are just looking for what they’re told is a solid career path.

In a class of 30 of my peers with a CS major at my school, I would expect maybe 4 could solve this problem in under 5 minutes. I’m sure most of you put it together in your heads in well under a minute, with maybe a minute to write it out. When will “teaching students to be proficient programmers” actually mean a programmer that can, as Jeff so elegantly put it, “program”.

/Warning "Shameless Self Plug located below"
I’m finishing my Junior year of college at Clarion University of Pennsylvania in mid-May. I am majoring in both Computer Science and Information Systems, with programming being my hobby and focus for career choice. If anyone’s company (or one that they know of) is looking for any summer interns or temporary summertime entry-level programmers for any reason, please contact me. I’m in PA, but have no problem relocating for the summer, especially if the job would pay enough to afford a place to live and some food. Anyone with any opportunities or interested in a resume or any information please feel free to contact me at walwal31(at)hotmail(dot)com.

Shameless? Yes. But where else will I find a better group of programmers and locations that may actually be part of a company looking for summer work?

You either have it or you don’t.

If you have it you will make a great programmer if you stick with it.

If you don’t you had better find something else to do besides making nightmares for people like me…

Real men write it in brainf**k anyway…

(Not quite to spec, does 00-99, but that’s a documentation issue).

++++++++++++++
+++[++++++++++-]+++++++[+++++++
++++++++-]++[+++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++-]++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
++[++-]+++++++++++++++++++++++++
++++++++[++-]++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++
+++++++++++++++++
+++++++++[++++++++-]
++++++[[-]++++++++++
[-]++++++[++++++++-][
[-]++
+[-][-][++-][+-][-]++
+[--][-]+[[-][-]][[-]
[-]....[-]]
[-][-][++-][+-][-]++++
+[--][-]+[[-][-]][[-][-
]....[-]]
[.
.[-]]
+.-]+-]

It appears that my logic has failed. I was just in the shower when it occurred to me that I faulted.

Multiples of 15, under my above interpretation of the original problem, should read “FizzBuzzFizzBuzz”.

I guess I failed :frowning:

I don’t believe any of this. If this is true why am I working so hard. You mean I can spend the day cutting out paper dolls of code and I’d be as good as the guy next to me? (I always suspected I was better than you, Carl.)

Is there a term for people who compulsively answer rhetorical questions?

FizzBuzz was easy, the real sad truth is most programmers are not tech savvy enough to send me $10 via PayPal.