Wednesday, March 07, 2007

FizzBuzz busted...

Just recently I wrote about the "FizzBuzz" test.

Well, it popped up again.  Someone wrote "Why Can't Programmers... read".   The author notes that of course, programmers hopped right on and provided their own solutions to the FizzBuzz test.

But, they did not read the specification!  So, they got it wrong.  Entirely wrong - but make comments like "I think the above solution is not elegant.".  Whoops.

Here is my second take on it, in my 3rd favorite language - C:

[tkyte@desktop ~]$ cat test.c
#include "stdio.h"
int main()
{
int i;
char buff[5];

for( i = 1; i<=100; i++ )
printf( "%s%s%s\n",
(i%3) ? "":"Fizz",
(i%5) ? "":"Buzz",
((i%3)&&(i%5)) ?
(snprintf(buff,sizeof(buff),"%d", i), buff):"" );
}
[tkyte@desktop ~]$ make test
cc test.c -o test
[tkyte@desktop ~]$ ./test
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...
Buzz
Fizz
97
98
Fizz
Buzz
[tkyte@desktop ~]$

Man, it has been a while since I programmed C, I was a bit rusty - it did not compile the first time (second time is a charm).


I have to admit - more than once I've fallen into the "sorry, I read that too fast, you are right - that won't work" trap myself on asktom.  That is one reason peer review is really important - no one lets a wrong answer get through :)

POST A COMMENT

26 Comments:

Blogger Hae said....

I saw the "Why can't programmers ... program" late last week on digg.com and have been wondering since. Not having coded previously aside from mere shell scripts, to a newbie, what would you recommend the person do when they learn coding so that they don't fall into the group mentioned in the article where "programmers" could not code a simple task?

By the way, are you a fan of digg.com? I could spend a large chunk of my time reading your blog, digg.com, and the Creating Passionate Users blog day in and day out. If only there was a job out there I could get paid for reading those, and learning new stuff.

Wed Mar 07, 11:46:00 AM EST  

Blogger Thomas Kyte said....

What would I recommend? A mentor, someone experienced and smart

I talked about that previously...

As for 'digg', I don't like the articles at digg as much as reddit. I like the articles that show up there much more than digg.

Wed Mar 07, 12:24:00 PM EST  

Anonymous Anonymous said....

So when do they write "Why can't Analysts write specs?"

(Says the guy who works in a place where everything is "designed" verbally and the specs change capriciously based on constant renegotiation with customers who buy and sell each other.)

Wed Mar 07, 02:17:00 PM EST  

Blogger Thomas Kyte said....

Oh, trust me - my biggest pet peeve is "not being able to phrase a sensible question" - which is analogous to "I cannot give you a specification from which to derive code"....

Wed Mar 07, 02:35:00 PM EST  

Blogger Hae-Kwang said....

[quote]
Thomas Kyte said....

What would I recommend? A mentor, someone experienced and smart
[/quote]

Thank you very much for your input! I can totally relate to the linked post as I had met someone similar to Woody Lons at my previous job. (As a side comment, everyone would seem to agree that this person was very similar to you in terms of being helpful and thorough in explaining things in an understandable way - very willing to help, and very good at it.) Unfortunately, they've moved out of state and it's a bit difficult to get a hold of the person.

Related to the post that you had linked to, I am without a degree and am finding it difficult to break into the IT sector. I've had fleeting thoughts that the lack of one might be keeping me from entering the door. Just the other day at an interview for a help desk position, the people interviewing me encouraged me to continue learning, and even go back to school to improve my chances of obtaining my current dream job. In addition to what they recommended, would you be able to offer insight regarding how I can at least get my foot in the door?

What'd be the best way to find a superb mentor that's experienced and smart to take me under their wings and teach me how to go through the ropes, especially for a newbie that's not even yet in the field?

Wed Mar 07, 06:05:00 PM EST  

Anonymous Pete Finnigan said....

Hi Tom,

I also read the original article to which you refer. I am not surprised by it..:-),

I thought I would drop a note to say how can the C language be ONLY your third favourite..:-), it warmed my heart to see some C this morning. It was

cheers

Pete

Thu Mar 08, 04:44:00 AM EST  

Blogger Thomas Kyte said....

What'd be the best way to find a superb mentor that's experienced and smart to take me under their wings and teach me how to go through the ropes, especially for a newbie that's not even yet in the field?

"Luck" :) Sorry, there is no formula here... Not that I know of

Thu Mar 08, 06:33:00 AM EST  

Blogger Thomas Kyte said....

how can the C language be ONLY your third favourite

Well, it used to be first, but SQL and PL/SQL superseded it in terms of usefulness (day to day)....

To be able to do in a single SQL statement what would take hundreds or thousands of lines of C code - that is the stuff of warming hearts :)

Thu Mar 08, 06:35:00 AM EST  

Anonymous Stew Ashton said....

In my company, they consider that "programmers can't program" -> SQL, so they insist on using Hibernate. I actually heard this argument today from a respected colleague: better generate fairly bad SQL from a tool than risk programmers coding very bad SQL.

I wish we could buy a tool that would generate fairly bad decisions instead of having managers and architects make very bad decisions :(

Thu Mar 08, 04:24:00 PM EST  

Anonymous Stew Ashton said....

In my company, they consider that "programmers can't program" -> SQL, so they insist on using Hibernate. I actually heard this argument today from a respected colleague: better generate fairly bad SQL from a tool than risk programmers coding very bad SQL.

If only we had a tool to generate fairly bad decisions instead of having managers make very bad ones.

Thu Mar 08, 04:25:00 PM EST  

Anonymous Haacked said....

Hey there. I wrote the Why can't programmers... read? post. I followed it up with two other posts that address some of these comments.

Why can't spec writers write specs? and a follow up to that one entitled Specs are always ambiguous.

Case in point, are you sure your output matches the spec? The original question says to replace multiples of 3 with Fizz, but it doesn't say anything about replacing multiples of 5. ;)

Of course, we can guess what the person meant, but a compiler can't.

Fri Mar 09, 08:06:00 PM EST  

Anonymous Anonymous said....

I am also one who believe that the specification is hopeless. It is written with total disregard for how people read and what can be understood.

It clearly states that the numbers should be printed. And then renegs on that.

BTW There is one glaring mistake with the code posted here: You are showing off. That is not how a programmer should write code.

Sat Mar 10, 04:05:00 AM EST  

Anonymous Anonymous said....

Our skills comes from problems we have solved. If university/school don't expose you to enough types of problems, you will appear stupid to those knows how to do these things.

Where I live a tax assessment form can be viewed as a program specification. However even bright people fail to create programs which can do the sums correct. Their problem is that they don't know how to solve these kinds of problems. They want to do everything at once instead of the small steps required.

How many have been thought to rewrite the specifications? That should help understanding.

Have a glorious weekend.

Sat Mar 10, 04:35:00 AM EST  

Anonymous Anonymous said....

Rule no 1: Rephrase specification and ask if your understanding is correct.

Rule no 2: Never, ever go away with the spec and come back with a program before doing actions in rule 1.

Rule no 3: Keep in contact with your customer to make sure that your understanding improves over time.

Rule no 4: Find ways of verifying your understanding: Make prototypes.

If you don't verify your understanding of the problem, you have not done you job.


greetings,

Sat Mar 10, 04:50:00 AM EST  

Blogger Robert Vollman said....

"it did not compile the first time (second time is a charm)."

Uhhh....

Did you change something in between the two compilation attempts? :)

Mon Mar 12, 12:16:00 AM EDT  

Anonymous Anonymous said....

Bash version:
a=("" "Fizz" "Buzz" "FizzBuzz");
for((i=1;i<=100;i++));do
a[0]=$i;
echo ${a[$(($i%15==0?3:($i%5==0?2:($i%3==0?1:0))))]};
done;

Mon Mar 12, 05:23:00 AM EDT  

Anonymous Sokrates said....

the obvious ugly point about your test.c is of course the
"char buff[5];"

which is sufficient for
"for( i = 1; i<=100; i++ )"

and would also be sufficient for
"for( i = 1; i<=10000; i++ )"

but would fail for
"for( i = 1; i<=10001; i++ )"

and all consecutive numbers.

sorry, don't like it !

not generalizable as it should be

Mon Mar 12, 09:51:00 AM EDT  

Blogger Thomas Kyte said....

Sokrates said....


it satisfies the requirements and was programmed in a "safe" way

snprintf(buff,sizeof(buff),"%d", i), buff)

using snprintf. So, seems perfectly "ok" to me.

generalizable is not always desirable (in fact, many/most times the opposite is true - we spend so much time making something generic - when it will never be used that way)

Mon Mar 12, 01:30:00 PM EDT  

Anonymous Sokrates said....

totally agree with you that
"generalizable is not always desirable"
but in such a "trivial" case it should (at least for me it appears it should)

using the "safe" implementation here is just another hidden
"exception when others then null",
isn't it ?
(nobody is complaining, but the result isn't correct)

of course we all know how to get around the issue here and find a "generic implementation", but this is these small things in C that annoy me.
(I started programming by learning C and really enjoyed it an liked the language very much, but nowadays I am pretty happy that I don't have to fight with these little issues anymore :-)

Tue Mar 13, 04:48:00 AM EDT  

Anonymous Anonymous said....

One should always try to make the code robust. Specifications might change.

BTW "char buff[5];" is enough for four characters. Remember to count in the trailing nul byte as part of the string.

Wed Mar 14, 02:17:00 AM EDT  

Blogger Thomas Kyte said....

One should always try to make the code robust. Specifications might change.

Code isn't robust - Xena the princess warrior, that is robust.

In any case - yes the trailing null was more than accounted for and the application could actually be expanded to do up to 9999 safely instead of just one hundred.

And - when specifications change, code will have to be reviewed, just like in all other technical practices (eg: building a building for example. They care not how "robust" you did it the first time - specs change - full review, else people die)


anyway, nuff said.

Wed Mar 14, 06:38:00 AM EDT  

Blogger Gert Bruhn said....

Unbelieveable just what I want to read today. I just found what I have been wondering over for a long time It was quite useful reading, found some interesting viewpoints in here, - Thanks!
You find my Website Here

Wed Mar 14, 05:34:00 PM EDT  

Anonymous luggage said....

I remember something similar about this in school : they once gave us a test in class, where like the third question said something about “don't worry answering any of the questions.” But because none of us read through all the instructions first, we spent 20 minutes trying to answer the first two questions and failed the test.
Ha ha, yep, this is obviously the same sort of thing. But don't worry – it's not programmers who are the only guilty parties in this sort of thing. Plus, we know that CEO's are probably the worst at this!

Thu Oct 04, 12:36:00 PM EDT  

Anonymous viagra said....

With regard to viagra and erectile dysfunction, it is already apparent that Viagra was first developed as a treatment for angina but later the drug emerged to be a suitable cure for male impotency but far more striking news is presently capturing the attention of the whole world. Seems Untrue! Well, it is doubtlessly true that oysters fed with the anti-impotency drug Viagra has shown effective results against erectile dysfunction and George May, the Australian farmer who created Viagra oysters became thoroughly popular after the news relating to the efficacy of his Viagra oysters leaked out. Guys! If cheap viagra can make oysters so powerful against erectile dysfunction then why won’t you be able to combat your impotency successfully after you buy viagra? Opt for Viagra via viagra online and once you buy Viagra online and administer the drug in accordance with the instructions of the doctor, you would instantly get cured of erectile dysfunction!

Tue Oct 30, 01:10:00 AM EDT  

Anonymous Anonymous said....

40inchplus extremeasses asses ass nice ass big ass sexy ass teen ass hot ass perfect ass big black ass big asses teen tight ass round ass huge ass nice asses hot teen ass round asses hot asses big butts nice butt big butt perfect butt cute butt nice butts cute butts perfect butts roundandbrown black pussy black booty black sex black ass black porn black tits black boobs black sluts black anal black blowjobs black butts black butt ebony porn ebony sex ebony teen ebony pussy ebony teens ebony ass ebony booty ebony anal ebony tits ebony blowjobs interracial sex interracial porn interracial cuckold streetblowjobs blowjob blowjobs blowjob clips teen blowjobs teen blowjob blowjob galleries young blowjobs blowjobs gallery young blowjob street blowjob blow job blow jobs massive blow job street blow jobs blow job gallery teen blow jobs teen blow job cumshot cumshots facial cumshots vaginal cumshots internal cumshots teen cumshots handjob handjobs hand job hand jobs oral sex oral penis sucking voyeur teen voyeur voyeur sex wild sex allsitesaccess porn sites reality porn sites sex sites reality porn reality sex reality sex sites xxx xxx porn xxx sex

Tue Dec 11, 09:42:00 AM EST  

Anonymous Anonymous said....

Tom, those spammers are at it again!

Thu Dec 20, 05:32:00 PM EST  

POST A COMMENT

<< Home