FizzBuzz (#126)

I didn’t want to post mine as there was to much golfing around :wink:

But ok…

Here would be my solution (I don’t like it specially, but I hope
it’ll do when the recruiters come :wink: ):

1.upto(100) do |number|
print “Fizz” if number % 3 == 0
print “Buzz” if number % 5 == 0
print number if number % 3 != 0 && number % 5 != 0
puts “”
end


Enrique Comba R.
[email protected]

I always thought Smalltalk would beat Java, I just didn’t know it
would be called ‘Ruby’ when it did.
– Kent Beck

If I want the job :wink:

X = [ %w{FizzBuzz} + %w{Fizz} * 4 ]
Y = %w{Buzz}
(1…100).each do |n|
puts( X[n%3][n%5]) rescue puts( Y[n%5]||n )
end

Well, It’s a few minutes early, but I’ve got an early morning tomorrow.

Here’s three attempts from me.

First is my initial shot, which will cause many of you to cringe at
the use of nested ternary operators…
Second, I decided to mess with Fixnum.
Then I got a little bored and thought of something for our friends at
thedailywtf (still can’t take to the new name)

I’m willing to go with whoever suggested this might bring in the
largest number of RubyQuiz responses yet.

Cheers,
Dave

#-------------------------------------------------
#Quick first attempt

1.upto(100) {|i| puts(i%15==0 ? ‘FizzBuzz’ : i%5==0 ? ‘Buzz’ : i%
3==0 ? ‘Fizz’ : i)}

#-------------------------------------------------
#Lets play with Fixnum

class Fixnum
def fizz_buzzed
a= (self%3==0 ? ‘Fizz’ : “”)
a+= ‘Buzz’ if self%5==0
a= self.to_s if a==“”
a
end
end

1.upto(100) {|i| puts i.fizz_buzzed}

#-------------------------------------------------

How about something for thedailywtf.com?

def divisible_by_3(i)
if i.to_s.size>1 then
divisible_by_3(i.to_s.split(//).map{|char| char.to_i}.inject{|
a,b| a+b})
else
[3,6,9].include?(i)
end
end

def divisible_by_5(i)
[‘0’,‘5’].include?(i.to_s[-1].chr)
end

def divisible_by_15(i)
divisible_by_3(i) && divisible_by_5(i)
end

1…100.each do |i|
if divisible_by_15(i) then puts ‘FizzBuzz’
elsif divisible_by_3(i) then puts ‘Fizz’
elsif divisible_by_5(i) then puts ‘Buzz’
else puts i
end
end

Hi everyone,

Here’s my solution. It’s the first one that occurred to me, and it
only took a few mins. to write.

(1…100).each do |n|
if (n % 3) == 0 && (n % 5) == 0
puts “FizzBuzz”
elsif (n % 3) == 0
puts “Fizz”
elsif (n % 5) == 0
puts “Buzz”
else
puts n
end
end

I tried it a few other ways just for fun. I’m happy with this one
that uses ‘case.’

(1…100).each do |n|
puts case
when (n % 3 == 0) && (n % 5) == 0: “FizzBuzz”
when n % 3 == 0: “Fizz”
when n % 5 == 0: “Buzz”
else n
end
end

Regards,
Craig

Here are my solutions to Quiz 126. Because it was the one that
popped into my mind when I read the quiz description, the first is
the one that I think I’d produce if challenged during a job interview.

# Simple, obvious (to me) way to do it. (1..100).each do |n| case when n % 15 == 0 : puts 'FizzBuzz' when n % 5 == 0 : puts 'Buzz' when n % 3 == 0 : puts 'Fizz' else puts n end end

Not quite so obvious, but a little DRYer:

(1…100).each do |n|
puts case
when n % 15 == 0 : ‘FizzBuzz’
when n % 5 == 0 : ‘Buzz’
when n % 3 == 0 : ‘Fizz’
else n
end
end

Of course, could do it with if-elsif-else …

(1…100).each do |n|
puts(
if n % 15 == 0
‘FizzBuzz’
elsif n % 5 == 0
‘Buzz’
elsif n % 3 == 0
‘Fizz’
else n
end
)
end

… or even with the ternary operator.

(1…100).each do |n|
puts(
n % 15 == 0 ? ‘FizzBuzz’ :
n % 5 == 0 ? ‘Buzz’ :
n % 3 == 0 ? ‘Fizz’ : n
)
end

Regards, Morton

that’s my clear and straight-forward solution, I would give on a job
interview:

#!/usr/bin/env ruby

$VERBOSE = true

1.upto(100) do |num|

case when num.modulo(3) == 0 && num.modulo(5) == 0 :puts
‘fizzbuzz’

     when num.modulo(3) == 0                            :puts 

‘fizz’

     when num.modulo(5) == 0                            :puts 

‘buzz’

     else                                                puts 

num
end

end


greets

                 one must still have chaos in oneself to be able to

give birth to a dancing star

and if I do not :wink:

require ‘zlib’
puts Zlib::GzipReader.new(DATA).read
END
?7`F☻♥ok.out ]ÃŽ;♫♥EÑÞ’↨cÀ|Ú¶ÙKâ–¬?fVY↨9ƒ☻.%ÛHܳæ
9z¯ësCæx¼¿-ó´9ü¾Yÿµî÷b∟9¯ßLq,çQw◄
"ÄuNÑ¡øßÆ(q¸É↑↔n6I>
#Å&(IÚ¤Ù6:»ÙvÚ6:»Ø¶%M>6>¢£Ífi3t¬Ø♀%K>5>£ãÍæist¼Ø☻¥H[4[ ‼Íö☺
ýØr²☺☻


You see things; and you say Why?
But I dream things that never were; and I say Why not?

– George Bernard Shaw

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

(1…100).each do |i|
puts i.options
end

Oops, this should be “if loki_roused”

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    numbers which are multiples of both three and five
    print "FizzBuzz".

Pretend you’ve just walked into a job interview and been hit with this question.
Solve it as you would under such circumstances for this week’s Ruby Q…

My “honest” solution (what I really would have submitted to the
interviewer):

arr = (1…100).map do |i|
i = ((“FizzBuzz” if i%15==0) or (“Fizz” if i%3==0) or (“Buzz” if
i%5==0) or i)
end
puts arr

I couldn’t resist throwing in the Loki solution. Why Loki? Because
it’s a mischievous rascal, and it makes excessive use of the name
“it”, albeit misguidedly, which it thinks is fitting giving the recent
hoopla about “it”:

require ‘date’

class Fixnum
@@shapes = {
3 => “Fizz”,
5 => “Buzz”
}

def self.invoke it
@@shapes = it if it.kind_of? Hash
end

def self.reveal
puts @@shapes.value.join( ’ ’ )
end

def << it
return self if it.empty?
it
end

def options( sep="", it=[] )
@@shapes.keys.sort.each do |k|
it << @@shapes[k] if self%k == 0
end
self << it.join( sep )
end
end

loki_roused = ARGV[0]

if loki_roused
the_simpsons = {
3=>“Homer”,
5=>“Marge”,
7=>“Bart”,
11=>“Lisa”,
13=>“Maggie”
}
Fixnum.invoke the_simpsons
end

(1…100).each do |i|
puts i.options
end

if loki_disturbed
jd = Date.today.jd
puts “\n---------------------------------”
puts "\n Out of the simpson family – on this Julian day of #{jd},
Loki can shape shift into:\n #{(it = jd.options "
“).kind_of?(String) ? it : ‘none of them’}”
puts “---------------------------------\n”
end

Some thoughts:

This is, obviously, an incredibly easy programming puzzle – as far as
writing down the pseudo code in english. It took me, however, no lie,
a good half hour just to decide on a course of action. In my head, I
struggled with the virtues of simplicity and the “coolness” of
conciseness, all while trying to avoid mediocrity. Actual programming
was a breeze. From there, though, no lie, at least fifteen minutes to
debug. I’m not kidding.

Now, I’m the first to admit that I’m new to Ruby, and programming is
not my strong suit, but I would never think that I’m one of those
people that couldn’t program themselves out of a paper bag.

Thinking about this quiz just reinforced what I already knew about
myself. I’m not a guy who just jumps in and gets his hands dirty. In
fact, I’m the exact opposite. I over-think the problem and often get
nowhere. If this were a test of performance under pressure, I’m
certain I would have failed to impress during the interview.

In any case, to the other newbies out there: don’t be intimidated by
such frivolous pursuits of the lofty few such as “golf”. In fact, be
so bold as to join in if you dare. But, as in all things, be
steadfast in your Ruby endeavors. Enlightenment will come :slight_smile:

Todd

|Ruby Q.|

Everything is supposed to be a “FizzBuzz” unless having been declared
something other.


def fizzbuzz(value)
result = “FizzBuzz”
result.gsub!(“Buzz”, “”) if value % 5 != 0
result.gsub!(“Fizz”, “”) if value % 3 != 0
result = value if result.empty?
result
end

(1…100).each {|x| puts fizzbuzz(x)}

Hi All,

Attached is my first submission to the Ruby Q… I am relatively
new to ruby (7 months) and I have never before been able to solve a
Ruby Q. without several days of head scratching effort. It is not
the most ruby centric code, but it only took me 10 minutes to complete.

Don L.
Brooklyn, New York

(1…100).each do |n|

case
when n.modulo(5) == 0 && n.modulo(3) == 0
print ‘FizzBuzz’, ’ ’
when n.modulo(5) == 0
print ‘Buzz’, ’ ’
when n.modulo(3) == 0
print ‘Fizz’, ’ ’
else
print n, ’ ’
end

end

So, I’ve got to lay claim to the 56 byte solution:

sh-3.2$ wc fizzbuzz.rb
0 2 56 fizzbuzz.rb
sh-3.2$ ruby fizzbuzz.rb | tail -11
“FizzBuzz”
91
92
“Fizz”
94
“Buzz”
“Fizz”
97
98
“Fizz”
“Buzz”

…but I’m not sure if I should post it. Would people consider that a
spoiler?

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

“Buzz”
“Fizz”
97
98
“Fizz”
“Buzz”
Have been there, but this is not a valid solution, sorry to say so :frowning:
you gotta get rid of the quotes in the output.

…but I’m not sure if I should post it. Would people consider that a
spoiler?
The non spoiler period is over already :wink:

Cheers
Robert

My first Ruby Q. submission.

Straightforward solution

(1…100).each do |n|
print “Fizz” if 0 == n % 3
print “Buzz” if 0 == n % 5
print n if 0 != n % 3 and 0 != n % 5
print “\n”
end

Peter S. “extra fun” solution

class Fixnum
alias old_to_s to_s

def to_s
value = “”
value += “Fizz” if 0 == self % 3
value += “Buzz” if 0 == self % 5
value += self.old_to_s if “” == value
value
end
end

(0…100).each { |x| p x }

make things right again

class Fixnum
alias to_fizz_buzz to_s
alias to_s old_to_s
end

golf solution (67 chars)

1.upto(?d){|n|puts 0<n%3&&0<n%5?n:(1>n%3?“Fizz”:’’)+(1>n%5?“Buzz”:’’)}

Cheers,

Michael G.
grzm seespotcode net

Here was my first attempt that was around 5 minutes of effort. After
thinking about it more I ended up writing a second version (also below)
that was less creative.

–Bill

First attempt

(1…100).each { |n|
if n % 3 == 0 then
print “Fizz”
end
if n % 5 == 0 then
print “Buzz”
end
if (n%3 != 0) and (n%5 != 0) then
print n
end
print “\n”
}

Second Attempt

(1…100).each { |n|
case
when (n%3 == 0) && (n%5 == 0) then
puts “FizzBuzz”
when (n%3 == 0) then
puts “Fizz”
when (n%5 == 0) then
puts “Buzz”
else
puts n
end
}

It’s been months, and everbody else is, so why not…

(1…100).each do |x|
m3 = x.modulo(3) == 0
m5 = x.modulo(5) == 0

puts case
when (m3 and m5) then ‘FizzBuzz’
when m3 then ‘Fizz’
when m5 then ‘Buzz’
else x
end
end

I went for clarity and simplicity.

Robert D. wrote:

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

“Buzz”
“Fizz”
97
98
“Fizz”
“Buzz”
Have been there, but this is not a valid solution, sorry to say so :frowning:
you gotta get rid of the quotes in the output.
Crud, I just tested at anarchy golf - FizzBuzz and you’re
right…

For those interested, the code was:

1.upto(?d){|i,x|i%5>0||x=:Buzz;p i%3>0?x||i:“Fizz#{x}”}

your code prints out quotes, which should be removed.

2007/6/3, Joshua B. [email protected]:

Sorry I didn’t see previous posts… :wink:

2007/6/3, Joshua B. [email protected]:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

puts ‘1’
puts ‘2’
puts ‘Fizz’
puts ‘4’
puts ‘Buzz’
puts ‘Fizz’
puts ‘7’
puts ‘8’
puts ‘Fizz’
puts ‘Buzz’
puts ‘11’
puts ‘Fizz’
puts ‘13’
puts ‘14’
puts ‘FizzBuzz’
puts ‘16’
puts ‘17’
puts ‘Fizz’
puts ‘19’
puts ‘Buzz’
puts ‘Fizz’
puts ‘22’
puts ‘23’
puts ‘Fizz’
puts ‘Buzz’
puts ‘26’
puts ‘Fizz’
puts ‘28’
puts ‘29’
puts ‘FizzBuzz’
puts ‘31’
puts ‘32’
puts ‘Fizz’
puts ‘34’
puts ‘Buzz’
puts ‘Fizz’
puts ‘37’
puts ‘38’
puts ‘Fizz’
puts ‘Buzz’
puts ‘41’
puts ‘Fizz’
puts ‘43’
puts ‘44’
puts ‘FizzBuzz’
puts ‘46’
puts ‘47’
puts ‘Fizz’
puts ‘49’
puts ‘Buzz’
puts ‘Fizz’
puts ‘52’
puts ‘53’
puts ‘Fizz’
puts ‘Buzz’
puts ‘56’
puts ‘Fizz’
puts ‘58’
puts ‘59’
puts 'FizzBuzz
puts ‘61’
puts ‘62’
puts ‘Fizz’
puts ‘64’
puts ‘Buzz’
puts ‘Fizz’
puts ‘67’
puts ‘68’
puts ‘Fizz’
puts ‘Buzz’
puts ‘71’
puts ‘Fizz’
puts ‘73’
puts ‘74’
puts ‘FizzBuzz’
puts ‘76’
puts ‘77’
puts ‘Fizz’
puts ‘79’
puts ‘Buzz’
puts ‘Fizz’
puts ‘82’
puts ‘83’
puts ‘Fizz’
puts ‘Buzz’
puts ‘86’
puts ‘Fizz’
puts ‘88’
puts ‘89’
puts ‘FizzBuzz’
puts ‘91’
puts ‘92’
puts ‘Fizz’
puts ‘94’
puts ‘Buzz’
puts ‘Fizz’
puts ‘97’
puts ‘98’
puts ‘Fizz’
puts ‘Buzz’

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFGYtTj2vy7v+d+psQRAqm7AJ4lq8Gmv4Q0teoEeG//H3LQppoA4QCbBNnp
8wWFeZolXrFhEbnH+WhqzjA=
=26q+
-----END PGP SIGNATURE-----