Sep 27, 2017

Here’s another journey through the world of simple programming problems… FizzBuzz, T-SQL edition. So just like the rest of the Fizz Buzz solutions. The idea is simple, for every number 1-100, if it’s divisible by 3 print ‘fizz’, if it’s divisible by 5 print ‘buzz’, if both print ‘fizzbuzz’, otherwise print the number. So here is a basic SQL solution:


WITH ctCounter AS
	SELECT 1 AS Number


	SELECT Number + 1 AS Number
	FROM ctCounter
	WHERE Number < 100
SELECT ISNULL(NULLIF(CONCAT(fizz.f, buzz.b), ''), c.Number) FROM ctCounter c
LEFT JOIN (VALUES('fizz')) fizz(f)
  ON c.Number % 3 = 0
LEFT JOIN (VALUES('buzz')) buzz(b)
  ON c.Number % 5 = 0

This is a common table expression, which generates a row for each number between 1 and 100. Then joins with the values ‘fizz’ and ‘buzz’. After that, it concatenates the fizz and buzz together, and if it still results in an empty string, selects the original number. The NULLIF function is useful for that, because it is basically the inverse of ISNULL. If the first parameter matches the second parameter, it returns null, otherwise returns the first parameter.

Ever been asked FizzBuzz in a programming interview, and thought to yourself, what could I do here that would really throw them off? Well, this. Do this.


private static void FizzBuzz1()
	// Declare all of our variables right here at the top.
	int i, i2;
	string buff;
	Action<string> fb = null;
	goto assignVars;
		if (0 == i2 % 3)
		goto dofizz;
		if (0 == i2 % 5)
		goto dobuzz;
		goto donumber;
	if (i --> 0 && (i2 = 100 - i) > 0)
	goto loop;
	goto end;
	buff += "fizz";
	if (0 == i2 % 5)
	goto dobuzz;
	goto writer;
	buff += "buzz";
	goto writer;
	buff += i2;
	fb(buff + Environment.NewLine);
	goto justBuff;
	// Do this safely at the end of the function.
	i = 100;
	fb = Console.Write;
	buff = string.Empty;
	goto endoftheloop;
	end: ;

Nobody can be tooooo mad, since it really is a completely working fizzbuzz. Some may question who taught you that goto works in c#, and why you dont seem to know how to use a for-loop. Some may even question why you assigned Console.Write to an action, or why you didn't just use Console.WriteLine. All good questions. Seems like you would have a lot to talk about during your interview.

Theres nothing like a single method chain which does all of Fizz Buzz. So here's some awesome.


private static void FizzBuzz2()
      Enumerable.Range(1, 100)
          i =>
              i % 3 == 0 ? "fizz" : null,
              i % 5 == 0 ? "buzz" : null,
              !(i % 5 == 0 || i % 3 == 0) ? $"{i}" : null))));

So lets say you are on a flight from DC to Seattle, and you forgot to download music to your phone. What a crappy hypothetical situation. Luckily, you have your trusty laptop, and a lack of creativity. So here we go, lets write FizzBuzz in powershell. The rules are simple. For all of the numbers between 1 and 100:

  • If the number is divisible by 3, print 'fizz'
  • If the number is divisible by 5, print 'buzz'
  • If the number is divisible by both 3 and 5, print 'fizzbuzz'
  • Otherwise print the number

Simple enough. The program practically writes itself. What recruiter wouldn't be impressed by fizzbuzz in powershell.


for($i = 1; $i -le 100; $i++)
    if($i % 3 -eq 0)
        Write-Host 'fizz' -NoNewline
    if($i % 5 -eq 0)
        Write-Host 'buzz' -NoNewline
    elseif($i % 3 -ne 0)
        Write-Host $i -NoNewline

Great that took about 30 seconds. So let's try something a little harder. Lets squish this into one confusing line. That's what scripting languages are for anyway right? You aren't cool until you can sqeeze a script down into a tiny confusing line of code.


(1..100) | ForEach-Object { $out = $null; if($_ % 3 -eq 0){$out = 'fizz'} if($_ % 5 -eq 0){$out += 'buzz'} if(-not $out){$out = $_} $out}

Thats better. But I don't think its confusing enough yet. Lets use some of the preset aliases: '%' and 'oh'. '%' is short for "ForEach-Object" and 'oh' is short for "Out-Host". Where does that put us?



Now that's a mess. I am sure we can make this even shorter, but I am a little too sleepy now. Anyway, there is the elusive 1 liner powershell fizzbuzz solution.

© 2019 - Peter Sulucz | disclaimer

log in