Skip to content

Instantly share code, notes, and snippets.

@blake41
Last active December 31, 2015 05:59
Show Gist options
  • Save blake41/7944664 to your computer and use it in GitHub Desktop.
Save blake41/7944664 to your computer and use it in GitHub Desktop.
require_relative "fibonacci_solution"
describe "fibo finder" do
it "it should return the nth number of the fibonacci sequence" do
fibo_finder(0).should eq(0)
end
it "it should return the nth number of the fibonacci sequence" do
fibo_finder(1).should eq(1)
end
it "it should return the nth number of the fibonacci sequence" do
fibo_finder(4).should eq(3)
end
it "it should return the nth number of the fibonacci sequence" do
fibo_finder(7).should eq(13)
end
it "it should return the nth number of the fibonacci sequence" do
fibo_finder(10).should eq(55)
end
end
# prereqs: arrays, methods, conditional logic
# create a method that returns the nth number of the fibonacci sequence
# fib sequence = 0,1,1,2,3,5,8,13 etc, assume the 1st element is 0 (the first number), the 2nd element is 1, the third element is 1
def fibo_finder(n)
# code goes here
end
def fibo_finder(n)
if n == 0
return 0
elsif n == 1
return 1
else
sequence = [0,1]
(n - 1).times do
sequence << sequence[-1] + sequence[-2]
end
end
return sequence.last
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment