Code Kata: FizzBuzz–How did you fare?

In my last post, I introduced the concept of a code-kata (coding exercises) and gave an example problem to solve:

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

For those of you who tried the example, how did you find it? Pretty simple right?

Did you make it look this easy though? Whoever recorded that video is nicely demonstrating the benefits of honing their development environment – he/she even goes as far as disabling their mouse to train themselves to use the keyboard more effectively.

How well was your code designed? Is it adaptable to change?

Let’s move the goal posts

In real life, requirements change all the time. We can easily practice writing adaptable code by throwing some new requirements into the mix.

Try to take these steps one by one, as if a client was drip feeding them to you – try not to read ahead too far!

  • Try extending the application to support another range of numbers, such as 15-175
  • Try extending the application such that a user could provide any range of numbers (such as from the console, or from configuration)
  • Try extending the application to support new rules – output “Baz” for numbers divisible by 4
  • Instead of printing the numbers to the console, try extending the app to write to a file

Put your money where your mouth is, show me the codez!

Ok, my C# kata can be found on my github page here: https://github.com/CraigCav/FizzBuzz. Over time, perhaps we’ll see more implementations of this appear on my github page in other languages, such as F#. Interestingly enough, I used this example to hone my environment; to learn the GIT commands (instead of using a GUI) by practicing them over and over with this small simple example.

About these ads

About craigcav

Craig Cavalier works as a Software Developer for Liquid Frameworks in Houston Tx, developing field ticketing and job management solutions for industrial field service companies.

Posted on March 29, 2012, in Unit Testing and tagged , . Bookmark the permalink. 2 Comments.

  1. Want to see an F# implementation?

    let fb = fun x ->
    if x % 15 = 0 then “FizzBuzz”
    elif x % 5 = 0 then “Buzz”
    elif x % 3 = 0 then “Fizz”
    else x.ToString()

    let rec fizzbuzz = fun x ->
    match x with
    | [] -> []
    | [v] -> [fb v]
    | h::t -> [fb h] @ (fizzbuzz t)

    [1..100]
    |> fizzbuzz
    |> List.iter (printfn “%s”)

  2. Ok, so the comments window mucked up the indentation and F# is particular about indentation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: