1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

FizzBuzz Test

Discussion in 'Programming' started by pradeep, May 27, 2009.

  1. pradeep

    pradeep Team Leader

    Recently I came across an article where the author finds most of the candidates interviewed for the post of a developer can't write basic code, so he gave everyone a simple test called "FizzBuzz Test", which is as follows:

    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".

    Read more here http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/

    Some newbies in the forum might consider posting a solution the problem in wichever language they wish to.
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Lets make it interesting.
    I request G4EF members to write the above mentioned program in ANY language of their choice, provided the same language has not been used by any other member in any of the previous posts in this thread.

    Let me start with C
    Code:
    #include <stdio.h>
    
    int main()
    {
          unsigned N, flag = 0;
          for( N = 1; N < 101; ++N, flag = 0 )
          {
                if ( ! (N % 3) )      {      printf("Fizz");      flag = 1;      }
                if ( ! (N % 5) )      {      printf("Buzz");      flag = 1;      }
                if ( ! flag )         printf("%d", N);
                putchar(10);
          }
          return 0;
    }
    
     
  3. navinkumarank

    navinkumarank New Member

    this is really cool... :) !

    I have it here in perl

    Code:
    perl -le 'print (($_%15)?(($_%3)?(($_%5)?$_:"Buzz"):"Fizz"):"FizzBuzz") for 1..100'
    OR
    Code:
    #!/usr/bin/perl
    
    for (1..100){
        print (($_%15)?(($_%3)?(($_%5)?$_."\n":"Buzz\n"):"Fizz\n"):"FizzBuzz\n");
    }
    
    let me know ur views... !
     
    Last edited by a moderator: May 28, 2009
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Please ALWAYS post code inside code-blocks. (I request shabbir to edit the above post.)

    I would really wish to see the FizzBuzz program in some esoteric programming language :wink:
     
  5. shabbir

    shabbir Administrator Staff Member

  6. shabbir

    shabbir Administrator Staff Member

    Code:
    for( unsigned N = 1; N < 101;N++,(!(N%15))?cout<<"FizzBuzz":(!(N%5))?cout<<"Buzz":(!(N%3))?cout<<"Fizz":cout<<N<<endl);return 0;
     
  7. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    :eek: :surprised What was that shabbir ?!

    Where is the main func ?
    Writing the whole code in a single line, significantly reduces the readability of your code.
     
  8. pradeep

    pradeep Team Leader

    Mine in shell script

    Code:
    #!/bin/sh
    
    for i in `seq 1 100`
    do
    	if [ `expr $i % 3`  -eq 0 ] && [ `expr $i % 5`  -eq 0 ];
    	then
    		echo "FizzBuzz"
    	elif [ `expr $i % 3` -eq 0 ];
    	then
    		echo "Fizz"
    	elif [ `expr $i % 5`  -eq 0 ];
    	then
    		echo "Buzz"
    	else
    		echo $i
    	fi
    
    done
    
     
  9. shabbir

    shabbir Administrator Staff Member

    Its same as your code only and so I thought would just not change the printf's to cout and so did it that way buddy
     
  10. xpi0t0s

    xpi0t0s Mentor

    Forth (developed and tested with Win32Forth)
    Code:
    variable test3
    variable test5
    variable prt
    
    : prog 1 0 0 test3 ! test5 ! prt !
    20 1 do 
    test3 @ 1 + dup test3 ! 3 = if ." fizz" 0 0 prt ! test3 ! then
    test5 @ 1 + dup test5 ! 5 = if ." buzz" 0 0 prt ! test5 ! then
    prt @ if i . else ."  " then
    1 prt !
    loop ;
    
     
  11. xpi0t0s

    xpi0t0s Mentor

    And a better one that doesn't use variables:
    Code:
    : prog1 
    20 1 do
    0
    i 3 mod 0= if ." fizz" drop 1 then
    i 5 mod 0= if ." buzz" drop 1 then
    0= if i . else ."  " then
    loop ;
    
     
  12. shency

    shency New Member

    Code:
    <?php
        for($i = 1; $i <= 100; $i++)
        {
         echo (($i%3 ==0) && ($i%5 ==0)) ? 'FizzBuzz<br>' : (($i%3==0) ? 'Fizz<br>' :(($i%5==0) ? 'Buzz<br>' : "$i<br>"));
        }
    ?>
     
  13. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    @ xpi0t0s : Please do mention the language (atleast in your code-block), before posting any code.

    BTW, that code looks like Forth, am I right ??


    One more by me : (in HLA = High Level Assembly)
    Code:
    program FizzBuzz;
    
    #include ("stdlib.hhf");
    
    static
    	Number:			int32 := 1;
    
    begin FizzBuzz;
    	mov(101, ebx);
    	mov(1, Number);
    	while(Number < ebx) do
    		mov(0,ecx);
    		mov(Number,eax);
    		mov(3,dl);
    		div(dl);
    		if (ah = 0) then
    			mov(1,ecx);
    			stdout.put("Fizz");
    		endif;
    		mov(Number,eax);
    		mov(5,dl);
    		div(dl);
    		if(ah = 0) then
    			mov(1,ecx);
    			stdout.put("Buzz");
    		endif;
    		if(! ecx) then
    			stdout.put(Number);
    		endif;
    		stdout.put(nl);
    		inc(Number);
    	endwhile;
    end FizzBuzz;
    
     
    Last edited: May 28, 2009
  14. xpi0t0s

    xpi0t0s Mentor

    You are, and I did, but it's on page 1:
     
  15. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Oh ! :embarrass: , I didn't see it !
    [ Bad habit of jumping to the last post, directly ]
     
  16. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Finalllyyyy... After struggling with BrainF*ck for 3 hours, I conclude that the language truly justifies its name !

    Here is my BF code for some thing as simple as a Fizz-Buzz test :

    Code:
    [FONT="Fixedsys"]<    * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       # *   (+) FIZZ-BUZZ PROGRAM BY SASWAT PADHI >             *
     # # *                                                       *
     # # *   This BrainF*ck program is written by Saswat Padhi   *
     # # *   I have done much labour on this code as writing a   *
     # # *   BF code is not easy; even harder than ASM     ;)    *
     # # *                                                       *
     # # *   Believe it or not I have spent 3 hours writing this *
     # # *   shit(looking) code !                                *
     # # *                                                       *
     # # *   If you want to use this code; please give me some   *
     # # *   credit by atleast mentioning my name :) Thanks      *
     # # *                                                       *
     # # *   PLEASE NOTE THAT THIS IS THE UNCOMMENTED VERSION OF *
     # # *   MY ORIGINAL FIZZBUZZ PROGRAM; SO BETTER NOT TO TRY  *
     # # *   READING IT ;)     IF YOU WANT A MORE HUMAN_FRIENDLY *
     # # *   VERSION THAT IS WELL COMMENTED; YOU MAY CONTACT ME  *
     # # *                                                       *
     # # * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
     # # # # # # # # # # # # # # # # # # # # # # # # # # # # #     >
    
    [COLOR="Blue"]
    ************************************************************************
    ******************** FIZZBUZZ BF SOURCE CODE BEGINS ********************
    ************************************************************************
    **                                                                    **
    **                                                                    **
    **  >++++++++++++++++++++++++++++++++++++++++++++[[>]+++++++++++++++  **
    **  ++++++++++++++++++++++++++++++++++++++++++++++++++[<]>-]>+++++.>  **
    **  ++++++++.>+++++++++++++++++++++++++.>+++++++++++++++++++++++++.>  **
    **  --------------------.>+.>++++++++++++++++++++.>+++++++++++++++++  **
    **  ++++++++.>+++++++++++++++++++++++++.>---------------------------  **
    **  ------.>+++++++++++++++.>+++++++++++++++++.>++++++++++++++.>++++  **
    **  ++.>+++++++++++++++++.>.>++++++++++++.>-------------------------  **
    **  --------.>+.>++++++++++++++++++++++++.>-------------------------  **
    **  --------.>++++++++++++++++++.>.>++++++++++++++++++.>++++++++++++  **
    **  ++++++++++.>.>+++++++++++++++++++.>-----------------------------  **
    **  ----.>+++++++++++++++.>.>+++.>+++++++.>++++++++.>---------------  **
    **  ----------------------------------------..>---------------------  **
    **  -------------------------------------------->+++++>++++++++>++++  **
    **  +++++++++++++++++++++>+++++++++++++++++++++++++>----------------  **
    **  ------------------------------------------------->+>++++++++++++  **
    **  ++++++++>+++++++++++++++++++++++++>+++++++++++++++++++++++++>>++  **
    **  ++++++++>>++++++++++++++++++++++++++++++++++++++++++++++++>+++++  **
    **  ++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++>  **
    **  >>>>>>>>>++<<<<<<<<<<<<<<[>++++++++++>>----------<<[>>+>>[-]>[-]  **
    **  >[-]>[-]+>[-]+>[-]+>[-]>[-]>[-]>+>[-]<[>+>+<<-]>>[<<+>>-]<<---[<  **
    **  <<<<<[-]>>>>>[-]+>-]<[>>[<+>-]>+<<<-]>>>[<<<+>>>-]<<<<<<<<<<<<<[  **
    **  >>+>+<<<-]>>>------------------------------------------------[>>  **
    **  >[-]>>[-]+<<<<+<-]>[<+>-]<-----[>>>>[-]>>[-]+<<<<<<-]<[<<+>>-]>>  **
    **  >[<<<<<<<<<<[<]<[<]>.>.>.>.>>>>>>>>>>>>>>>-]>[<<<<<<<<<<<[<]>.>.  **
    **  >.>.>>>>>>>>>>>-]>[<<<<<<<<<<<<[<]>.>.>.>.>>>>>>>>>>>>-]>[>[>[<<  **
    **  <<<<<<<<<.>.>>>>>>>>>>-]<-]<-]<<<<<<<.<<<-]>+<<-]<<[<]>.>.>.>.<>  **
    **                                                                    **
    **                                                                    **
    ************************************************************************
    ******************** END OF FIZZBUZZ BF SOURCE CODE ********************
    ************************************************************************
    [/COLOR]
    
    Program Size    :: 4254 bytes
    Instructions    :: 1669
    Number of moves :: 694083
    Average RunTime :: 30 ms
    
    Notes :
    (*) Compile with option "Allow wrapping of pointers = TRUE"
    (*) Compile with option "Allow wrapping of values = TRUE"[/FONT]
     
    shabbir likes this.
  17. shabbir

    shabbir Administrator Staff Member

    Thats really a BrainF*ck really
     
  18. pradeep

    pradeep Team Leader

  19. pradeep

    pradeep Team Leader

    Code:
    for(var i = 1; i <= 100; i++)
    {
    	document.write(((i%3 ==0) && (i%5 ==0)) ? 'FizzBuzz<br>' : ((i%3==0) ? 'Fizz<br>' :((i%5==0) ? 'Buzz<br>' : i+"<br>")));
    }
    
     
  20. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Code:
    FOR I = 1 TO 100
        Flag = 0
        IF I MOD 3 = 0 THEN PRINT "FIZZ"; : Flag = 1
        IF I MOD 5 = 0 THEN PRINT "BUZZ"; : Flag = 1
        IF Flag = 0 THEN PRINT I;
        PRINT ""
    NEXT I
    END
    
     

Share This Page