Created
August 10, 2012 01:23
-
-
Save dictav/3310094 to your computer and use it in GitHub Desktop.
Fizz Buzz
This file contains hidden or 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
#! /usr/bin/env ruby | |
# coding:utf-8 | |
def fizz_buzz(i) | |
str = [] | |
str << "fizz" if (i/3 - (i-1)/3) == 1 | |
str << "buzz" if (i/5 - (i-1)/5) == 1 | |
str << i.to_s if str.empty? | |
str.join(" ") | |
end |
This file contains hidden or 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
# coding:utf-8 | |
require "rspec" | |
require "./fizz_buzz.rb" | |
describe "FizzBuzz" do | |
before do | |
@fizz_buzz_arr = [ 1, 2, "fizz", 4, "buzz", "fizz", 7, 8, "fizz", "buzz", 11, "fizz", 13, 14, "fizz buzz"] | |
end | |
it "3で割り切れる時はfizzを出力する" do | |
1000.times do | |
a = 1 + rand(1000000) | |
a -= a % 3 | |
fizz_buzz(a).should match /fizz/ | |
end | |
end | |
it "5で割り切れる時はbuzzを出力する" do | |
1000.times do | |
a = 1 + rand(1000000) | |
a -= a % 5 | |
fizz_buzz(a).should match /buzz/ | |
end | |
end | |
it "Fizz Buzz 列を作れる" do | |
10.times do | |
a = 1 + rand(1000000) | |
a -= a%15 - 1 | |
puts a | |
answer = @fizz_buzz_arr.map{|n| (n.kind_of? Fixnum)? (n+a-1).to_s : n} | |
range = (a...a+15) | |
arr = range.map{|n| fizz_buzz(n)} | |
print 'correct output ' | |
p answer | |
print 'fizz_buzz output' | |
p arr | |
arr.should == answer | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment