-
-
Save Kerrick/2483510 to your computer and use it in GitHub Desktop.
| 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 |
How many times has this problem been solved already? Idiomatic Ruby embraces the DRY principle. Not just in your own code base, but the entire internet.
require 'curb'
require 'nokogiri'
class Dry
# Why copy & paste answers from stack overflow when you can curl & eval them!
# Expects a url#answer-id and a hash of adjustments to the answer code to gsub over
def self.fuck_it_just_copy_something_from_stackoverflow(url, adjustments)
# build the adjustment lambda
edit = "lambda { |method_string| method_string"
adjustments.each { |k,v| edit += ".gsub('#{k}', '#{v}')" }
edit += "}"
# then get some of that overflow goodness
answer = url.split('#').last
@doc ||= Nokogiri::HTML Curl.get(url).body_str
@doc.css("#answer-#{answer} code").each do |code|
# Oh yeah, it's lambda time! Eval the edit string, pass it the overflow code and eval the resulting lambda
return eval eval(edit).call code.content
end
end
end
100.times do |number|
url = 'http://stackoverflow.com/questions/24435547/ruby-fizzbuzz-not-working-as-expected#24435693'
adjustments = {'puts' => 'return', 'def fizzbuzz(n)' => 'lambda do |n|'}
p Dry.fuck_it_just_copy_something_from_stackoverflow(url, adjustments).call(number).last.to_s
endI really like this solution from reddit:
a=b=c=(1..100).each do |num|
print num, ?\r,
("Fizz" unless (a = !a) .. (a = !a)),
("Buzz" unless (b = !b) ... !((c = !c) .. (c = !c))),
?\n
endfizz_buzz =
(1..100).map do |i|
case
when i % 15 == 0 then "FizzBuzz"
when i % 3 == 0 then "Fizz"
when i % 5 == 0 then "Buzz"
else i
end
end
puts fizz_buzz
low=1
high=100
(low..high).each {|x|
puts x%15==0?'FizzBuzz':x%5==0?'Buzz':x%3==0?'Fizz':x
}
_=$$/$$;__=_-_;@_=_+_;$_=@_+_;$__=@_+$_;$-_=$__*$_;@__=''<<$-_*($__+$_)+@_;$___=''<<$-_*$__-$__<<$-_*($__+@_)<<@__<<@__;@___=''<<$-_*$__-$_*$_<<$-_*($__+$_)-$_<<@__<<@__;(___=->{$.+=_;$><<($.%$-_==__ ?$___+@___:$.%$_==__ ?$___:$.%$__==__ ?@___:$.)<<(''<<$__*@_);$.<($__*@_)**@_?___[]:_})[]
Thanks to http://threeifbywhiskey.github.io/2014/03/05/non-alphanumeric-ruby-for-fun-and-not-much-else/
(1..100).each do |x|
fizz = "Fizz" if (x % 3 == 0)
buzz = "Buzz" if (x % 5 == 0)
puts (fizz || buzz) ? fizz.to_s + buzz.to_s : x
end
class Fixnum
def to_s
string = ''
string += 'fizz' if self % 3 == 0
string += 'buzz' if self % 5 == 0
return string unless string.empty?
inspect
end
end
puts (1..100).to_a(1..100).each do |i|
if i % 5 == 0 && i % 3 == 0
puts 'FizzBuzz'
elsif i % 5 == 0
puts 'Buzz'
elsif i % 3 == 0
puts 'Fizz'
else
puts i
end
end
#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
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
enddef 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
endfor 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
endFizz Buzz, Object-Oriented Edition
module NumericRefinements
refine Numeric do
def divisible_by?(other) = remainder(other).zero?
end
endclass Replacement
def self.for(n) = @registered.find { it.valid? n }.new(n).to_s
def initialize(n) = @n = n
def self.valid?(n) = false
def to_s = self.class.name
private
# Subclasses defined later take precedence
def self.register(subclass) = (@registered ||= []).prepend subclass
def self.inherited(subclass) = register subclass
endusing NumericRefinements
class TheNumber < Replacement
def self.valid?(n) = true
def to_s = @n.to_s
end
class Fizz < Replacement
def self.valid?(n) = n.divisible_by?(3)
end
class Buzz < Replacement
def self.valid?(n) = n.divisible_by?(5)
end
class FizzBuzz < Replacement
def self.valid?(n) = Fizz.valid?(n) && Buzz.valid?(n)
endclass Game
def initialize(limit: 16) = @limit = limit
def to_a = (1..@limit).collect { Replacement.for it }
end
puts Game.new.to_a
Poor man's pattern matching:
What I like about this approach is that you avoid the duplicate
n % x == 0conditions that are necessary in theif elseversion without having to resort toi % 15which always seemed hacky to me, without going too crazy like many of the purely un-conditional fizzbuzzes out there.