August 3, 2007 at 4:31 am
Ok. We reach the agreement about Andrew's "concat' logic, yes?
The answer to your question, IMHO, really simple: sequentially. 3 times. It's the same as replace with the last one.
August 3, 2007 at 4:43 am
First off, everyone is taking this FAR too seriously.
That said, most of the functional solutions I've seen work fine for numbers divisible by 15. Inherently anything divisible by 15 is going to be both divisible by 3 and by 5. Numbers are fun that way. There is no requirement to explicitly state anything about 15 in the logic of the question (which, say it with me, is a simple test, not the be all and end all of coding knowledge).
I agree with Adam by the way, ELSE is a perfectly functional mechanism.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
August 3, 2007 at 4:54 am
Poor logic!
Did you suggest CASE?
Case will pick 1st match, it will never come to the last one.
> The answer to your question, IMHO, really simple: sequentially
How is this?
Can you show how you "sequentual" answer looks like?
_____________
Code for TallyGenerator
August 3, 2007 at 5:48 am
1. CASE:
case
sign(@i%5) + 2*sign(@i%3)
when 0 then 'BizzBuzzBizzBuzz'
when 1 then 'Bizz'
when 2 then 'Buzz'
when 3 then cast(@i as varchar(10))
end
2. sequential
declare
@replaced varchar(20)
set
@replaced = 15
set
@replaced = replace(@replaced, @replaced, 'Bizz')
set
@replaced = replace(@replaced, @replaced, 'Buzz')
set
@replaced = replace(@replaced, @replaced, 'BizzBuzz')
@replaced
August 3, 2007 at 6:20 am
Exactly my point! "Where do you see - "divisible by 3 but not divisible by 5"? Everyone just assumed that each number must fall into 1 and only 1 case, the question however does not imply this.
O yeah, about the concat, if you look at my solution I dont concat anything!
Bottom line the specs were ambiguous and if you looked at the question, before looking at some of the solutions, the article asked you to do so, then you may have strayed from the group, as I did.
August 3, 2007 at 7:53 am
I kind of hate these silly quibbles, especially over something trivial, but... The statement did in fact imply that only one case should be applied. It did not state that only one case should be applied but relied on the implication. Not that it really and truly matters a whit.
Everyone repeat to themselves: It was just a simple exercise. It was just a simple exercise. It was just a ....
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
August 3, 2007 at 8:47 am
Actually, the directions do state quite implicitly that it can only fall into one category. It uses the word "substitute". When you substitute for something, the original thing is no longer present and therefore can NOT be substituted for again.
For example, consider this piece of code:
Declare @Number varchar(10)
Set @Number = 15
If @Number % 3 = 0 Then Replace(@Number, @Number, 'Bizz')
If @Number % 5 = 0 Then Replace(@Number, @Number, 'Buzz')
If @Number % 15 = 0 Then Replace(@Number, @Number, 'BizzBuzz')
See? Once you make the first subtitution, the number is no longer there to be substituted.
August 3, 2007 at 9:34 am
Good point, Im convinced, I would have failed the interview...darn!
This is just an excercise, This is just an excercise, This is just an exercise...
August 3, 2007 at 10:06 am
This thread should be printed out and preserved in acrylic. It was as much a Rorschach test as a SQL test. The responses perfectly illustrate many of the good traits of programmers (logic, knowledge of SQL, documentation, making queries more efficient, persistence) with several of the bad ones (the obsessive nitpicking that is the mixed blessing of the IT personality, condescending and unconstructive criticism of mistaken approaches, and taking things too seriously).
All in all, an instant classic.
Thanks again, Grant!
-------------------
A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
August 3, 2007 at 12:49 pm
If nothing else, it certainly was a thought-provoking, compelling article. Very few of these things even come close to double digit pages of responses.
August 3, 2007 at 1:43 pm
Agree.
I got even more from the discussion than from the original article.
It means that article is really good.
August 3, 2007 at 4:12 pm
August 3, 2007 at 4:17 pm
No.
'BizzBuzz'
August 3, 2007 at 4:31 pm
Yes, it's quite obvious that only one case should be applied.
But which one?
Computer does not do any implications.
Unless you specify the rules for it.
Andrew demonstrated the very computer-like way of thinking.
And very non-trivial one.
Grant, it's interesting, would such guy pass your test?
Because I would stop test right there and start discuss his working hours, etc.
And I would not be interested if he can write a single row in SQL or C# without syntax errors.
_____________
Code for TallyGenerator
August 3, 2007 at 6:43 pm
Sergiy, I'm trying to understand your point.
1. The guy himself is already convinced:
"Im convinced, I would have failed the interview...darn!"
2. Two people provided identical snippets that should explain you sequential replacement logic.
3. You were not exactly right about CASE.
Now. You're still protecting the logic from original Andrew's post, despite that even he agreed that it's wrong.
Viewing 15 posts - 166 through 180 (of 309 total)
You must be logged in to reply to this topic. Login to reply