Created
June 25, 2013 07:01
-
-
Save dadhi/5856527 to your computer and use it in GitHub Desktop.
FizzBuzz on C# as generic solution using LINQ.
This file contains hidden or 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
[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