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 hidden or 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 |
for n in 1..101
print "Fizz" if n%3 == 0
print "Buzz" if n%5 == 0
print n unless n%3 == 0 || n%5 == 0
print "\n"
end
(1..100).each_with_object([]) do |number, array|
next array << "FizzBuzz" if number % 15 == 0
next array << "Fizz" if number % 3 == 0
next array << "Buzz" if number % 5 == 0
array << number
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.