6bff7d5313e73334ef0edc691a7564f7

Wrote a Fizz Buzz http://en.wikipedia.org/wiki/Bizz_buzz#Other_uses solution in Ruby. It works but I'm sure it could be better/more concise.

101.times do |n|

  next if n == 0

  case true
    when (n%5) == 0 && (n%3) == 0
      puts "fizzbuzz"
    when ((n%3) == 0)
      puts "fizz"
    when (n%5) == 0
      puts "buzz"
    else
      puts n
  end

end

Refactorings

No refactoring yet !

37cebc7c6f4cee5d5bc34277e691e7ba

Matt

January 15, 2010, January 15, 2010 15:48, permalink

2 ratings. Login to rate!

Here's my crack at it...

101.times do |n|
  next if n == 0
  
  result = ''
  result += (n%3 == 0) ? 'fizz' : ''
  result += (n%5 == 0) ? 'buzz' : ''
  result += (result.length < 1) ? "#{n}" : ''
  
  puts result
end
A74c34f1043b833b1fcc86ce9f3521ee

Pavel Gorbokon

January 15, 2010, January 15, 2010 17:48, permalink

2 ratings. Login to rate!
1.upto(100) do |n|
  s = ""
  s << "fizz" if n%3 == 0
  s << "buzz" if n%5 == 0
  s = "#{n}"  if s.empty?
  puts s
end
A74c34f1043b833b1fcc86ce9f3521ee

Pavel Gorbokon

January 15, 2010, January 15, 2010 18:16, permalink

2 ratings. Login to rate!
def fizzbuzz(n)
  s = [[5, "fizz"],[3, "buzz"]].select{|(div, msg)| n%div == 0 }.map{|(div, msg)| msg }.join
  s.empty? ? n.to_s : s
end

puts (1..100).map {|n| fizzbuzz(n) }
E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

January 16, 2010, January 16, 2010 12:35, permalink

No rating. Login to rate!
def fizzbuzz max
  (1..max).map do |n|
    s = ['fizz'][n%3].to_s + ['buzz'][n%5].to_s
    s.empty? ? n.to_s : s
  end
end

puts fizzbuzz(100)
37cebc7c6f4cee5d5bc34277e691e7ba

Matt

January 16, 2010, January 16, 2010 15:58, permalink

No rating. Login to rate!

2nd stab

1.upto(100) do |n|
  print (n%3 == 0) ? 'fizz' : ''
  puts (n%5 == 0) ? 'buzz' : (n%3 == 0) ? '' : n
end
37cebc7c6f4cee5d5bc34277e691e7ba

Matt

January 16, 2010, January 16, 2010 16:37, permalink

No rating. Login to rate!
1.upto(100) do |n|   
  s = "#{n%3 == 0 ? 'fizz' : ''}#{n%5 == 0 ? 'buzz' : ''}"
  puts s.empty? ? n : s
end
E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

January 18, 2010, January 18, 2010 20:40, permalink

1 rating. Login to rate!
puts (1..100).map { |n| ['fizzbuzz'][n%3+n%5] || ['fizz'][n%3] || ['buzz'][n%5] || n }
E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

January 18, 2010, January 18, 2010 21:13, permalink

1 rating. Login to rate!

You could also use 15 as the Least common multiple of 3 and 5:

puts (1..100).map { |n| [:fizzbuzz][n%15] || [:fizz][n%3] || [:buzz][n%5] || n }
D41d8cd98f00b204e9800998ecf8427e

Enonatan

February 15, 2010, February 15, 2010 23:21, permalink

No rating. Login to rate!

So, if we are looking for the shortest solution... I need a way to make empty string nil, but the only idea is regexp (

puts (1..50).map { |n| "#{[:fizz][n%3]}#{[:buzz][n%5]}"[/.+/] || n }
F9a9ba6663645458aa8630157ed5e71e

Ants

June 13, 2010, June 13, 2010 02:49, permalink

No rating. Login to rate!

Are we playing code golf? http://perl.guru.org/scott/misc/golf.html

25ff3dfe48d3847ecf9971aab99589fb

mxcl

February 13, 2011, February 13, 2011 12:57, permalink

No rating. Login to rate!

A slightly more readable version of Enonatan's?

1.upto 100 do |n|
  puts [[:fizz][n%3], [:buzz][n%5]].join[/.+/m] || n
end

Your refactoring





Format Copy from initial code

or Cancel