Hello! I come from .NET (C#) environment, so my solution is this:
namespace Dummy.FizzBuzz
{
class Program
{
static void Main(string[] args)
{
FizzBuzzEvaluator evaluator = new FizzBuzzEvaluator();
for (int i = 0; i <= 100; i++)
{
Console.WriteLine("{0} = {1}", i, evaluator.FizzBuzz(i));
}
Console.ReadKey();
}
}
public class FizzBuzzEvaluator
{
public string FizzBuzz(int number)
{
string result = null;
if (number % 3 == 0) result = "Fizz";
if (number % 5 == 0) result += "Buzz";
return result ?? number.ToString();
}
}
}
Written from the top of my head, so it's not the best code in the world - but I think it does its job pretty decently.
So... explanation:
Core functionality is implemented in public class FizzBuzzEvaluator. I wanted to keep code modular and separate for reusage (for example: tests).
That being said, code follows the logic:
If a number is a multiple of 3, result equals "Fizz". If a number is a multiple of 5, add "Buzz" to the result. This logic automatically covers the scenario where a number is a multiple of 3 and 5 - without any additional 'if' statements (because "Fizz" was already added).
Zero is mathematically a multiplier of 3 and 5, so it's covered in above conditions.
Another code candy is the return statement: C# has convenient operator ??, which means "if value is null return something else". In our case, if number doesn't fit "mod 3" and/or "mod 5" conditions, the value of variable "result" is null and we return the number (instead of an empty string).
In the program entry point (Main method) we simply instantiate FizzBuzzEvaluator class, call the FizzBuzz method for each number from 0 to 100 and write the output.
Console.ReadKey method at the end is added just for pause after execution, otherwise program automatically closes if it's run directly from Visual Studio.
And now: tests
We need to create a new "Unit test project" and "Unit test file" and the final code of Unit test file is:
using Dummy.FizzBuzz;
namespace Dummy.FizzBuzzTest
{
[TestClass]
public class EvaluatorTest
{
private FizzBuzzEvaluator evaluator = null;
[TestInitialize]
public void Init()
{
evaluator = new FizzBuzzEvaluator();
}
[TestMethod]
public void Number0()
{
var result = evaluator.FizzBuzz(0);
Assert.AreEqual("FizzBuzz", result);
}
[TestMethod]
public void Number3()
{
var result = evaluator.FizzBuzz(3);
Assert.AreEqual("Fizz", result);
}
[TestMethod]
public void Number5()
{
var result = evaluator.FizzBuzz(5);
Assert.AreEqual("Buzz", result);
}
[TestMethod]
public void Number13()
{
var result = evaluator.FizzBuzz(13);
Assert.AreEqual("13", result);
}
[TestMethod]
public void Number15()
{
var result = evaluator.FizzBuzz(15);
Assert.AreEqual("FizzBuzz", result);
}
}
}
We test 5 main number values: 0, 3, 5, 15 and 13. If any of the tests fails an exception is thrown (and Test explorer shows red X instead of green checks :) ).
Well, this is a lot of stuff... I don't doubt I made a boo-boo somewhere in between :)
Let me know what you think! Too complicated?
I think your result needs to be initialized, because on 5 your have
null + "Buzz"
. I'm not a c# person so I don't know if that implicitly works.Kudos for test cases, that is worth a tip! I suggest you test a few negative numbers and multiples of 3 and 5, maybe
[-21, -25, -3, -15, 35, 33]
.Maybe a c# person can chip in but I like this solution :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
It works, I have test cases to prove it :)
On a serious note, here's a quote from C# language specification: "If an operand of string concatenation is null, an empty string is substituted."
I also added tests for mentioned numbers and it seems to work fine (-21 returns "Fizz", 35 returns "Buzz", etc.).
Thanks for your feedback!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Interesting, that is a pretty cool feature. Thank you for pulling out the specifications 😊
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hi @bandli! You have just received a 0.5 SBD tip from @reggaemuffin!
@tipU - send tips by writing tip! in the comment and get share in service profit :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for the tip!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit