TSQL Fizz Buzz

Sep27.17 12AM

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:
raw
WITH ctCounter AS
(
	SELECT 1 AS Number

	UNION ALL

	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.



Some messy C# code.

Mar09.17 07AM

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.
raw
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;
	loop:
	{
		if (0 == i2 % 3)
		goto dofizz;
		if (0 == i2 % 5)
		goto dobuzz;
		goto donumber;
	}
	endoftheloop:
	if (i --> 0 && (i2 = 100 - i) > 0)
	goto loop;
	goto end;
	dofizz:
	buff += "fizz";
	if (0 == i2 % 5)
	goto dobuzz;
	goto writer;
	dobuzz:
	buff += "buzz";
	goto writer;
	donumber:
	buff += i2;
	writer:
	fb(buff + Environment.NewLine);
	goto justBuff;
	// Do this safely at the end of the function.
	assignVars:
	i = 100;
	fb = Console.Write;
	justBuff:
	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.
raw
private static void FizzBuzz2()
{
  Console.WriteLine(
    string.Join(
      Environment.NewLine,
      Enumerable.Range(1, 100)
        .Select(
          i =>
            string.Join(
              string.Empty,
              i % 3 == 0 ? "fizz" : null,
              i % 5 == 0 ? "buzz" : null,
              !(i % 5 == 0 || i % 3 == 0) ? $"{i}" : null))));
}
                            
                        



Powershell FizzBuzz

Jul05.16 12AM

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.
raw
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
    }
    Write-Host
}
                            
                        
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.
raw
(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?
raw
(1..100)|%({if(-not($_%3)){$$='fizz'}if(-not($_%5)){$$+='buzz'}if(-not$$){$$=$_}$$|oh;$$=$__})
                            
                        
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.



© 2017 - Peter Sulucz | disclaimer

log in