(for (i 1 100)
(if
(= 0 (% i 15)) (set 'i "FizzBuzz")
(= 0 (% i 5)) (set 'i "Buzz")
(= 0 (% i 3)) (set 'i "Fizz"))
(println i))
The first thing thatās happening here is people are thinking āHmm, yes it sounds like a really simple problem anyone should be able to cope with, but Iād better check it myself to make sure there isnāt a hidden twistā.
The second thing is that once people find thereās no twist, it strikes them as quite a neat little problem which could be used to compare languages or try out new forms of obfuscation.
So I can see why any of us who hears of this problem first tries it out in our favourite language, then our second favourite language, then the language weāre currently trying to learn, then the language we hate, then the toy language we once invented, then in our favourite language in a silly way, ā¦
Hereās my solution in Smalltalk. It may not be the most elegant one, but, certainly, Smalltalk is the most elegant programming language.
" FizzBuzz test "
Transcript clear.
1 to: 100 do: [ :n |
(n \\ 3 = 0) ifTrue: [Transcript show: 'Fizz'].
(n \\ 5 = 0) ifTrue: [Transcript show: 'Buzz'].
((n \\ 3 ~= 0) (n \\ 5 ~= 0)) ifTrue: [Transcript show: n printString].
Transcript cr.
]
Here is my solution. It is written in 8051 assembly. You will need a 11.0592 MHz quart to run this thing
;*********************************
;* Program: fizzbuzz
;* Author: Samir Ben Abid (samirbenabid@linuxmail.org)
;* Description: Outputs the FizzBuzz test results to an rs232 port, 9600,8,0,n
;* P.S:
;* Compiled with Keil C51 running on Wine (lame!)
;*********************************
;*** CONSTANTS ***
; divider1
; the first divider's value
divider1 equ 3d
;divider2
; the second devider's value
divider2 equ 5d
; limit
; the maximum value that the fizzbizz program can reach. Must be between 0 and 100
limit equ 100d
;*** CODE ***
; Code segment at 0
cseg at 0
ljmp start ; avoir interrupt vectors
org 0x100 ; and go to a safer place
start: ; a.k.a here.
;
mov scon,#0x52 ; set UART to 8,0,n
mov tmod,#0x20 ; Timer1: autoreload
mov th1,#0xfe ; speed: 9600 bps
setb tr1 ; start Timer1
mov dptr,#intro ; load the intro and...
call emit ; beem it!
mov r7,#0 ; set counter to 0
compute:
cjne r7,#limit,continue ; are we finished with counting to 100?
jmp rest_in_peace ; Yes... rest in peace.
continue:
call do_crlf ; go to next line
mov r4,#1 ; set the (r7%3)? flag
inc r7 ; counter++
mov b,#divider1 ;
mov a,r7 ; divide the counter by three
div ab ;
mov a,b ; and get the reminder
jz do_fizz ; if it is null, do a fizz :)
next:
mov b,#divider2 ;
mov a,r7 ; divide the counter by five
div ab ;
mov a,b ; and get the reminder
jz do_buzz ; if it is null, do a buzz :D
mov a,r4 ; did we do a fizz?
jz compute ; if yes, reume the loop
call write_number ; else, write the number
jmp compute ; and resume the loop
rest_in_peace:
clr tr1 ; Stop Timer1
jmp $ ; AM STONED!
; do_fizz
; gets: nothing
; returns: 0 in r4
; description:
; Beems a "Fizz" through the UART
do_fizz:
mov dptr,#fizz ; load the fizz
call emit ; then display it
mov r4,#0 ; and leave a message: "I was here"
jmp next ; then resume your normal activity
; do_buzz
; gets: nothing
; returns: nothing
; description:
; Beems a "Bizz" through the UART
do_buzz:
mov dptr,#buzz ; load the buzz
call emit ; then display it
jmp compute ; then resume the loop
; do_crlf
; gets: nothing
; returns: nothing
; description:
; Beems the Carriage return/Line feed controle caracters through the UART.
do_crlf:
mov dptr,#crlf ; load crlf
call emit ; then send it
ret ; and return
; emit
; gets: the adress of the message to display in dptr
; returns: nothing
; description:
; Beems an ASCIIZ message, stored in the code memory, through the UART.
emit:
mov r6,#0 ; initialize the index to 0
bc_1:
mov a,r6 ;
inc r6 ; load the pointed byte
movc a,@a+dptr ;
jz fin ; if zero then return
jnb ti,$ ; if the last transmission isn't over, stay in your place
mov sbuf,a ; and transmit!
clr ti ; and clear ti to get further notifications :p
jmp bc_1 ; end of the loop
fin:
ret ; return
; emit_id
; gets: the digit's value in A (must be between 0 and 9)
; returns: nothing
; description:
; Translates a one-digit-bcd value located in A into Ascii and the beems it through the
; UART.
emit_id:
mov r5,a
mov a,#'0'
add a,r5
jnb ti,$
mov sbuf,a
clr ti
ret
; write_numer
; gets: the number to display in r7
; returns: nothing
; description:
; Beems the Ascii representation of the number located in r7 through the UART. r7 must
; be between 0 and 99
write_number:
mov a,r7 ;
mov b,#10d ; divide the number by 10
div ab ;
jz write_l ; if it si less than 10 then just write the modulo
call emit_id ; else, write the result (because r7100) :)
write_l:
mov a,b ; prepare the parameters
call emit_id ; and send the digit
ret ; then return
; *** STATIC DATA ***
; fizz
; type: Asciiz string
; description:
; containes the fizz message
fizz: db "Fizz",0
; buzz
; type: Asciiz string
; description:
; containes the buzz message
buzz: db "Buzz",0
; intro
; type: Asciiz string
; description:
; contains the intro message.
; P.S:
; intro shares it's Asciiz 0 with crlf, 3 code bytes of economy :)
intro: db "The FizzBuzz test"
; crlf:
; type: Asciiz string
; description:
; containes the crlf byte couple
; P.S:
; shares itself and it's Asciiz 0 with intro
crlf: db 10,13,0
; Bye Bye :)
end
Iāve liked the BrainF**k solution.
Any solution in INTERCAL?
I love this example it is a prime way to weed out the candidates who donāt understand the basic operators that come in very handy.
Groan
The debate about proving oneself aside, this whole thing is symptomatic of a downward spiral in hiring by so called Human Resources.
I was recently multiply interviewed by a well-known internet search company (who shall obviously remain nameless) for a post in image processing. Now, I think itās fair to say Iām an expert in this field with over 12 years research experience and a PhD to boot. However, in one of the many interviews I was asked question after question on programming databases - about which I know approximately zero - by someone who had a list of set answers. Some of these questions required āreal timeā programming, ie. writing programs on the fly, which is damn near impossible. Needless to say that because of that one interview I wasnāt hired.
On another occasion some years previous I was asked to program a Euclidean distance estimate function, which I did perfectly, only to be told that it was no good since it wasnāt how their programmer had done it. This was a tacit acknowledgement that the person asking the question didnāt actually know how to do it but could compare the two and see they werenāt the same. [Incidentally, my solution was the more elegant of the two].
My points being:
- This kind of mindless task is far easier to ask than to answer, so is perfect for HR robots looking to eliminate candiates a.s.a.p. with minimall effort. As such it should be avoided not encouragedā¦
- It proves little about programming ability - which is a problem solving skill rather than a regurgitation skill. We all have access to the resources on the 'net in our daily work and knowing how to use that effectively is more important.
Z.
Itās so difficult:
for($i = 1; $i 101; $i++) echo $i % 15 == 0 ? 'Fizz-Buzz' : ($i % 3 == 0 ? 'Fizz' : ($i % 5 == 0 ? 'Buzz' : $i)) , "\n";
ā¦
I just enjoyed seeing how this problem was expressed in all the different languages⦠Itās slightly more complicated than hello world, so it shows a little about those languageās syntactic and logical structures.
I wrote it in Postscript, which was fun. I wrote 3 versions of it⦠the ānormalā version, the compact speed-optimized unreadable/obfuscated version, the completely generalized version.
The final version allowed you to specify any range of numbers (1-100), any strings for replacement (fizz, buzz), any numbers to compare against (3,5), and any comparison logic to use via a named functioned (multiples of), so that you now have a function that will iterate over a range replacing integer values with string via whatever arbitrary comparison you wishā¦
Why? Because it was fun, and relaxing compared to the programming tasks I have to do all day at work. I became a programmer because I enjoy it. Itās hard to maintain your enjoyment of it when ābusiness interestsā are constant destroying any fun you might be able to eek out of your work. So, doing stuff like this is akin to doing the crossword puzzles in the newspaper⦠Itās no great feat, but it exercises your mind just the tiniest bit, and itās completely frivolous, which is less boring, and less stressful than not doing it.
-th
This Fizzbuzz issue really is a problem though. I remember in first year, there were a bunch of people who couldnāt code worth a damn. I figured theyād be weeded out by 2nd year. Nope. 3rd year? Nope. 4th year? Nope, theyāre still there, right beside me. Somehow, they manage to basically skimp by all the classes, without ever actually coding. My supervisor for my thesis says he even has this problem with graduate students that canāt code! I think the issue is that, in my program at least, there arenāt that many hardcore coding classes. And, basically all the hard coding courses past 1st year put you in groups to do work⦠and large groups at that, since the classes are so big. So, if these people can manage to squeeze by first year, then it only gets easier to weasel by in 2nd, 3rd, and 4th year, since they simply let other group members handle the coding part of the project⦠or they may do nothing at all. The few courses Iāve taken that have hard individual coding assignments that are actually worth something are only required by certain streams. Many people also manage to graduate w/o the courses by getting substitutes.
Console.WriteLine("1\n2\nfizz\n4\nbuzz\nfizz\n7\n8\nfizz\nbuzz\n11\nfizz\n13\n14\nfizz\n16\n17\nfizz\n19\nbuzz\nfizz\n22\n23\nfizz\nbuzz\n26\nfizz\n28\n29\nfizz\n31\n32\nfizz\n34\nbuzz\nfizz\n37\n38\nfizz\nbuzz\n41\nfizz\n43\n44\nfizz\n46\n47\nfizz\n49\nbuzz\nfizz\n52\n53\nfizz\nbuzz\n56\nfizz\n58\n59\nfizz\n61\n62\nfizz\n64\nbuzz\nfizz\n67\n68\nfizz\nbuzz\n71\nfizz\n73\n74\nfizz\n76\n77\nfizz\n79\nbuzz\nfizz\n82\n83\nfizz\nbuzz\n86\nfizz\n88\n89\nfizz\n91\n92\nfizz\n94\nbuzz\nfizz\n97\n98\nfizz\nbuzz\n");
Iāve been using FizzBuzz-type questions, but usually simpler than FizzBuzz, in interviews for 20 years now (I just dug out my first interview notes from 1987!). Iāve interviewed people for many types of programming job. What follows the FizzBuzz-type question will vary depending on the specific job, but I always start with the trivial one.
Why do I ask them to write Fizzbuzz? If theyāre lost when looking at that one, I can save everybodyās time by wrapping up the interview quickly.
Jeffās question was āWhy do we have to ask people to write FizzBuzz?ā
-
Because a CS degree is not a guarantee of basic coding literacy. Thatās unfortunate, but true. Me, I think they should ask for their money back from the colleges that churned them out.
-
Because HR is now a game of tick-the-box. āSalary expectations within range? Check. Degree? Check. Two years of Java? Check. Able to program? Sorry, HR canāt handle that one.ā
Last week, I interviewed 5 people, all with CS degrees. None of them answered the FizzBuzz-type question correctly, and only one of them came close. It was a bad week.
Iām disappointed in the answers to the fizzbuzz problem that have been posted here. I never saw my favorite:
printf("1,2,FIZZ,4,BUZZ,5,FIZZ,7,8,FIZZ,BUZZ,11,FIZZ,13,14,FIZZBUZZ.....\n");
Kinda puts the world into perspective. Iām 12 years old, in 6th grade, and the fizzbuzz problem is ridiculously easy. Picked up C++ for dummies and within a few weeks, I can script (albeit not really wellā¦).
I HAS A FizzBuzz in LOLCode (http://tinyurl.com/27eszt). Now I just need a compiler so I can test itā¦
HAI
CAN HAS STDIO?
I HAS A LOOPVAR
IM IN YR LOOP
IZ (LOOPVAR MOD 3 LIEK 0) AND (LOOPVAR MOD 5 LIEK 0)?
YARLY
VISIBLE "FizzBuzz"
NOWAI
IZ (LOOPVAR MOD 3 LIEK 0)?
YARLY
VISIBLE "Fizz"
NOWAI
IZ (LOOPVAR MOD 5 LIEK 0)?
YARLY
VISIBLE "Buzz"
NOWAI
VISIBILE LOOPVAR
KTHX
KTHX
KTHX
UP LOOPVAR!!1
IZ LOOPVAR BIGR THAN 100? KTHXBYE
IM OUTTA YR LOOP
KTHXBYE
āAnd instead of writing FizzBuzz code, they should be thinking about ways to prevent us from needing FizzBuzz code in the first place.ā
Is the same as sayingā¦
āAnd instead of taking tests, they should be thinking about ways to prevent us from needing to take tests in the first place.ā
That is the dumbest thing Iāve heard today. How do you get people to prove they know what they know? Itās called testing. Thanks for being an egotistical prick about it though.
Iāll play stairway to heaven whenever I want!
Good point. I read the FizzBuzz replies, and have to say, I actually was asked to write a FizzBuzz once for a job interview. They told me they were impressed, which only depressed me. If knowing how to make use of modulus arithmetic and a for loop is impressive, then what is the world coming to?
Still, I got an interview out of it. But no job - I think that was because I couldnāt encapsulate the differences between C and Java during the interview. And/or, I wasnāt a C# guru, though not exactly a C# moron.
Iām also depressed by the paucity of languages most FizzBuzz respondents used. I mean, what, no COBOL? No FORTRAN? No Algol68? No PL/I? No IBM S/390 HLASM? No Ada? No Bliss?
And only one APL!
Sheesh!
Akbar on March 5, 2007 06:10 AM
The interviewer who is expecting, and eventually selecting, a programmer to developing the FizzBuzz logic in less than couple of minutes, is actually trying to build a team for a project which is more than likely to lead to failure and waste millions of dollars and leading the project stakeholders to suicidal death. A programming skill is not something that may spontaneously be predicted and come up with the best logic suitable for a given scenario.
Looks like someone failed the test. I guess we do need a test to see if a person can think⦠even if an 8 year old could pass it