Skip to content

Instantly share code, notes, and snippets.

@mxcl
Created November 27, 2010 11:53
Show Gist options
  • Save mxcl/717832 to your computer and use it in GitHub Desktop.
Save mxcl/717832 to your computer and use it in GitHub Desktop.
Improve this Fizzbuzz!
#!/usr/bin/ruby
# The Ruby FizzBuzz with the best concision versus elegance compromise.
# Fork if you have better ideas.
1.upto 100 do |n|
puts [[:fizz][n%3], [:buzz][n%5]].join[/.+/m] || n
end
@rlotun
Copy link

rlotun commented Nov 30, 2010

You might like Python (https://gist.github.com/720888):

#!/usr/bin/python
for i in xrange(1, 101):
    print ''.join(['fizz' if i % 3 == 0 else '', 'buzz' if i % 5 == 0 else '']) or i

@mxcl
Copy link
Author

mxcl commented Nov 30, 2010

You can do or off of empty strings? I'm jealous.

@mxcl
Copy link
Author

mxcl commented Nov 30, 2010

I also like that you can do if statements inside of array constructs.

@joshbuddy
Copy link

here is a much sillier possiblity

1.upto(100) { |n| puts Integer(((a=[[:fizz][n%3], [:buzz][n%5]])+[n]).join) rescue puts a.join }

@mxcl
Copy link
Author

mxcl commented Dec 6, 2010

Hah. I like it.

@lg
Copy link

lg commented Dec 6, 2010

jank, but less chars:

100.times { |n| puts "#{'fizz' if n%3==0}#{'buzz' if n%5==0}#{n if n%3!=0 && n%5!=0}" }

@joshbuddy
Copy link

I feel like we need an obfuscated fizzbuzz contest?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment