FizzBuzz

Today I stumbled into http://codingdojo.org/cgi-bin/wiki.pl?KataFizzBuzz and decided to implement the kata as a little TDD finger exercise. Interestingly I learned that FizzBuzz is sometimes used in job interviews to validate a candidate’s ability to code. To my suprise I also learned that a lot of programmers seem to fail this test.

The FizzBuzz problem can shortly be described as:

Any number divisible by three is replaced by the word Fizz and any number divisible by five by the word Buzz. Numbers divisible by both become FizzBuzz. All other numbers remain as they are.

The task at hand is to “write a program that prints the numbers from 1 to 100 by applying the above rules”.

Let me show you what I came up with – the “FizzBuzzer”:

package de.ake.FizzBuzz;

public class FizzBuzzer {

	public String calculateValue(int i) {
		String returnValue = String.valueOf(i);

		if (isDivisibleByFifteen(i)) {
			returnValue = "FizzBuzz";
		}
		else if (isDivisibleByThree(i)) {
			returnValue = "Fizz";
		}
		else if (isDivisibleByFive(i)) {
			returnValue = "Buzz";
		}

		return returnValue;
	}

	private boolean isDivisibleByFifteen(int value) {
		return (0 == value % 15);
	}

	private boolean isDivisibleByThree(int value) {
		return (0 == value % 3);
	}

	private boolean isDivisibleByFive(int value) {
		return (0 == value % 5);
	}	

	public static void main(String[] args) {
		FizzBuzzer fb = new FizzBuzzer();

		for (int idx = 1; idx <= 100; idx++) {
			System.out.println(fb.calculateValue(idx));
		}
	}

}

I moved the checks for divisibility into seperate functions with meaningful names so that it should be clear what is being checked and so in the end there is not to much magic left to be explored. The only important thing is to make the check for mod(15) first, since otherwise it will never be reached.

Now for a variation of the problem the requirements can slightly be changed to:

Any number divisible by three or containing the digit three is replaced by the word Fizz and any number divisible by five or containing the digit five by the word Buzz.

What I’m missing in this description is the order in which these rules shall be applied. Let’s for example take the number 35. There is definitely a three in 35 and a five and it also divisible by five. So what ranks higher? Divisibility or containing the digit and which number comes first? Since nothing is mentioned I just didn’t care to much and changed the code to this:

package de.ake.FizzBuzz;

public class FizzBuzzer {

	public FizzBuzzer() {
	}

	public String calculateValue(int value) {
		String returnValue = String.valueOf(value);

		if (isDivisibleByThree(value) || containsDigitThree(value)) {
			returnValue = "Fizz";
		}
		else if (isDivisibleByFive(value) || containsDigitFive(value)) {
			returnValue = "Buzz";
		}

		return returnValue;
	}

	private boolean isDivisibleByThree(int value) {
		return (0 == value % 3 );
	}

	private boolean isDivisibleByFive(int value) {
		return (0 == value % 5 );
	}	

	private boolean containsDigitThree(int value) {
		return String.valueOf(value).contains("3");
	}

	private boolean containsDigitFive(int value) {
		return String.valueOf(value).contains("5");
	}

	public static void main(String[] args) {
		FizzBuzzer fb = new FizzBuzzer();

		for (int idx = 1; idx <= 100; idx++) {
			System.out.println(fb.calculateValue(idx));
		}
	}

}

While creating the code I wrote my test cases to validate my doings and stepwise push the implementation as it is done in TDD.

For the sake of completeness here are at least the tests that I wrote while implementing the FizzBuzz variation:

package de.ake.FizzBuzz;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class AFizzBuzzerVariationShould {
	FizzBuzzer fb;	

	@Before
	public void init() {
		fb = new FizzBuzzer();
	}

	@Test
	public void map1To1() {
		assertEquals("1", fb.calculateValue(1));
	}

	@Test
	public void map2To2() {
		assertEquals("2", fb.calculateValue(2));
	}

	@Test
	public void map3ToFizz() {
		assertEquals("Fizz", fb.calculateValue(3));
	}	

	@Test
	public void map5ToBuzz() {
		assertEquals("Buzz", fb.calculateValue(5));
	}

	@Test
	public void map13ToBuzz() {
		assertEquals("Fizz", fb.calculateValue(13));
	}	

	@Test
	public void map35ToBuzz() {
		assertEquals("Fizz", fb.calculateValue(35));
	}

	@Test
	public void map52ToBuzz() {
		assertEquals("Buzz", fb.calculateValue(52));
	}

	@Test
	public void map51ToBuzz() {
		assertEquals("Fizz", fb.calculateValue(51));
	}
}

Well, I assume that there is always a sleeker way of making things happen and if you search a little on the internet you’ll find more FizzBuzz solutions than you can dream of. Still I hope you liked this one 😉

Advertisements

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

%d bloggers like this: