Skip to content

Instantly share code, notes, and snippets.

@alejofv
Last active May 6, 2019 19:49
Show Gist options
  • Select an option

  • Save alejofv/2dae0a7264881a846eda17acf8d8adaf to your computer and use it in GitHub Desktop.

Select an option

Save alejofv/2dae0a7264881a846eda17acf8d8adaf to your computer and use it in GitHub Desktop.
A C# FizzBuzz solution with some linq love and extensibility support
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