Last active
May 6, 2019 19:49
-
-
Save alejofv/2dae0a7264881a846eda17acf8d8adaf to your computer and use it in GitHub Desktop.
A C# FizzBuzz solution with some linq love and extensibility support
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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| namespace FizzBuzz | |
| { | |
| static class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var buzzMap = new Dictionary<int, string> | |
| { | |
| { 3, "Fizz" }, | |
| { 5, "Buzz" }, | |
| }; | |
| Func<int, string> buzzer = i => | |
| { | |
| var buzzed = buzzMap.Keys | |
| .Where(k => i % k == 0) | |
| .Select(k => buzzMap[k]); | |
| return buzzed.Any() ? buzzed.Aggregate((prev, next) => prev += next) : null; | |
| }; | |
| var output = Enumerable.Range(1, 1000) | |
| .Select(i => buzzer(i) ?? i.ToString()) | |
| .Aggregate(new StringBuilder(), (sb, s) => sb.AppendLine(s)) | |
| .ToString(); | |
| Console.WriteLine(output); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment