Monday, 2 April, 2007

FizzBuzz ... anyone ... ?

OK, hands up all of you that read the Why Can't Programmers.. Program? article on Coding Horror and wrote a FizzBuzz program? I have to admit that did ...
******************************************************** program FizzBuzz; {Write a program that prints the numbers from 1 to 100 and test for multiples of 3, 5 and both 3 and 5 use Fizz, Buzz and FizzBuzz respectivly to indicate these finds.} {$APPTYPE CONSOLE} uses SysUtils; resourcestring rsFizzBuzz = ' ... FizzBuzz'; rsBuzz = ' ... Buzz'; rsFizz = ' ... Fizz'; const cZero = 0; cThree = 3; cFive = 5; cForDoStart = 1; cForDoEnd = 100; var i: Integer; function FizzBuzzTest(IntIn: Integer): string; {For numbers which are multiples of both 3 and 5 return "FizzBuzz", for multiples of 3 return "Fizz" and multiples of 5 return "Buzz"} var bolMultp3, bolMultp5: Boolean; begin bolMultp3 := (IntIn mod cThree = cZero); bolMultp5 := (IntIn mod cFive = cZero); if bolMultp3 and bolMultp5 then Result := rsFizzBuzz else if bolMultp5 then Result := rsBuzz else if bolMultp3 then Result := rsFizz else Result := ''; end; begin for i := cForDoStart to cForDoEnd do begin writeln(IntToStr(i) + FizzBuzzTest(i)); end; readln; end. ********************************************************
Hey, I just want to see ... Well, I admit, I did go a little over-board with it. I did however write this before the FizzBuzz: the Programmer's Stairway to Heaven came out ... honest.

3 comments:

  1. OK, so it was only a day shy of April 1st and it was over a year ago, but having just read the comments over at Coding Horror, I think there is a place for a solution which does not use any testing.

    Technically, it is wrong because the question did not ask for the number to be printed as well as Fizz or buzz, but it matches the sample you put up.

    Why did I do it?, Well, I climb rocks for a hobby, so don't ask.

    DerekSmith

    program ProjectFizzBuzz;

    {$APPTYPE CONSOLE}

    uses
    SysUtils;
    var
    answer: array [1..100] of string;
    c: integer;

    begin
    for c := 1 to 100 do answer[c]:= inttostr(c)+'..';
    for c := 1 to 33 do answer[c*3]:= answer[c*3]+'fizz';
    for c := 1 to 20 do answer[c*5]:= answer[c*5]+'buzz';
    for c := 1 to 100 do writeln(answer[c]);
    readln;
    end.

    ReplyDelete
  2. So, to do it as the request posted:-

    program ProjectFizzBuzz;

    {$APPTYPE CONSOLE}

    uses
    SysUtils;
    var
    answer: array [1..100] of string;
    c: integer;

    begin
    for c := 1 to 100 do answer[c]:= inttostr(c); //fill in the list
    for c := 1 to 33 do answer[c*3]:= 'fizz'; //fill in the fizzes
    for c := 1 to 20 do answer[c*5]:= 'buzz'; //fill in the buzzes
    for c := 1 to 6 do answer[c*15]:= 'fizzbuzz'; //fil in the fizzbuzzes
    for c := 1 to 100 do writeln(answer[c]); //write out the results
    readln; //hold the console open
    end.

    DerekSmith

    ReplyDelete
  3. Hey, Derek these are great. Hope you enjoyed playing with it.

    The only thing that I find worthy of comment is that you visit 59 item in the array twice when populating the appropriate data and then every item in it once more to print out the values.

    Having said that, even with resorting to QueryPerformanceCounter to try and measure a speed hit, I [personally] can't come up with anything significant.

    Thanks,
    Dave

    ReplyDelete