Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Site Related Forums
 The Yak Corral
 FizzBuzz

Author  Topic 

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2007-03-01 : 14:38:43
http://golf.shinh.org/p.rb?FizzBuzz

SQL FizzBuzz:

declare @c varchar(413),@i int;select @c='',@i=1
while @i<101
select @c=@c+case @i%3 when 0 then case @i%5 when 0 then 'FizzBuzz' else 'Fizz' end else case @i%5 when 0 then 'Buzz' else ltrim(@i) end end+char(10),@i=@i+1
select @c


The challenge is to write as short and fast code as possible...

rockmoose

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-03-02 : 04:26:53
ok this whole fizzbuzz thing is ridicolous

it all kind of started here:
http://www.codinghorror.com/blog/archives/000804.html

it's a bit silly...

Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-02 : 05:42:34
Here is a 71 char shorter code (155 in total). I managed to squeeze your's into 226 chars in total.
declare @p int set @p=1while @p<101begin print ISNULL(NULLIF(CASE WHEN @p%3=0THEN'Fizz'ELSE''END+CASE WHEN @p%5=0THEN'Buzz'ELSE''END,''),@p)set @p=@p+1 end

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-03-02 : 09:30:46
i prefer this site for coding challenges actually, much more amusing: http://www.ioccc.org


www.elsasoft.org
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2007-03-02 : 09:41:38
[:8]

I got mine down to 204 characters after heavy stripping

rockmoose
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-03-02 : 10:04:34
A "real" programmer would never worry about different ways of writing the fizzbuzz code, that was the entire point of his article.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2007-03-02 : 11:55:04
Well it's for fun, and we can't let FizzBuzz go without an sql implementation

Peso drop the p and loose another 8 characters:
declare @ int set @=1while @<101begin print ISNULL(NULLIF(CASE WHEN @%3=0THEN'Fizz'ELSE''END+CASE WHEN @%5=0THEN'Buzz'ELSE''END,''),@)set @=@+1 end

rockmoose
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-03-02 : 12:45:28
that is hilarious, I had no idea that was legal...

declare @ int
set @ = 3
select @

who would have thunk it ..... I guess it is kind of like creating a table name with only spaces, which is legal as well....

create table [ ] (i int)
go
select * from [ ]
go
drop table [ ]


Think of the database we could design using these tricks !!!! Completely unmaintainable !

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2007-03-02 : 12:52:55
Yes, it's a feature
same goes with: create table #(i int)

If you really want to save typing ;)

rockmoose
Go to Top of Page

jhermiz

3564 Posts

Posted - 2007-03-02 : 13:31:43
" ok this whole fizzbuzz thing is ridicolous

it all kind of started here:
http://www.codinghorror.com/blog/archives/000804.html

it's a bit silly..."

Great link...

States:

quote:

An example of a Fizz-Buzz question is the following:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes.

Want to know something scary ? - the majority of comp sci graduates can’t. I’ve also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.

I’m not saying these people can’t write good code, but to do so they’ll take a lot longer to ship it. And in a business environment that’s exactly what you don’t want.

This sort of question won’t identify great programmers, but it will identify the weak ones. And that’s definitely a step in the right direction.




Woohooo doesn't apply to me:



using System;
using System.Collections.Generic;
using System.Text;

namespace FizzBuzz
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Beginning app...");

for(int i = 1; i<=100; i++)
{
if (((i % 3)==0) && ((i % 5)==0))

Console.WriteLine("FizzBuzz!");

else if ((i % 3)==0)

Console.WriteLine("Fizz");

else if ((i % 5)==0)

Console.WriteLine("Buzz");

else
Console.WriteLine(i);
}
Console.ReadKey();
}
}
}



Sure they are basic but it sure brings back university memories..CSC 1100, this one's for you Richard Weinand!!!


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-03-02 : 14:30:41
From the link:

FizzBuzz was presented as the lowest level of comprehension required to illustrate adequacy. There's no glory to be had in writing code that establishes a minimum level of competency. Even if you can write it in five different languages or in under 50 bytes of code.

The whole point of the original article was to think about why we have to ask people to write FizzBuzz. The mechanical part of writing and solving FizzBuzz, however cleverly, is irrelevant. Any programmer who cares enough to read programming blogs is already far beyond such a simple problem. FizzBuzz isn't meant for us. It's the ones we can't reach-- the programmers who don't read anything-- that we're forced to give the FizzBuzz test to.



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-03-02 : 14:37:46
quote:
Originally posted by jsmith8858

that is hilarious, I had no idea that was legal...

declare @ int
set @ = 3
select @

who would have thunk it ..... I guess it is kind of like creating a table name with only spaces, which is legal as well....

create table [ ] (i int)
go
select * from [ ]
go
drop table [ ]


Think of the database we could design using these tricks !!!! Completely unmaintainable !

- Jeff
http://weblogs.sqlteam.com/JeffS




i remember fixing a bug in SSMS where the create database dialog failed if someone tried to create a database with the name of ' '. yep, that's a space. Looks good in OE. hard to distinguish from ' ' though.


www.elsasoft.org
Go to Top of Page

jhermiz

3564 Posts

Posted - 2007-03-02 : 14:50:51
quote:
Originally posted by jsmith8858

From the link:

FizzBuzz was presented as the lowest level of comprehension required to illustrate adequacy. There's no glory to be had in writing code that establishes a minimum level of competency. Even if you can write it in five different languages or in under 50 bytes of code.

The whole point of the original article was to think about why we have to ask people to write FizzBuzz. The mechanical part of writing and solving FizzBuzz, however cleverly, is irrelevant. Any programmer who cares enough to read programming blogs is already far beyond such a simple problem. FizzBuzz isn't meant for us. It's the ones we can't reach-- the programmers who don't read anything-- that we're forced to give the FizzBuzz test to.



- Jeff
http://weblogs.sqlteam.com/JeffS




Exactly jeff, but the mere post shows that many programmers who boost their egos and fill that resume cannot even write the simpliest piece of code!


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-03-02 : 18:58:14


Slightly shorter:
declare @ char(3)set @=0while @<100begin set @=@+1print case when @%15=0then'FizzBuzz'when @%3=0then'Fizz'when @%5=0then'Buzz'else @ end end

Than this:
declare @ int set @=1while @<101begin print ISNULL(NULLIF(CASE WHEN @%3=0THEN'Fizz'ELSE''END+CASE WHEN @%5=0THEN'Buzz'ELSE''END,''),@)set @=@+1 end





CODO ERGO SUM
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-03-02 : 19:46:02
This one is 142 characters, but you're right it's getting silly

declare @ char(3) set @=1while @<101begin print case when @%15=0then'FizzBuzz'when @%3=0then'Fizz'when @%5=0then'Buzz'else @ end set @=@+1 end
by Michael Valentine Jones
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-03-02 : 20:10:56
you think that's silly?

try implementing FizzBuzz in WhiteSpace: http://en.wikipedia.org/wiki/Whitespace_%28programming_language%29

be sure to post your solutions here when you are done.


www.elsasoft.org
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-03-02 : 20:21:21
This is definitely silly. but I'll bite. here's one without a CASE that's 135 characters ....

declare @ int set @=0while @<100begin set @=@+1 print left(@,sign(@%3*@%5)*3)+left('fizz',4-sign(@%3)*4)+left('buzz',4-sign(@%5)*4)end

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-03-03 : 14:57:35
Of course, we should really not be writing T-SQL for this that uses variables and loops and print statements. With no attempt to keep it short:

SELECT case when (Num1 + Num2) % 15 = 0 then 'FizzBuzz'
when (Num1 + Num2) % 3 = 0 then 'Fizz'
when (Num1 + Num2) % 5 = 0 then 'Buzz'
else cast(Num1 + Num2 as varchar(10)) end AS SQLFizzBuzz
FROM (
SELECT 1 Num1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10
) AS S1
CROSS JOIN (
SELECT 0 Num2 UNION ALL
SELECT 10 UNION ALL
SELECT 20 UNION ALL
SELECT 30 UNION ALL
SELECT 40 UNION ALL
SELECT 50 UNION ALL
SELECT 60 UNION ALL
SELECT 70 UNION ALL
SELECT 80 UNION ALL
SELECT 90
) AS S2
ORDER BY Num1 + Num2
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-03-03 : 17:44:55
quote:
Originally posted by jsmith8858

This is definitely silly. but I'll bite. here's one without a CASE that's 135 characters ....

declare @ int set @=0while @<100begin set @=@+1 print left(@,sign(@%3*@%5)*3)+left('fizz',4-sign(@%3)*4)+left('buzz',4-sign(@%5)*4)end

- Jeff
http://weblogs.sqlteam.com/JeffS




Very high NZDF factor also.



CODO ERGO SUM
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-03-03 : 17:54:22
quote:
Originally posted by snSQL

Of course, we should really not be writing T-SQL for this that uses variables and loops and print statements. With no attempt to keep it short:

SELECT case when (Num1 + Num2) % 15 = 0 then 'FizzBuzz'
when (Num1 + Num2) % 3 = 0 then 'Fizz'
when (Num1 + Num2) % 5 = 0 then 'Buzz'
else cast(Num1 + Num2 as varchar(10)) end AS SQLFizzBuzz
FROM (
SELECT 1 Num1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10
) AS S1
CROSS JOIN (
SELECT 0 Num2 UNION ALL
SELECT 10 UNION ALL
SELECT 20 UNION ALL
SELECT 30 UNION ALL
SELECT 40 UNION ALL
SELECT 50 UNION ALL
SELECT 60 UNION ALL
SELECT 70 UNION ALL
SELECT 80 UNION ALL
SELECT 90
) AS S2
ORDER BY Num1 + Num2




select
case
when number % 15 = 0 then 'FizzBuzz'
when number % 3 = 0 then 'Fizz'
when number % 5 = 0 then 'Buzz'
else left(number,3)
end SQLFizzBuzz
from
F_TABLE_NUMBER_RANGE(1,100)


CODO ERGO SUM
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-03 : 23:45:23
[code]declare @ int
set @=1
while @<101
begin
print ltrim(LEFT(REPLICATE(str(@),(@%3)*(@%5))+REPLICATE(' Fizz',@%5)+REPLICATE(' Buzz',@%3)+' FizzBuzz',10))
set @=@+1
end[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page
    Next Page

- Advertisement -