Skip to content
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
44 changes: 43 additions & 1 deletion lib/fibonacci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,47 @@
# Space Complexity - ? (should be O(n))
# Hint, you may want a recursive helper method
Comment on lines 4 to 5

Choose a reason for hiding this comment

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

No answers here?

def fibonacci(n)

Choose a reason for hiding this comment

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

👍


return fibonacci_helper(n, solution = [0,1], current = 2)
end

def fibonacci_helper(n, s, current)

if n < 0
raise ArgumentError
end

if n == 0 || n == 1
return n
end

if current == n
return s[0] + s[1]
end

temp = s[1]
s[1] = s[0] + s[1]
s[0] = temp
Comment on lines +24 to +26

Choose a reason for hiding this comment

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

Yes!

return fibonacci_helper(n, s, current + 1)
end

#Iterative

# def fib(n)
# if n == 0 || n == 1
# return n
# end
# solution = [0, 1]
# current = 2
# while current < n
# p current
# p solution, "before"
# temp = solution[1]
# solution[1] = solution[0] + solution[1]
# solution[0] = temp
# # solut

# current += 1
# p solution, "after"
# end
# return solution[0] + solution[1]
# end
13 changes: 7 additions & 6 deletions lib/super_digit.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Superdigit

# Time Complexity - ?
# Space Complexity - ?
# Time Complexity - O(n)
# Space Complexity - O(1)
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.

The time/space complexity will be the same O(log10n)

def super_digit(n)

Choose a reason for hiding this comment

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

👍


return n if n.to_s.length == 1
return super_digit(n.digits.sum)
end


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

# end

4 changes: 2 additions & 2 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 Down Expand Up @@ -33,7 +33,7 @@
expect(answer).must_equal 6
end

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