Skip to content

Instantly share code, notes, and snippets.

@dadhi
Created June 25, 2013 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadhi/5856527 to your computer and use it in GitHub Desktop.
Save dadhi/5856527 to your computer and use it in GitHub Desktop.
FizzBuzz on C# as generic solution using LINQ.
[Test]
public void FizzBuzz()
{
var rules = new Dictionary<int, string> { { 3, "Fizz" }, { 5, "Buzz" } };
Func<int, string> translate = i =>
rules.Aggregate((string)null, (s, x) => i % x.Key == 0 ? (s == null ? x.Value : s + x.Value) : s)
?? i.ToString();
var words = Enumerable.Range(9, 7).Select(translate).ToArray();
CollectionAssert.AreEqual(new[] { "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" }, words);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment