Created
August 20, 2012 19:18
-
-
Save shostakovich/3406888 to your computer and use it in GitHub Desktop.
Fibonaci Sequence Generator 1
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
class FibonaciSequenceGenerator | |
class SequenceToLongError < StandardError; end | |
def generate(length) | |
raise SequenceToLongError if length > 50 | |
@sequence = [] | |
1.upto(length) {|number| @sequence << fibonaci_number(number)} | |
@sequence | |
end | |
def fibonaci_number(number) | |
case number | |
when 1 then 0 | |
when 2 then 1 | |
else @sequence[number - 2] + @sequence[number - 3] | |
end | |
end | |
end |
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
describe FibonaciSequenceGenerator do | |
let(:generator){ FibonaciSequenceGenerator.new } | |
it "contains 0 as the first number in a sequence" do | |
generator.generate(8).first.should be == 0 | |
end | |
it "contains 1 as the second number in a sequence" do | |
generator.generate(8)[1].should be == 1 | |
end | |
it "contains 1 as the third number in a sequence" do | |
generator.generate(8)[2].should be == 1 | |
end | |
it "contains 2 as the fourth number in a sequence" do | |
generator.generate(8)[3].should be == 2 | |
end | |
it "limits the length to 50" do | |
lambda{ generator.generate(51) }.should raise_error FibonaciSequenceGenerator::SequenceToLongError | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel like I should have refactored my tests :/ Well next time..