Skip to content

Instantly share code, notes, and snippets.

@jcasimir
Created June 11, 2014 15:23
Show Gist options
  • Save jcasimir/1cac59b67c1016b77ba5 to your computer and use it in GitHub Desktop.
Save jcasimir/1cac59b67c1016b77ba5 to your computer and use it in GitHub Desktop.
# Go through the numbers 0 through 100 (including both 0 and 100)
# If the number is divisible by 3, print "Fizz"
# If the number is divisible by 5, print "Buzz"
# If the number is divisible by both 3 and 5, print "FizzBuzz"
# Otherwise print the number
# Solution 1
#
# (0..100).each do |i|
# if (i % 3 == 0) && (i % 5 == 0)
# puts "FizzBuzz"
# elsif (i % 3 == 0)
# puts "Fizz"
# elsif (i % 5 == 0)
# puts "Buzz"
# else
# puts i
# end
# end
class FizzBuzz
def print(value)
if (value % 3 == 0) && (value % 5 == 0)
"FizzBuzz"
elsif value % 3 == 0
"Fizz"
elsif value % 5 == 0
"Buzz"
else
value.to_s
end
end
def run(maximum)
# result = []
# (0..maximum).each do |i|
# result << print(i)
# end
# return result
(0..maximum).collect{|i| print(i)}
end
end
gem 'minitest'
require 'minitest/autorun'
require './fizz.rb'
require 'minitest/pride'
class FizzBuzzTest < Minitest::Test
def test_it_exists
assert FizzBuzz
end
def test_it_prints_fizz_for_a_multiple_of_three
fb = FizzBuzz.new
assert_equal "Fizz", fb.print(3)
assert_equal "Fizz", fb.print(6)
assert_equal "Fizz", fb.print(27)
end
def test_it_prints_buzz_for_a_multiple_of_five
fb = FizzBuzz.new
assert_equal "Buzz", fb.print(5)
assert_equal "Buzz", fb.print(10)
assert_equal "Buzz", fb.print(80)
end
def test_it_prints_fizzbuzz_for_a_multiple_of_five_and_three
fb = FizzBuzz.new
assert_equal "FizzBuzz", fb.print(15)
assert_equal "FizzBuzz", fb.print(30)
assert_equal "FizzBuzz", fb.print(90)
end
def test_it_prints_a_number_otherwise
fb = FizzBuzz.new
assert_equal "11", fb.print(11)
assert_equal "23", fb.print(23)
assert_equal "88", fb.print(88)
end
def test_it_correctly_outputs_a_small_sequence
fb = FizzBuzz.new
result = fb.run(15)
expected = %w(FizzBuzz 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz)
assert_equal expected, result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment