FizzBuzz with Linq

I was reviewing potential interview questions and one intrigued me.

The question was to provide an  alternate way to do FizzBuzz. I had previously done the straightforward loop method and pondered on it for a while.

While washing our car, I had an inspired thought that I could easily do it  with Linq. I eventually came up with  the following after getting up at about 3 am, this morning, 3/6/2015.

 

Console.Write(string.Join("\n",Enumerable.Range(1,100).Select( number => number % 15 == 0? "FizzBuzz": number % 5
== 0? "Buzz" : number % 3 == 0 ? "Fizz" : number.ToString())).TrimEnd("\n".ToCharArray()));

 

 

Inspired by a past manager, Jason Bowers, who more or less planted the seeds of Clean Code in my mind,  I  continued through a bit longer. Now I don’t know if Jason approves, as I’m sure I could do more but here’s where I stopped.


class Program
{

static List<Tuple<int, string>> Divisor_FizzBuzzTuples = new List<Tuple<int, string>>();

static void Main(string[] args)
{
int rangeStart = 1;
int rangeEnd = 300;
Divisor_FizzBuzzTuples.Add(new Tuple<int,string>(3,"Fizz"));
Divisor_FizzBuzzTuples.Add(new Tuple<int, string>(5, "Buzz"));
Divisor_FizzBuzzTuples.Add(new Tuple<int, string>(7, "Pi"));
Divisor_FizzBuzzTuples.Add(new Tuple<int, string>(15, "FizzBuzz"));
Divisor_FizzBuzzTuples.Add(new Tuple<int, string>(21, "FizzPi"));
Divisor_FizzBuzzTuples.Add(new Tuple<int, string>(35, "BuzzPi"));
Divisor_FizzBuzzTuples.Add(new Tuple<int, string>(105, "FizzBuzzPi"));

Func<int, string> FizzBuzzify = i => GetFizzBuzzFromNumber(i);
var fizzBuzzList = Enumerable.Range(rangeStart, rangeEnd).Select(FizzBuzzify);
Console.Write(String.Join("\n", fizzBuzzList).TrimEnd("\n".ToCharArray()));

}

static string GetFizzBuzzFromNumber(int number)
{
string fizzbuzzornah = number.ToString();

var list = Divisor_FizzBuzzTuples.OrderByDescending(i => i.Item1);
foreach (var tuple in list)
{
if(number % tuple.Item1 == 0)
{
fizzbuzzornah = tuple.Item2;
break;
}

}

return fizzbuzzornah;

}
}

 

 

I was considering  adding only primes and then doing the necessary permutations but I decided to stop and work on some other outstanding stuff I had to do.

Update: 3/7/2014

Folowing some engagement on Facebook by Elliott Manley who cowrote the OWLS Word Games Enhancer with me I did come up with a better solution. It left me with a little egg on my face because I missed it.


class Program
{

static List<Tuple<int, string>> Divisor_FizzBuzzTuples = new List<Tuple<int, string>>();

static void Main(string[] args)
{
int rangeStart = 1;
int rangeEnd = 300;
Divisor_FizzBuzzTuples.Add(new Tuple<int,string>(3,"Fizz"));
Divisor_FizzBuzzTuples.Add(new Tuple<int, string>(5, "Buzz"));
Divisor_FizzBuzzTuples.Add(new Tuple<int, string>(7, "Pi"));

Func<int, string> FizzBuzzify = i => GetFizzBuzzFromNumber(i);
var fizzBuzzList = Enumerable.Range(rangeStart, rangeEnd).Select(FizzBuzzify);
Console.Write(String.Join("\n", fizzBuzzList).TrimEnd("\n".ToCharArray()));

}

static string GetFizzBuzzFromNumber(int number)
{
StringBuilder fizzbuzzbuilder = new StringBuilder();

var list = Divisor_FizzBuzzTuples.OrderBy(i => i.Item1).Distinct();
foreach (var tuple in list)
{
if(number % tuple.Item1 == 0)
{
fizzbuzzbuilder.Append(tuple.Item2);
}

}

string fizzbuzzornah = String.IsNullOrEmpty(fizzbuzzbuilder.ToString()) ? number.ToString() : fizzbuzzbuilder.ToString();

return fizzbuzzornah;

}
}

Also, at the end of date night, my wife and I actually played Fizz Buzz, which she knew as Bizz Buzz from high school. She’s much better than I am but she actually helped me to relax and do better than I thought I could. With her help, I might even be able to be a Scrabble Grand Master!

  • Of course you had to stop. The new code is so verbose that you could code yourself into infinity. We should have a coding challenge around this.

    • Dale Ross

      :). I was wondering if I inspired the post in JSDA.

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com