Skip to content

Instantly share code, notes, and snippets.

@markglenfletcher
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markglenfletcher/e449633c88a596772c56 to your computer and use it in GitHub Desktop.
Save markglenfletcher/e449633c88a596772c56 to your computer and use it in GitHub Desktop.
FizzBorkBuzz
Note: The initial code and test files form a
simple example to start you off.
They are *not* related to the chosen exercise,
whose instructions now follow...
- - - - - - - - - - - - - - - - - - - - - - -
Write a program that prints the numbers from 1 to 100.
But for multiples of three print "Fizz" instead of the
number and for the multiples of five print "Buzz". For
numbers which are multiples of both three and five
print "FizzBuzz".
class FizzBuzz
def answer(num)
tests.map do |test_class|
test_class.new(num).run
end.compact.join
end
private
def tests
[
MultipleOfNeitherTest,
MultipleOfThreeTest,
MultipleOfFourTest,
MultipleOfFiveTest
]
end
end
class FizzBuzzTest
def initialize(input)
@input = input
end
def run
test ? prize : nil
end
end
class MultipleOfThreeTest < FizzBuzzTest
def test
@input % 3 == 0
end
def prize
'Fizz'
end
end
class MultipleOfFourTest < FizzBuzzTest
def test
@input % 4 == 0
end
def prize
'Bork'
end
end
class MultipleOfFiveTest < FizzBuzzTest
def test
@input % 5 == 0
end
def prize
'Buzz'
end
end
class MultipleOfNeitherTest < FizzBuzzTest
def test
!MultipleOfThreeTest.new(@input).test &&
!MultipleOfFourTest.new(@input).test &&
!MultipleOfFiveTest.new(@input).test
end
def prize
@input
end
end
require './fizzborkbuzz'
describe "FizzBuzz" do
let(:fizz_buzz) { FizzBuzz.new }
context "inputting an number output" do
it "should be number" do
[1,2,7,11,13,14,17,19].each do |i|
expect(fizz_buzz.answer(i)).to eq i.to_s
end
end
it 'should be fizz' do
[3,6,9,18,21,27,33].each do |i|
expect(fizz_buzz.answer(i)).to eq 'Fizz'
end
end
it 'should be buzz' do
[5,10,25,35,50,55,65,70].each do |i|
expect(fizz_buzz.answer(i)).to eq 'Buzz'
end
end
it 'should be bork' do
[4,8,16,28,32,44,52,56,64,68,76].each do |i|
expect(fizz_buzz.answer(i)).to eq 'Bork'
end
end
it 'should be fizz buzz' do
[15,30,45,75,90,105,135,150,165].each do |i|
expect(fizz_buzz.answer(i)).to eq 'FizzBuzz'
end
end
it 'should be fizz bork' do
[12,24,36,48,72,84,96,108,132,144].each do |i|
expect(fizz_buzz.answer(i)).to eq 'FizzBork'
end
end
it 'should be bork buzz' do
[20,40,80,100,140,160,200,220,260,280,320,340,380,400].each do |i|
expect(fizz_buzz.answer(i)).to eq 'BorkBuzz'
end
end
it 'should be fizz bork buzz' do
[60,120,240,360,480,600,720,840,960,1080,1200,1320,1440,1560,1680,1800].each do |i|
expect(fizz_buzz.answer(i)).to eq 'FizzBorkBuzz'
end
end
end
end
fb = FizzBuzz.new
1000.times do |i|
puts fb.answer i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment