FizzBuzz (#126)

In message
[email protected], “Todd
Benson” writes:

Just build a self-sustaining computer complete with time-travel
capabilities, programmed to reboot a few eons every once and a while,
and, poof there you go … an instant solution to any NP problem! I
wonder if Turing thought of such things.

Asimov did. :slight_smile:

-s

On 6/3/07, James Edward G. II [email protected] wrote:

On Jun 3, 2007, at 1:11 PM, Robert D. wrote:

On 6/3/07, [email protected] [email protected] wrote:

puts %w{ 1 2 Fizz 4 … }
.join(“\n”)

I think you were kidding, but just to be clear, the join() call is
not needed here.
No I was not, I was just wrong :slight_smile:
Robert

“Robert D.” [email protected] wrote in message

Robert, you are not kind to guys who will maintain your code.
That could be discussed, but this is a small entity, from my
experience maintenance nightmares come from bad design and code
dependencies.
Sure, you are right.
But please don’t forget, that ‘small entity’ can cause ‘big problem’ if
muliplyed by huge number.
Given 100 average-level programmers, how many will be able to expand
your
solution
to handle addition of one more multiplyer, say 7-Gozz?
Enterprise managers face luck of experienced programmers to execute
routine
maintenance tasks,
so simple solution for simple task is a must.

Do you want the job or demonstration of your smartness?
This is not very smart a solution, it reflects some features of Ruby,
after all they are hiring a Ruby developer not a C or Perl developer.
I didn’t say it was smart solution :slight_smile:

imho: in any situation: KISS!
This is strong an expression, I will not take offense, others might.
I’m terrybly sorry, no offense was intended (I forgot to mention this
specifically in first place)!
ashamed,
Sergey V.

On 6/3/07, Joshua B. [email protected] wrote:

Oh, and just to clarify my earlier suggestion of a 56 chr random code
generator…my rough guestimate is that it would take all the computers

Ah, but another approach which might get there faster is to start with
a working small solution and then randomly mutate it until a shorter
correct solution was found.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On 6/3/07, S.Volkov [email protected] wrote:

solution
to handle addition of one more multiplyer, say 7-Gozz?
Enterprise managers face luck of experienced programmers to execute routine
maintenance tasks,
so simple solution for simple task is a must.
But what is simple?
(1…100).each{ |x|
print “Fuzz” if x%3 ==0
print “Bizz” if x%5
print x unless x%3*y%5==0
puts “”
}
Is this simple?
Maybe you are right about the must but then I feel that you are
wrong about the language.
I feel very strongly that Ruby is not a language for such code…
Let us maybe take a break, wait for James’ resume and what other think
about this and than we might find a better understanding of this.
Boy who said this was a simple Quiz :wink:

Do you want the job or demonstration of your smartness?
This is not very smart a solution, it reflects some features of Ruby,
after all they are hiring a Ruby developer not a C or Perl developer.
I didn’t say it was smart solution :slight_smile:
Hmm I do :wink:

imho: in any situation: KISS!
This is strong an expression, I will not take offense, others might.
I’m terrybly sorry, no offense was intended (I forgot to mention this
specifically in first place)!
ashamed,
No that is exaggerated of course as I said no offense taken.
Sergey V.
Do you like Star Trek?
Reminds me of the famous dialog between bones and Spock:
I am Spock you are bones :wink:

Robert

Sergey it is your fault if I cannot sleep ;), is this more readable ?

module Enumerable
def change(to, &blk)
map{ |x| blk.call(x) ? to : x }
end
end # module Enumerable

puts (1…100).change(:FizzBuzz){ |x| x%15 == 0 }.change(:Fizz){ |x|
x%3 == 0 rescue false}.change(:Buzz){ |x| x%5 == 0 rescue false }

Here’s my solution. It makes heavy use of the Ruby idiom of using the
&& and || operators to perform conditionals because a) they are short-
circuited and because b) && returns the right-hand-side when the left-
hand-side is a true value.

====
class Integer; def factor? n; self % n == 0; end; end

puts (1…100).map { |i| i.factor?(15)&&“FizzBuzz” || i.factor?
(3)&&“Fizz” || i.factor?(5)&&“Buzz” || i }

Eric

Are you interested in on-site Ruby training that uses well-designed,
real-world, hands-on exercises? http://LearnRuby.com

----- Original Message -----
From: “Robert D.” [email protected]
To: “ruby-talk ML” [email protected]
Sent: Sunday, June 03, 2007 6:38 PM
Subject: Re: [QUIZ] FizzBuzz (#126)

Sergey it is your fault if I cannot sleep ;), is this more readable ?
relax, Robert, nobody hurt :slight_smile:

module Enumerable
def change(to, &blk)
map{ |x| blk.call(x) ? to : x }
end
end # module Enumerable

puts (1…100).change(:FizzBuzz){ |x| x%15 == 0 }.change(:Fizz){ |x|
x%3 == 0 rescue false}.change(:Buzz){ |x| x%5 == 0 rescue false }
hard to make formal conclusions - there are no formal requirements,
nor context is well defined, I can share my own opinion and potential
concerns only:

  • yes, indeed, this solution is more readable,
    compared to your Array-nested-tricky-indexed original solution;
  • clear functional decomposition simplifies understanding, maintenance;
  • it is easily expandable thanks to chain processing;
    btw: I prefer different name for Enumerable method, ex: substitute_if;

Not so attractive in this solution:

  • run-time/memory inefficient: each item is generates 4 times;
  • memory inefficient: generates and stores in memory 4*original array
    (ex: not well suited for 10**9 length array);
  • can not be adopted for stream processing (read - process - print);
  • ‘rescue’ is for exceptional processing, should be avoided in normal
    control flow;

Sergey V.

irb(main):282:0> a = (1…100).inject([]){|hash, x| hash[x-1] = x unless
x%3
==0 || x%5==0 || x%15==0 ; hash[x-1] = ‘Fizz’ if x%3==0 ; hash[x-1] =
‘Buzz’
if x%5==0 ; hash[x-1] = ‘FizzBuzz’ if x%15==0 ; hash}

irb(main):283:0> a.each{|x| p x}

I like one line coding in ruby ^^

and sorry about my parameter name “hash” lazy to change >.<

Hi –

On Mon, 4 Jun 2007, Robert D. wrote:

puts ‘59’
31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44
Or:

puts %w{ 1 2 Fizz 4 … }
.join("\n")

You don’t need that though; puts will puts each element in the array.

David

Attached are my 7 attempts. Some are ok, some are crap. No golfing.

Okay, one more solution that just came to mind:

1.upto(100) do |i|
f, b = (i % 3).zero?, (i % 5).zero?
puts “#{‘Fizz’ if f}#{‘Buzz’ if b}#{i unless (f or b)}”
end

My solution, which is probably pretty similar to everyone else’s
solution, but I didn’t feel like being creative this weekend:

1.upto(100) do |i|
puts case i % 15
when 0 then “FizzBuzz”
when 5, 10 then “Buzz”
when 3, 6, 9, 12 then “Fizz”
else i
end
end

On Jun 1, 2007, at 10:37 PM, Morton G. wrote:

On Jun 1, 2007, at 8:28 AM, Ruby Q. wrote:

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”.

I would not be surprised if this quiz produces the most submissions
of any quiz ever.

Good prediction. You were right.

James Edward G. II

On 6/3/07, Sergey V. [email protected] wrote:

module Enumerable

  • yes, indeed, this solution is more readable,
    compared to your Array-nested-tricky-indexed original solution;
  • clear functional decomposition simplifies understanding, maintenance;
  • it is easily expandable thanks to chain processing;
    I feel good :wink:
    btw: I prefer different name for Enumerable method, ex: substitute_if;
    agreed and alias to change_if then there will be a
    substitute/change_unless too.

Not so attractive in this solution:

  • run-time/memory inefficient: each item is generates 4 times;
  • memory inefficient: generates and stores in memory 4*original array
    Good point a change_if! might have been better.
    (ex: not well suited for 10**9 length array);
    Too bad :wink:
  • can not be adopted for stream processing (read - process - print);
    good point, but is that not the price to pay for functional
    decomposition?
  • ‘rescue’ is for exceptional processing, should be avoided in normal
    control flow;
    I gotta think about this, I tend to agree for
    begin
    rescue …
    end
    the inline form seems too compact for that and I do not consider it
    control flow but rather a logical operator like
    expression or_if_that_does_not_work other
    but you do have a point.

Thx for your thoughts AAMOF you make me think that the discussion of
your solution with the recruiters is very important too.

Cheers
Robert

On Jun 3, 2007, at 11:22 PM, James Edward G. II wrote:

I would not be surprised if this quiz produces the most
submissions of any quiz ever.

Good prediction. You were right.

Well, if I’m on a roll, perhaps you’ll tolerate some further semi-
random remarks about this quiz.

I guess a lot of us want new jobs.

I really like Fred P.’ solution. A fine example of pursuing the
KISS principle right into the ground. I wish I’d thought of it.

I don’t envy you your job of having to summarize all these submissions.

Are the above remarks facetious? Mostly but not entirely.

Regards, Morton

On Jun 1, 7:28 am, Ruby Q. [email protected] wrote:

The three rules of Ruby Q.:

Interesting quiz, as usual.

I don’t really have experience applying for jobs, but my own
experience tells me that when writing code, you should write it for
others to read it, change it, manage it and/or throw it away.

The first question that jumps into my mind is: why would a job
interviewer wanted me to write me such a program? What the heck is a
‘FizzBuzz’?

Probably is a company secret. To me, it looks like a little useless
noise coming from somewhere.

Class that takes numbers and transforms them into a symbol

specified for the user

class UselessSoundMaker

attr_accessor :sounds

def initialize(args)
@sounds = args
end

def add_sounds(args)
@sounds.update(args)
end

def display(range = 1…100)
for number in range
if (multiples = @sounds.keys.select{|m| number % m == 0}).empty?
puts number
else
puts multiples.sort.collect{|m| @sounds[m]}.join
end
end
end

end

fizzbuzz = UselessSoundMaker.new(3 => ‘Fizz’, 5 => ‘Buzz’)
fizzbuzz.display

foobar = UselessSoundMaker.new(3 => ‘Foo’, 4 => ‘Bar’, 5 => ‘Baz’)
foobar.display(1…50)

beepbeep = UselessSoundMaker.new(10 => “\a”)
beepbeep.display(1…100)

Thanks again for the quizes.

Ruben M…


Dictionary is the only place that success comes before work. - Vince
Lombardi

On 6/4/07, Morton G. [email protected] wrote:

On Jun 3, 2007, at 11:22 PM, James Edward G. II wrote:

I really like Fred P.’ solution.
Honestly the idea is awesome, but he should have taken care to get the
syntax right. I think it is not a good solution in the exact context
but a good distribution for us to have fun.
A fine example of pursuing the
KISS principle right into the ground. I wish I’d thought of it.
The KISS principle was already embraced by nobody less than Albert
Einstein who said “As simple(*) as possible, but not simpler!”
the last part being quite irrelevant in our case.
BUT does the merciless application of KISS give you the warm fuzzy
feeling?
I believe - idiotically and idealistically ( they often come in pairs
) - that this warm fuzzy feeling is necessary to be productive. So
emotionally I dislike KISS somehow but I have to admit that I learnt a
lot while being put on my place :wink:

I don’t envy you your job of having to summarize all these submissions.

Sure and even more so, as we are already discussing solutions, which
have never seen before, and might even be bad form when I think about
it -maybe with the exception of the one liners. But this Quiz is
special :slight_smile:

Knowing James a little bit he will find a practical approach making
his life a little bit easier and still giving a good resume, if he is
doing it himself BTW.

Are the above remarks facetious? Mostly but not entirely.

Regards, Morton

(*) I daresay that “simple” is not well defined though :slight_smile:

Cheers
Robert

James Edward G. II [email protected] writes:

On Jun 1, 2007, at 10:37 PM, Morton G. wrote:

I would not be surprised if this quiz produces the most submissions
of any quiz ever.

Good prediction. You were right.

And yet, no one has so far posted a solution involving callcc or
threads. (unless I missed it)

I may have to go back for another round of interviews at
CompuGlobalMegaTech…

I’ll add my solutions to the thundering horde. My first solution was
moderately clever:

puts
(1…100).map{|n|[[3,‘fizz’],[5,‘buzz’]].inject(n){|s,a|s.is_a?(Fixnum)?s
=a[1]:s+=a[1] if n%a[0]==0;s}}

then I tried to use as few characters as possible:

puts (1…100).map{|n|n%15==0?:fizzbuzz:n%5==0?:buzz:n%3==0?:fizz:n}
puts (1…100).map{|n|n%15>0?n%5>0?n%3>0?n:‘fizz’:‘buzz’:‘fizzbuzz’}

  • donald