Created
April 24, 2012 20:36
-
-
Save Kerrick/2483510 to your computer and use it in GitHub Desktop.
Different solutions for Fizz Buzz in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizz_buzz_1(max) | |
arr = [] | |
(1..max).each do |n| | |
if ((n % 3 == 0) && (n % 5 == 0)) | |
arr << "FizzBuzz" | |
elsif (n % 3 == 0) | |
arr << "Fizz" | |
elsif (n % 5 == 0) | |
arr << "Buzz" | |
else | |
arr << n | |
end | |
end | |
return arr | |
end | |
def fizz_buzz_2(max) | |
arr = [] | |
(1..max).each do |n| | |
if (n % 3 == 0) | |
if (n % 5 == 0) | |
arr << "FizzBuzz" | |
else | |
arr << "Fizz" | |
end | |
elsif (n % 5 == 0) | |
arr << "Buzz" | |
else | |
arr << n | |
end | |
end | |
return arr | |
end | |
def fizz_buzz_3(max) | |
arr = [] | |
(1..max).each do |n| | |
text = "" | |
if (n % 3 == 0) | |
text << "Fizz" | |
end | |
if (n % 5 == 0) | |
text << "Buzz" | |
end | |
if !((n % 3 == 0) || (n % 5 == 0)) | |
text = n | |
end | |
arr << text | |
end | |
return arr | |
end |
buzzer = lambda do |i|
map = {ff: i, tf: 'Fizz', ft: 'Buzz', tt: 'FizzBuzz'}
map[((i%3==0).to_s[0] + (i%5==0).to_s[0]).to_sym]
end
puts (1..100).map(&buzzer)
(0..100).map{|i|i%15==0?'FizzBuzz':i%3==0?'Fizz':i%5==0?'Buzz':i}
#66 chars :)
This one requires presence
from Rails (i.e., it works in rails console
):
puts 1.upto(100).map { |n| "#{'Fizz' if n%3==0}#{'Buzz' if n%5==0}".presence || n }
(1..100).map{|n|({3=>['Fizz'],5=>['Buzz']}.map{|x,y|y[n%x]}.join.gsub(/^$/,n.to_s))}
@willwright82 's solution modified for less == 0
:
(1..100).map do |m|
case 0
when m % 15 then 'FizzBuzz'
when m % 3 then 'Fizz'
when m % 5 then 'Buzz'
else m
end
end
def fizzbuzz(int)
if int % 3 == 0 && int % 5 == 0
"FizzBuzz"
elsif int % 3 == 0
"Fizz"
elsif int % 5 == 0
"Buzz"
else int % 4 == 0
nil
end
end
70 characters
(1..100).map{|x|puts x%15==0?'FizzBuzz':x%5==0?'Buzz':x%3==0?'Fizz':x}
When I thought we already had way too many solutions... !
module BeingMultiple
refine Integer do
def eitherFizzOrBuzz!
return "FizzBuzz" if multiple?(3) && multiple?(5)
return "Fizz" if multiple?(3)
return "Buzz" if multiple?(5)
self
end
private
def multiple?(target)
self % target == 0
end
end
end
using BeingMultiple
puts (1..n).map(&:eitherFizzOrBuzz!).join("\n")
require "fizzbuzz"
RSpec.describe FizzBuzz do
describe "#compute" do
context "when number is dvisisble by 3" do
it "returns fizz" do
expect(FizzBuzz.compute(6)).to eq("fizz")
end
end
context "when number is dvisisble by 5" do
it "returns buzz" do
expect(FizzBuzz.compute(10)).to eq("buzz")
end
end
context "when number is dvisisble by both 3 and 5" do
it "returns buzz" do
expect(FizzBuzz.compute(15)).to eq("fizzbuzz")
end
end
end
end
class FizzBuzz
def self.compute(number)
return "fizzbuzz" if (number % 15).zero?
if (number % 3).zero? "fizz" else "buzz" end
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#FizzBuzz
(1..100).each do |number|
if (number % 3 == 0) and (number % 5 != 0)
puts "Fizz"
elsif (number % 5 == 0 ) and (number % 3 != 0)
puts "Buzz"
elsif (number % 5 == 0) and (number % 3 == 0)
puts "FizzBuzz"
else
puts number
end
end