Last active
March 5, 2019 12:05
-
-
Save sachin21/3521ccc3fa5cd42063bb5c780ea8c108 to your computer and use it in GitHub Desktop.
Fizz Buzz Solver
This file contains 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
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