Skip to content

Instantly share code, notes, and snippets.

@vhbsouza
Created May 17, 2014 20:57
Show Gist options
  • Save vhbsouza/7d02bd9951d2c893afb5 to your computer and use it in GitHub Desktop.
Save vhbsouza/7d02bd9951d2c893afb5 to your computer and use it in GitHub Desktop.
Fizz Buzz
=begin
fizz Buzz
Given a number the function returns “Fizz” if it is a multiple of 3, “Buzz” if it is a multiple of 5 and “FizzBuzz” if it is a multiple of 15. If the number is not a multiple of 3 or 5 then the number is returned as a string.
Example:
1
2
3
4
fizzbuzz(3) => "Fizz"
fizzbuzz(10) => "Buzz"
fizzbuzz(45) => "FizzBuzz"
fizzbuzz(31) => "31"
=end
# Golf style
1.upto(?d){|i|puts ["#{x=[:Fizz][i%3]}Buzz"][i%5]||x||i}# 56
1.upto(?d){|i|x=[:Fizz][i%3];puts i%5<1?"#{x}Buzz":x||i}# 56
1.upto(?d){|i|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":x||i}# 56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment