SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Site Related Forums
 The Yak Corral
 FizzBuzz
 New Topic  Reply to Topic
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

rockmoose
SQL Natt Alfen

Sweden
3279 Posts

Posted - 03/01/2007 :  14:38:43  Show Profile  Reply with Quote
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

Slovenia
11751 Posts

Posted - 03/02/2007 :  04:26:53  Show Profile  Visit spirit1's Homepage  Reply with Quote
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

Sweden
30282 Posts

Posted - 03/02/2007 :  05:42:34  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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

Edited by - SwePeso on 03/02/2007 06:13:14
Go to Top of Page

jezemine
Flowing Fount of Yak Knowledge

USA
2886 Posts

Posted - 03/02/2007 :  09:30:46  Show Profile  Visit jezemine's Homepage  Reply with Quote
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

Sweden
3279 Posts

Posted - 03/02/2007 :  09:41:38  Show Profile  Reply with Quote
[:8]

I got mine down to 204 characters after heavy stripping

rockmoose
Go to Top of Page

jsmith8858
Dr. Cross Join

USA
7423 Posts

Posted - 03/02/2007 :  10:04:34  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
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

Edited by - jsmith8858 on 03/02/2007 10:10:51
Go to Top of Page

rockmoose
SQL Natt Alfen

Sweden
3279 Posts

Posted - 03/02/2007 :  11:55:04  Show Profile  Reply with Quote
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

USA
7423 Posts

Posted - 03/02/2007 :  12:45:28  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
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

Edited by - jsmith8858 on 03/02/2007 12:46:12
Go to Top of Page

rockmoose
SQL Natt Alfen

Sweden
3279 Posts

Posted - 03/02/2007 :  12:52:55  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
3564 Posts

Posted - 03/02/2007 :  13:31:43  Show Profile  Visit jhermiz's Homepage  Reply with Quote
" 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 -- http://www.web-impulse.com

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

jsmith8858
Dr. Cross Join

USA
7423 Posts

Posted - 03/02/2007 :  14:30:41  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
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

Edited by - jsmith8858 on 03/02/2007 14:31:38
Go to Top of Page

jezemine
Flowing Fount of Yak Knowledge

USA
2886 Posts

Posted - 03/02/2007 :  14:37:46  Show Profile  Visit jezemine's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
3564 Posts

Posted - 03/02/2007 :  14:50:51  Show Profile  Visit jhermiz's Homepage  Reply with Quote
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 -- http://www.web-impulse.com

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

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 03/02/2007 :  18:58:14  Show Profile  Reply with Quote


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
Flowing Fount of Yak Knowledge

USA
1837 Posts

Posted - 03/02/2007 :  19:46:02  Show Profile  Reply with Quote
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

Edited by - snSQL on 03/02/2007 19:49:36
Go to Top of Page

jezemine
Flowing Fount of Yak Knowledge

USA
2886 Posts

Posted - 03/02/2007 :  20:10:56  Show Profile  Visit jezemine's Homepage  Reply with Quote
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

USA
7423 Posts

Posted - 03/02/2007 :  20:21:21  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
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

Edited by - jsmith8858 on 03/02/2007 20:30:34
Go to Top of Page

snSQL
Flowing Fount of Yak Knowledge

USA
1837 Posts

Posted - 03/03/2007 :  14:57:35  Show Profile  Reply with Quote
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)

USA
7020 Posts

Posted - 03/03/2007 :  17:44:55  Show Profile  Reply with Quote
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)

USA
7020 Posts

Posted - 03/03/2007 :  17:54:22  Show Profile  Reply with Quote
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

Sweden
30282 Posts

Posted - 03/03/2007 :  23:45:23  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.23 seconds. Powered By: Snitz Forums 2000