Author |
Topic |
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2007-03-01 : 14:38:43
|
http://golf.shinh.org/p.rb?FizzBuzzSQL FizzBuzz:declare @c varchar(413),@i int;select @c='',@i=1while @i<101select @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+1select @c The challenge is to write as short and fast code as possible...rockmoose |
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
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 LarssonHelsingborg, Sweden |
 |
|
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 |
 |
|
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 |
 |
|
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. - Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
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 endrockmoose |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-03-02 : 12:45:28
|
that is hilarious, I had no idea that was legal...declare @ intset @ = 3select @ 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)goselect * from [ ]godrop table [ ] Think of the database we could design using these tricks !!!! Completely unmaintainable !- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
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 |
 |
|
jhermiz
3564 Posts |
Posted - 2007-03-02 : 13:31:43
|
" ok this whole fizzbuzz thing is ridicolousit all kind of started here:http://www.codinghorror.com/blog/archives/000804.htmlit'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] |
 |
|
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. - Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
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 @ intset @ = 3select @ 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)goselect * from [ ]godrop table [ ] Think of the database we could design using these tricks !!!! Completely unmaintainable !- Jeffhttp://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 |
 |
|
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. - Jeffhttp://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] |
 |
|
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 endThan 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 |
 |
|
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 sillydeclare @ 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 |
 |
|
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%29be sure to post your solutions here when you are done.  www.elsasoft.org |
 |
|
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- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
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 SQLFizzBuzzFROM ( 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 S1CROSS 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 S2ORDER BY Num1 + Num2 |
 |
|
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- Jeffhttp://weblogs.sqlteam.com/JeffS
Very high NZDF factor also.CODO ERGO SUM |
 |
|
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 SQLFizzBuzzFROM ( 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 S1CROSS 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 S2ORDER 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 SQLFizzBuzzfrom F_TABLE_NUMBER_RANGE(1,100) CODO ERGO SUM |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-03 : 23:45:23
|
[code]declare @ intset @=1while @<101beginprint ltrim(LEFT(REPLICATE(str(@),(@%3)*(@%5))+REPLICATE(' Fizz',@%5)+REPLICATE(' Buzz',@%3)+' FizzBuzz',10))set @=@+1end[/code]Peter LarssonHelsingborg, Sweden |
 |
|
Next Page
|