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

Branches - Macaria #33

Open
wants to merge 1 commit 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
25 changes: 23 additions & 2 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
# Improved Fibonacci

# Time Complexity - ?
# Time Complexity - o(n)
# Space Complexity - ? (should be O(n))
# Hint, you may want a recursive helper method
def fibonacci(n)

Choose a reason for hiding this comment

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

This works, but you're keeping the entire list of fibonacci numbers for the whole length of the recursion.  Instead you should only keep the last 2 fibonacci numbers.


if n < 0
raise ArgumentError.new
end
return fib_helper([0, 1], 2, n)
end

def fib_helper(solutions, current, n)
if n == 0 || n == 1
return n
end
if current == n
return solutions[n - 1] + solutions[n - 2]
end

solutions.push(solutions[current - 1] + solutions[current - 2])
return fib_helper(solutions, current + 1, n)

# puts fib(3)
# puts fib(5)
# puts fib(0)
# puts fib(8)

end
12 changes: 6 additions & 6 deletions lib/super_digit.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Superdigit

# Time Complexity - ?
# Space Complexity - ?
# Time Complexity - O(n)
# Space Complexity - O(n)
Comment on lines +3 to +4

Choose a reason for hiding this comment

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

Where n is the number of digits, yes.

def super_digit(n)

Choose a reason for hiding this comment

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

👍


if n < 10
return n
end
return super_digit(n.digits.sum)
end


# Time Complexity - ?
# Space Complexity - ?
def refined_super_digit(n, k)

end

18 changes: 9 additions & 9 deletions test/super_digit_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "test_helper"

xdescribe "super_digit" do
describe "super_digit" do
it "will return 2 for super_digit(9875)" do
# Act
answer = super_digit(9875)
Expand All @@ -15,7 +15,7 @@

# Assert
expect(answer).must_equal 5
end
end

it "will return 6 for super_digit(123)" do
# Act
Expand All @@ -33,13 +33,13 @@
expect(answer).must_equal 6
end

describe "refined superdigit" do
describe "refined superdigit" do
it "will return 1 for n = 1 and k = 1" do
# Act
answer = refined_super_digit(1, 1)

# Assert
expect(answer).must_equal 1
expect(answer).must_equal 1
end

it "will return 8 for n=9875 and k = 4" do
Expand All @@ -51,11 +51,11 @@
end

it "will return 3 for n=148 and k = 3" do
# Act
answer = refined_super_digit(148, 3)
# Assert
expect(answer).must_equal 3
# Act
answer = refined_super_digit(148, 3)

# Assert
expect(answer).must_equal 3
end
end
end