Last active
December 23, 2015 17:39
-
-
Save damonjmurray/6670161 to your computer and use it in GitHub Desktop.
A standard FizzBuzz solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
using System.Text; | |
namespace FizzBuzzKata | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
const int fizzFactor = 3; | |
const int buzzFactor = 5; | |
var result = Enumerable.Range(1, 100) | |
.Select(i => FizzBuzz(i, fizzFactor, buzzFactor)); | |
PrintResults(Console.Out, result); | |
Console.ReadLine(); | |
} | |
private static string FizzBuzz(int value, int fizzFactor, int buzzFactor) | |
{ | |
var result = new StringBuilder(); | |
if (value % fizzFactor == 0) result.Append("Fizz"); | |
if (value % buzzFactor == 0) result.Append("Buzz"); | |
return result.ToString() == string.Empty | |
? value.ToString(CultureInfo.InvariantCulture) | |
: result.ToString(); | |
} | |
private static void PrintResults(System.IO.TextWriter writer, IEnumerable<string> resultSet) | |
{ | |
foreach (var result in resultSet) | |
writer.WriteLine(result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment