Why Can't Programmers... Read?

UPDATE: This post was meant to be mostly tongue in cheek. I love programmers. Some of my best friends are programmers. Also, to be fair to the FizzBuzzers, the spec for this question is flawed, as are all specs as I point out in Why Can’t Spec Writers Write Specs.

You gotta love CodingHorror.com. Once again, Jeff Atwood writes a great post entitled Why Can’t Programmers.. Program? But in this case, the best part is in the comments. Let me explain.

Jeff writes about a simple "Fizz Buzz" test given by Reginald Braithwaite. Here’s the functional spec.

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Mind you, this is not a trick question. It is as easy as it sounds. Or it should be. According to Reginald Braithwaite, the majority of comp sci graduates can’t do it.

Naturally, the very first comment to Jeff’s post solves this simple problem. But read further and be amazed at how many developers get it wrong in such a public forum!

The problem may not be that these programmers can’t program. It’s that they can’t read!

I think reading comprehension must be at an all-time low among graduating programmers. Perhaps it’s time to bring back more word problems in our curriculum. Of course we’ve all seen this lack of reading comprehension in comments to our blog posts, haven’t we? As in when someone completely argues against a misinterpretation of your post.

It never fails to amaze me at how some companies I’ve worked with (or at) praise developers who are fast, even if they do the wrong thing because they failed to understand the spec or include so many bugs as to have made the spec irrelevant.

At least he’s fast!

Indeed!

So what’s worse? A programmer who can’t write code, or a programmer who can, but can’t follow a spec?

What others have said

Requesting Gravatar... Haacked Feb 27, 2007 10:24 AM
# re: Why Can't Programmers... Read?
I should add, apparently programmers can't TEST either. It's a really simple test to see if your code works. But then again, if you can't read the spec, how do you know your output is wrong?
Requesting Gravatar... Rob Conery Feb 27, 2007 10:41 AM
# re: Why Can't Programmers... Read?
I think most programmers have ADD, have too little concentration juice to go around, and egos that like to strut naked around the room with painted chests. Not a good mix.

I'm still wondering if you're going to give us the answer?
Requesting Gravatar... Rob Conery Feb 27, 2007 10:55 AM
# re: Why Can't Programmers... Read?
I just went back there and had to wait 30 seconds for the page to load. I honestly can't believe how many people are, publicly, throwing themselves under a bus.

It's almost scarey to see how Jeff's point is borne out by his readers.

I liked the comments about recursion. Follow FIzzBuzz with a recursion problem and I think you'd really separate the top guys.

But are these the guys you really want tackling (and creatively solving) your company's work? And moreover, are these the guys you want to hang out with at company Christmas parties?
Requesting Gravatar... Haacked Feb 27, 2007 10:57 AM
# re: Why Can't Programmers... Read?
Give you the answer?

I still need to gather requirements! What platform must this FizzBuzz program support? Any performance requirements? Does the output need to be available over the web?...
Requesting Gravatar... Brandon K. Feb 27, 2007 10:57 AM
# re: Why Can't Programmers... Read?
Man I really want to read that post on CodingHorror, but it is currently down :-(. I do agree with Rob though in that most programmers have ADD and it is due to that I think programmers miss a lot of the requirements that are written down. They start to read the reqs then their minds start to wonder around to who knows what. Since the site is down what were some of the things that were getting posted?

Phil, with concern to the testing wouldn't you agree though that the praise for people being fast is due to Project managers or supervisors view that we need to come in or under budget? I don't think that is the correct way to think, but that is what I tend to see. If it is wrong then that is what the maintenance period or stabilization is for.
Requesting Gravatar... Haacked Feb 27, 2007 11:16 AM
# re: Why Can't Programmers... Read?
@Brandon: Yeah, the time pressures are very real and often the cause of this mentality. But it's extremely short sighted at times.

For example, does it matter if you came under budget on time if by rushing, there's a bug in the code that does the wrong thing? Did you *really* finish the project on time if the code you produced does the wrong thing?

If you go to have a custom car made, and they give you a bicycle, but it's on time. Did they succeed?

Also, it's well documented by Steve McConnell and others that the longer a bug is in the system, the more costly it is to find and remove.

So by rushing the coding phase, and hoping to catch it in the maintenance/stabilization phase, a project overall is more costly and takes longer.

Ideally, project managers would look at the overall cost of a project when judging a developer's productivity and not separate the rush and code stage and the bug-fix stage.

If developer A takes 10days to code feature A, but the bugs introduced take 20 days to find and fix. But developer B takes 15 days, and his bugs take 5 days to find and fix. Which developer should be rewarded?
Requesting Gravatar... Ryan Smith Feb 27, 2007 11:20 AM
# re: Why Can't Programmers... Read?
I think the argument between coding fast and coding right isn't always a clear cut one.

Sure, we would all love lots of time to test and debug our code to perfection, but most of the time, that just isn't a reality.

Just an example, last Thursday, I had to toss some untested code that I wrote at 11:45 at night with a few drinks in me over the wall on to a major client's live site.

I sure as hell didn't want to do it, but the code had to be up and running in the morning for a corporate webinar they were having. Certainly the code could have caused major problems, but there would be equally as many problems if I didn't just get the code up there.

It all turned out all right (thank the coding and alcohol gods) except for the fact I only got an hour of sleep because I was stressing about the untested code.

The bottom line is: we aren't all writing code for the space shuttle with the luxury of testers, QA, and a large defined process for validating the code. Sometimes "good enough" with the time alloted is all that matters.
Requesting Gravatar... Scott Feb 27, 2007 11:28 AM
# re: Why Can't Programmers... Read?
"If developer A takes 10days to code feature A, but the bugs introduced take 20 days to find and fix. But developer B takes 15 days, and his bugs take 5 days to find and fix. Which developer should be rewarded?"

Obviously, whichever one is the owners nephew.

That post is the best example of "solutions in search of a problem" I've seen in a while. Lots of people wanting to show how they would solve the problem more elegantly using code tricks. It all snowballed off that first example written in VBscript, but it was going to happen no matter what.

I also love that you dropped the "www" from his domain when you linked to him. hehehe. That's probably why his site is down, ISAPI-rewrite has spun out of control trying to re-write all the referrals from your post.
Requesting Gravatar... Haacked Feb 27, 2007 11:38 AM
# re: Why Can't Programmers... Read?
@Scott: Ha ha ha!

@Ryan: Agreed. My point is fast for fast sake does not a good developer make. It's all a series of trade-offs. Notice in my hypothetical question, the second developer still had bugs in his code to fix.

Developers and Project Managers have to find the right balance. It's all risk analysis. But, how many project managers you know do risk management?
Requesting Gravatar... Ryan Smith Feb 27, 2007 11:49 AM
# re: Why Can't Programmers... Read?
Risk management is a tricky thing with software. In my years as a developer, I have learned that you ALWAYS need a well defined statement of work before you start a project. You also have to set the expectation that anything outside the statement of work pushes out the schedule and budget.

But even with the solid statement of work, there's still risk involved due to the fact your always writing something you haven't created before. Even if you have, you never know what types of new obstacles your going to run into.

I was putting up a site onto a new server that I had done a million times before. Didn't expect to hit a single snag. Turned out that the new OS was x64 and good ole Adobe hasn't updated IFilter to handle x64 operating systems. What should have taken me under an hour burned an entire day coming up with a work around.

All that aside, I completely agree with you. There is always going to be a balance between getting it done right and just getting it done.
Requesting Gravatar... Haacked Feb 27, 2007 11:56 AM
# re: Why Can't Programmers... Read?
@Ryan: Heh heh. Well the thing I try and remind people is that risk management doesn't mean you can prevent the risk. It only means that you are aware of the risk, are working to reduce the risk, and hopefully have a contingency if the worst happens.

Even with clear acceptance criteria, SOW, etc... There's always the risk of miscommunication with a client.
Requesting Gravatar... Haacked Feb 27, 2007 12:00 PM
# re: Why Can't Programmers... Read?
...and I might add the risk that there's something you forgot to add to your list of risks. ;)

There always seems to be something you couldn't anticipate. I had a similar incident where I flew to Chicago to install an Intranet for a HUGE company. And their database on-site is ORACLE. I developed the site for SQL Server. Somehow, this little detail slipped everyone's radar.

Luckily, it took a bit of feverish on-site coding to get things working. THis was in the ASP days, so I only needed notepad.
Requesting Gravatar... Shan Plourde Feb 27, 2007 12:32 PM
# re: Why Can't Programmers... Read?
Clearly efficiency is important, but efficiencies aren't always realized by "programming fast". Reading comprehension is important also but so is critical problem solving thinking, design awareness, and the capability to independently question requirements and identify issues. I think there's so many other factors involved that it's too difficult to make such a binary comparison. You need to look at a more holistic picture - someone who knows theory, is adaptable, works well in ambiguous situations, communicates well, interacts with people well, etc.

If I had to consider efficiency just on its own then clearly certain types of efficiencies have been proven to be ineffective, such as IBM's infamous relationship of SLOC with developer productivity.

If a programmer can't read specs and requires lots of hand holding, design reviews and iterations, produces crappy code, and doesn't grow reasonably over time with feedback then fire them or transfer them. Get someone that will be a better fit and you won't have as many issues to deal with. If you've got a crappy budget then you'll get what you pay for.
Requesting Gravatar... Kevin Dente Feb 27, 2007 12:53 PM
# re: Why Can't Programmers... Read?
Fast but sloppy programmers - the bane of my existence. Managers love them because they churn out lots of code. Co-workers hate them because the churn out lots of CRAP.

It's kinda like the old joke. The warden addressed the prisoners and says "I've got good news and bad news. The bad news is that we have to eat is horse shit. The good news we've got lots of it".
Requesting Gravatar... Brandon K. Feb 27, 2007 1:02 PM
# re: Why Can't Programmers... Read?
A thought just popped in my head as I was talking this over with a co-worker. How many programmers do some basic testing as they code. I know that time can be tight and not really adequate to perform complete testing, but shouldn't you at least mentally walk through code (in the case of the writing a function/subroutine and a piece of paper) at the minimum?
Requesting Gravatar... Corey Goldberg Feb 27, 2007 1:55 PM
# re: Why Can't Programmers... Read?
ouch..

I am one of the guilty. I quickly read Jeff's post and then posted my Python version of FizzFuzz in his comments.

After reading your post, I went back and realized I didn't read the spec fully and my answer was incomplete.. doh!

ok ok.. shoot me.. i'm worse than somebody that can't program :)
Requesting Gravatar... Haacked Feb 27, 2007 1:58 PM
# re: Why Can't Programmers... Read?
@Corey: Ha ha. Well, I was a bit harsh in my post. As Scot Hanselman points out, there are some things you can't teach to programmers. For example, enthusiasm.

That's definitely a good sign.
Requesting Gravatar... Haacked Feb 27, 2007 1:59 PM
# re: Why Can't Programmers... Read?
P.S. It's "FizzBuzz" not "FizzFuzz". [;)]
Requesting Gravatar... Adam Feb 27, 2007 2:10 PM
# re: Why Can't Programmers... Read?
P.S. It's "FizzBuzz" not "FizzFuzz". [;)]

LOL!

(I'm afraid to write anything else)

Requesting Gravatar... Ray Villalobos Feb 27, 2007 3:11 PM
# re: Why Can't Programmers... Read?
This is PHP, but I didn't time myself so I don't know if I'd make it. I kind of lose track of time when I'm thinking about a problem. No more than 3-5 mins. I thought it was an obvious modulus operator problem soon as I saw it. If someone didn't know about the modulus operator, it'd be much harder.

I keep on thinking the last part is kind of messy and there's probably a better way of doing it.

<? for ($x=1; $x<=100; $x++) {
if ($x%3==0) {
print "Fizz";
}

if ($x%5==0) {
print "Buzz";
}


if ((!($x%3==0)) && (!($x%5==0))){
print $x;
}
print "<br />";
}
?>
Requesting Gravatar... Scott Feb 27, 2007 3:43 PM
# re: Why Can't Programmers... Read?
Fizz
Requesting Gravatar... Rob Conery Feb 27, 2007 6:43 PM
# re: Why Can't Programmers... Read?
I was teasing you Phil when I asked if you would write the answer (pushing you off the cliff of Jeff's point) - I'm so glad you didn't.

The tempation to offer a solution is pretty much like farting in church: you gotta do it - the sensation and need is just too much. And ... "uh oh!" FizzBuzz!

I don't know PHP but Ray your solution is wrong I hate to say. The big problem here is that we know you dealt it :).
Requesting Gravatar... jayson knight Feb 27, 2007 9:29 PM
# re: Why Can't Programmers... Read?
Interesting...I've been typing up a post on what I consider healthy developer attributes but haven't published it yet.

I've worked with guys in the past who were phenomenal coders, but had to be micromanaged to the point of insanity and were guilty of just what you pointed out...inability to read, or in most cases, not reading between the lines i.e. validating input, or including exception handling, or logging, etc.

Then there are guys (and I fall into this category) who are average coders, but are sticklers for getting things right the first time, and also take the time to ask lots of questions if they aren't sure about something. It also amazes me how many so called developers turn their machines off at 5pm, and don't turn them back on until the next workday. For them, it's just another gig. Passion scores more points for folks I interview than just about anything else.
Requesting Gravatar... Phantom of Ash Feb 27, 2007 10:14 PM
# Programmers Can't Program Are Not Programmers
Programmers Can't Program Are Not Programmers
Requesting Gravatar... Adi Feb 28, 2007 4:29 AM
# re: Why Can't Programmers... Read?
"As it turns out, there are still people out there with reading difficulties, as I was amazed to find someone actually wrote a PHP code in a comment to Phil's post!"
Requesting Gravatar... Eric D. Burdo Feb 28, 2007 5:03 AM
# re: Why Can't Programmers... Read?
This is prevalent in meetings too. The whole ADD thing.

As soon as people start mentioning technical or design requirements, the "programmer" in us starts thinking of solutions. Never mind that what they just gave isn't the real spec. We are already trying to solve it.

That happens on written specs too. We start trying to solve the problem from the first paragraph, and we never finish reading the sentence.
Requesting Gravatar... Jiltedcitizen Feb 28, 2007 9:08 AM
# re: Why Can't Programmers... Read?
I just laugh at people actually thinking there are 'specs'. If I got a project with such clearly specified specs I'd be overwhelmed with joy. Here someone would just say "fizzbuzz" to me and then after I coded tell me what they meant. Of course I try and get everything out of them that I can.
Requesting Gravatar... DotNetKicks.com Feb 28, 2007 9:51 AM
# Why Can't Programmers... Read?
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Requesting Gravatar... BloggingaBlog Feb 28, 2007 11:34 AM
# re: Why Can't Programmers... Read?
I really dont think everyone who replied would be considered programmers. But kudos on insulting a group of people.
Requesting Gravatar... Haacked Feb 28, 2007 11:42 AM
# re: Why Can't Programmers... Read?
And kudos to you for missing the entire point.
Requesting Gravatar... Harry Feb 28, 2007 8:39 PM
# re: Why Can't Programmers... Read?
Manager: What is your estimation for solving the FizzBuzz problem?

Programmer: Hmmm... 30 minutes with automated build, unit test, and everything.

Manager: What? No, it's over the budget. You have only 2 minutes to give me your program.

Programmer: OK, I guess I can take out the automated build and unit testing part. Which will take 5 minutes.

Manager: I don't think you comprehend what I just said. 2 minutes.

Programmer: OK, I guess I can just do it without even trying to test run it. BTW, is 3 and 5 going to changed? Will the client want to introduce new requirements to it? It will make more sense if the rules can be configurable by external files...

Manager: I don't know. And, you are wasting you time. Just do it.


-- That's our daily life --
Requesting Gravatar... Tap Mar 01, 2007 7:54 AM
# re: Why Can't Programmers... Read?
@Rob (or anyone who knows): How is Ray's solution wrong? Was this a joke? The output looks correct when I run it.
Requesting Gravatar... Sougent Mar 01, 2007 11:53 AM
# re: Why Can't Programmers... Read?
I did the whole fizzbuzz thing myself, just to see where I stood. It took me a while to do because I am one of the "What's a modulus operator?" kind of programmers and so I did it in Visual Basic without it (did the division and used INSTR to find the decimal on the answer, no decimal in the answer then it was evenly divided).

Of course, after reading some of the other responses, I promptly went and rewrote it "properly" using the MOD operator. Even spiffied up the GUI, added a Clear button and all that.

Of course, both times I did a proper unit test and refrained from posting the code, though I'm tempted to compile it and make it available for download, it's so pretty. Maybe I'll put the answers in a nice spreadsheet type grid...........

Requesting Gravatar... Scott Mar 01, 2007 12:17 PM
# re: Why Can't Programmers... Read?
Tap:
Read the problem again.
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".


Ray's solution doesn't print out "FizzBuzz" if the number is a multiple of both 3 and 5. It just prints the number.
Requesting Gravatar... Tap Mar 01, 2007 4:54 PM
# re: Why Can't Programmers... Read?
It does print out FizzBuzz if the number is a multiple of 15; there just isn't a separate test for it. As proof, I present the first 15 lines of output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Requesting Gravatar... sden Mar 02, 2007 8:00 AM
# re: Why Can't Programmers... Read?
if ((!($x%3==0)) && (!($x%5==0))){
print $x;
}

Hand trace

if not x is (not == !) mod 3 and not x mod 5
print x;

why wouldn't that print the number?

;')
Requesting Gravatar... Nate Mar 02, 2007 12:04 PM
# re: Why Can't Programmers... Read?
I think you guys are full of shit.
This non-sense ego exualtation.
No one is better than I am crap.

A Quality programmer is one with a lot of analysis/design and programming experience and one who learns from his experiences either good or bad.
You may have guys quick on their feet and very intelligent but if they are not willing to take criticism or advice because they know everything then that is a bad programmer. No one knows everything.
I work for a fortune 500 company and thank the Lord we do not ask people to take such meaningless test.
it does not indicate to anyone how this person will benefit the company! Geesh! Such sillyness.

Bad programmers are those who come up with crazy coding tricks and who refuse input from collegues or from their seniors.
Requesting Gravatar... Haacked Mar 02, 2007 12:35 PM
# re: Why Can't Programmers... Read?
Tell us how you really feel Nate. ;)

Honestly, I think you kinda missed the point of the test. The test doesn't tell you who a good programmer is. It's too simple and meaningless for that, as you rightly pointed out.

The point of the test is to screen out the self proclaimed programmers who really can't programmer. I don't know about your company, but at companies I've worked at, we'd get a few hundred submissions for each position.

Do we really time to give each submission a rock solid interview? Of course not.
Requesting Gravatar... Rob Conery Mar 02, 2007 4:27 PM
# re: Why Can't Programmers... Read?
I think we need a test to confirm if someone's "full of shit" :). I think Nate has a point...

And I want to thank Nate for allowing me to swear on Phil's blog. I've always wanted to...
Requesting Gravatar... Haacked Mar 02, 2007 5:08 PM
# re: Why Can't Programmers... Read?
This one is for Nate et all. It's my own experience with "ego exaltation" in an interview from hell.

I think the questions I was asked were decidedly NOT "FizzBuzz" questions.

Requesting Gravatar... Scott Mar 03, 2007 1:43 PM
# re: Why Can't Programmers... Read?
You're right Ray and tap, can't argue with the output. ;)
Requesting Gravatar... Jehanzeb Mar 06, 2007 1:35 AM
# re: Why Can't Programmers... Read?
using System;
using System.Collections.Generic;
using System.Text;

namespace FizzBUzz
{
class Program
{
static void Main(string[] args)
{
for (int index = 1; index <= 100; index++)
{
if (index % 3 == 0 && index % 5 == 0)
{
// Print it out
Console.WriteLine("FizzBuzz");
}
else
{
if (index % 3 == 0)
{
// Print Fizz
Console.WriteLine("Fizz");
}
else
if (index % 5 == 0)
{
// Print Buzz
Console.WriteLine("Buzz");
}
}

if (index % 3 != 0 && index % 5 != 0)
{
// Print it out
Console.WriteLine(index.ToString());
}
}
}
}
}
Requesting Gravatar... Reg Braithwaite Mar 07, 2007 6:09 AM
# re: Why Can't Programmers... Read?
Actually, I did not say that 199 out of 200 graduates cannot program. What I said was that 199 out of 200 people applying for jobs blindly cannot program.

I posted a follow-up explaining the math.

I can't justify the 199 out of 200 number!

I actually stole that from Joel Spolsky. And he didn't say 199 out of 200 can't program at all: he just asserted that 199 out of every 200 people spamming employers are the same people who spam every employer, so you can't assume that you're hiring the top 0.5% just because you reject 199 out of 200 applicants.
Requesting Gravatar... blah Mar 07, 2007 8:10 AM
# re: Why Can't Programmers... Read?
Rays solution works fine. It seems like the ones criticizing his program and screaming amen to the article are the ones the article is talking about. Just because he doesnt explicitly test for the "FizzBuzz" condition doesnt mean it is broken. I did pretty much the same thing in perl. I know I could use flags to avoid doing the modulus tests twice but I wanted to do it with the least amount of typing and this solution is copy/paste friendly.

#!/usr/bin/perl

for(1..100){
print unless ($_ % 3 == 0 or $_ % 5 == 0);
if ($_ % 3 == 0) {print "Fizz";}
if ($_ % 5 == 0) {print "Buzz";}
print "\n";
}

And can we give developers the benefit of the doubt that when playing around on a web blog we are not going to read and code a spec with the exact same brain power and resolve that we would on a project at work? I know I wouldnt. I dont care about coding some elementary programming project on somebodies web blog exactly to spec. This is not justification to make mass blanket statements about developers.
Requesting Gravatar... AC Mar 07, 2007 8:23 AM
# re: Why Can't Programmers... Read?
>Write a program that prints the numbers from 1 to 100...

Why can't spec writers write? Unless you mean integers, there are an infinite number of real numbers 'from 1 to 100'

#!/usr/bin/perl -w
use strict;
my $msg;

for (my $i=1; $i <= 100; $i++) {
$msg = 0;
if ($i % 3 == 0) {
print 'Fizz';
$msg = 1;
}
if ($i % 5 == 0) {
print 'Buzz';
$msg = 1;
}
if ($msg == 0) {
print $i;
}
print "\n";
}
Requesting Gravatar... aju Mar 07, 2007 1:16 PM
# re: Why Can't Programmers... Read?
The original statement was "The problem may not be that these programmers can’t program. It’s that they can’t read!"

Comments like Nates, proves the point of the statement.
Requesting Gravatar... Tony Morris Mar 07, 2007 1:17 PM
# re: Why Can't Programmers... Read?
"Follow FIzzBuzz with a recursion problem"
Here is the clincher.

FizzBuzz *is* a recursion problem. The fact that people try to solve it in any other way (typically a loop) is what leads to the high defect rate.

It is simply a map over a list

> map f [1..100]

Of course, I have omitted the definition of f, but I only wish to point out that loops (imperative programming for that matter) are a) leading to high defect rates b) nobody (in the general sense) knows it because they don't know anything superior exists (or they get on their high horse at the mention of one thing being superior to another - I've seen this before)
Requesting Gravatar... tesla Mar 07, 2007 3:49 PM
# re: Why Can't Programmers... Read?
Specs? What is this that you speak of. I am programmer, I need no spec...
Requesting Gravatar... acidt Mar 08, 2007 11:37 PM
# re: Why Can't Programmers... Read?
#include <iostream>
using namespace std;
int main()
{
for(int i=1; i<101; i++)
{
if (i%3==0) cout << "Fizz";
if (i%5==0) cout << "Buzz";
if (! ((i%3==0)||(i%5==0)) ) cout << i;
cout << endl;
}
system("pause");
return 0;
}
Requesting Gravatar... opello Mar 09, 2007 9:08 AM
# re: Why Can't Programmers... Read?
Good read.

Closing tags since ... oh never mind.
Requesting Gravatar... Haacked Mar 09, 2007 10:43 AM
# re: Why Can't Programmers... Read?
Thanks opello for pointing out the unclosed tags! I'm surprised that happened. Shouldn't happen in Subtext. Looks like I have a bug to open. I closed the tags myself and removed the closing tags from your comment.
Requesting Gravatar... clef Mar 10, 2007 5:36 PM
# re: Why Can't Programmers... Read?
My attempt at this:

class Fixnum
def divisble_by?(num)
(self % num).zero?
end
end

def fizzbuzz
(1..100).each do |i|
if i.divisble_by? 3 and i.divisble_by? 5
puts 'FizzBuzz'
elsif i.divisble_by? 3
puts 'Fizz'
elsif i.divisble_by? 5
puts 'Buzz'
else
puts i
end
end
end

fizzbuzz
Requesting Gravatar... WishKnew Mar 11, 2007 9:56 AM
# re: Why Can't Programmers... Read?
I remember the old days using my turbo prolog:

predicates
fizzbuzz(integer)
solution(integer)

clauses
fizzbuzz(I) :- I mod 3 = 0, write("fizz", "\n"), !.
fizzbuzz(I) :- I mod 5 = 0, write("buzz","\n"), !.
fizzbuzz(I) :- write(I, "\n"), !.

solution(X) :-
X <= 100, fizzbuzz(X), Y = X + 1, solution(Y).

goal
solution(1).
Requesting Gravatar... Wishknew Mar 11, 2007 10:07 AM
# re: Why Can't Programmers... Read?
This is turbo prolog:

predicates
fizzbuzz(integer)
solution(integer)

clauses
fizzbuzz(I) :- I mod 15 = 0, write("fizzbuzz", "\n"), !.
fizzbuzz(I) :- I mod 3 = 0, write("fizz", "\n"), !.
fizzbuzz(I) :- I mod 5 = 0, write("buzz","\n"), !.
fizzbuzz(I) :- write(I, "\n"), !.

solution(X) :-
X <= 100, fizzbuzz(X), Y = X + 1, solution(Y).

goal
solution(1).
Requesting Gravatar... Zak Mar 12, 2007 8:58 AM
# re: Why Can't Programmers... Read?
Tony, mapping a function over a list isn't recursion[0], though it is a good way to solve this problem. I would not use recursion to solve fizzbuzz; iterative and functional solutions are shorter, clearer and less error-prone. Here are solutions in Common Lisp:

(defun fizzbuzz-iterative ()
(loop for x from 1 to 100 do
(cond ((and (= (rem x 3) 0)
(= (rem x 5) 0))
(print "FizzBuzz"))
((= (rem x 3) 0)
(print "Fizz"))
((= (rem x 5) 0)
(print "Buzz"))
('t (print x)))))


(defun fizzbuzz-recursive (&optional (x 1))
(cond ((> x 100)
(return-from fizzbuzz-recursive 'nil))
((and (= (rem x 3) 0)
(= (rem x 5) 0))
(print "FizzBuzz"))
((= (rem x 3) 0)
(print "Fizz"))
((= (rem x 5) 0)
(print "Buzz"))
('t (print x)))
(fizzbuzz (1+ x)))

and a fizzbuzz function that can be mapped across any list of integers:

(defun f (x)
(cond ((and (= (rem x 3) 0)
(= (rem x 5) 0))
(print "FizzBuzz"))
((= (rem x 3) 0)
(print "Fizz"))
((= (rem x 5) 0)
(print "Buzz"))
('t (print x))))


[0] A given language might implement map recursively or iteratively - this should be invisible to the user.
Requesting Gravatar... John Bailo Oct 04, 2007 3:38 PM
# re: Why Can't Programmers... Read?

You could also say "Why SHOULD Programmers Read".

Most "specs" are pie in the sky fantasies.

A programmer writes in code, not spec-talk.

Requesting Gravatar... thrage Apr 19, 2008 11:25 PM
# re: Why Can't Programmers... Read?
beacuse were same in programming structure so that its easily them to understand the codes...... even its different programming language the used....
Requesting Gravatar... Ben Perry Jul 18, 2008 8:29 PM
# re: Why Can't Programmers... Read?
Just for the hell of it I did it in Befunge-93
(lets hope the comment form doesn't screw it up)


>1+29+9*2+%:#v_@
v \0 <
,>:3%#v_"zziB" v
*v <\1$\,,,,<
5>:5%#v_"zzuF" v
2v <\1$\,,,,<
>\#v_:.v
^ < <
Requesting Gravatar... jOE Jul 19, 2008 3:01 PM
# re: Why Can't Programmers... Read?
My question is a little more fundamental: Why are all people named "Scott" programmers. Is this some kind of national law that I'm not aware of. Seriously, are there any Scott's who are accountants or doctors or pool cleaners? Seems like they're all in IT to me. And they all talk VERY FAST ;)

As for the ADD thing, I agree. Good programmers start thinking of solutions from the get go. And its only the purest NON-programmer who would ever accuse a programmer in this process of having ADD :)

Acutually that's a handly little way of determing whose a programmer and who isn't!
Requesting Gravatar... Scott Sep 17, 2008 9:14 PM
# re: Why Can't Programmers... Read?
That is hardly fair jOE. I am not a programmer... well I might do some VB.Net VBA office automation. I am, however, a Document Specialist. But yes... I am in IT. I don't have ADD. I don't talk fast... much.
Requesting Gravatar... Jeck Lamnent Mar 27, 2009 1:07 AM
# re: Why Can't Programmers... Read?
That example spec is poorly written which is why such a large amount of programmers are failing at it.

1) It doesn't clearly state that multiples of 5 should not replace the number with buzz.
2) Through normal logic one would assume that 'FizzBuzz' is the result of 3 & 5 having both passed, but the spec doesn't say that, it just says to print FizzBuzz again.

I've been programming my whole life and poorly worded/written specs are a big problem. Not all clients have the ability to communicate their needs logically.

At any rate, here is what those specs actually say:

for ($i = 1; $i <= 100; $i++) {
$is3 = !($i%3);
$is5 = !($i%5);
print $is3 ? 'fizz' : $i;
print $is5 ? 'buzz' : null;
print $is3 && $is5 ? 'fizzbuzz' : null;
print "\r\n";
}

-Jeck
Requesting Gravatar... pluma Apr 15, 2009 6:02 PM
# re: Why Can't Programmers... Read?
Just for argument's sake, here's my take in PHP:

for ($i = 0; $i < 101; $i++) {
$fizz = ($i % 3 == 0);
$buzz = ($i % 5 == 0);
$output = true;
if ($fizz) {
echo 'Fizz';
$output = false;
}
if ($buzz) {
echo 'Buzz';
$output = false;
}
if ($output) {
echo $i;
}
echo "\n";
}

Or without temporary variables:

for ($i = 0; $i < 101; $i++) {
if ($i % 3 == 0) {
echo 'Fizz';
if ($i % 5 == 0) {
echo 'Buzz';
}
} else if ($i % 5 == 0) {
echo 'Buzz';
} else {
echo $i;
}
echo "\n";
}

That way the condition is duplicated, but there will be at most two checks performed at any one step of the loop.

Also, loop terminating conditions should be exclusive, I hear. I confess that I mostly disregard that rule, but then again, I also violate K&R by handling function blocks and other blocks identically.

What do you have to say?

(will show your gravatar)
Please add 7 and 6 and type the answer here: