Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ports - Angela #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,51 @@
# ....
# e.g. 6th fibonacci number is 8

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n is size of input
# Space complexity: O(1)
def fibonacci_non_recursion(n)
# n i step nth fibonacci number in series
# if n = 5
# 0 1 1 2 3
# set first_num = 0
# set second_num = 1
# set count of number to be 3
# until the count is greater than the input n
# new number is equal to first_num + second_num
# reassign first_num and second_num

if n.class != Integer || n < 0
raise ArgumentError, "Your input #{n} is not an non-negative integer!"
end

first_num = 0
second_num = 1
fibonacci_num = 0
count = 2

if n == 0
return fibonacci_num
elsif n == 1
return fibonacci_num + 1
else
until count > n
fibonacci_num = first_num + second_num
first_num = second_num
second_num = fibonacci_num
count += 1
end
end
return fibonacci_num
end

def fibonacci(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice recursive solution here!

raise NotImplementedError
if n.class != Integer || n < 0
raise ArgumentError, "Your input #{n} is not an non-negative integer!"
end

if n < 2
return n
else
return fibonacci(n - 1) + fibonacci(n - 2)
end
end
11 changes: 6 additions & 5 deletions specs/fibonacci_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/fibonacci'
require "minitest/autorun"
require "minitest/reporters"
require "minitest/pride"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go Pride!

require_relative "../lib/fibonacci"

describe "fibonacci" do
describe "basic tests" do
Expand Down Expand Up @@ -29,11 +30,11 @@
describe "edge cases" do
# if the parameter is an object, check for nil
it "nil object is not an integer" do
proc {fibonacci(nil)}.must_raise ArgumentError
proc { fibonacci(nil) }.must_raise ArgumentError
end

it "negative input" do
proc {fibonacci(-9)}.must_raise ArgumentError
proc { fibonacci(-9) }.must_raise ArgumentError
end

it "zero input" do
Expand Down