Skip to content

Instantly share code, notes, and snippets.

@sachin21
Last active March 5, 2019 12:05
Show Gist options
  • Save sachin21/3521ccc3fa5cd42063bb5c780ea8c108 to your computer and use it in GitHub Desktop.
Save sachin21/3521ccc3fa5cd42063bb5c780ea8c108 to your computer and use it in GitHub Desktop.
Fizz Buzz Solver
require 'singleton'
class FizzBuzzSolver
include Singleton
FIZZ = 3
BUZZ = 5
def run(numbers)
answer = solve(numbers)
answer.join("\n")
end
private
def solve(numbers)
numbers.map do |num|
next 'fizzbuzz' if fizz?(num) && buzz?(num)
next 'fizz' if fizz?(num)
next 'buzz' if buzz?(num)
next num
end
end
def fizz?(num)
(num % FIZZ).zero?
end
def buzz?(num)
(num % BUZZ).zero?
end
end
puts FizzBuzzSolver.instance.run((1..20).to_a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment