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
23 changes: 20 additions & 3 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
# Improved Fibonacci

# Time Complexity - ?
# Space Complexity - ? (should be O(n))
# Time Complexity - O(n) n being the number given
# Space Complexity - O(n) (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.


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

def fibonacci_helper(solutions, current, n)
if n < 0
raise ArgumentError
end

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

if current == n
return solutions[n-1] + solutions[n-2]
end

solutions << solutions[current-1] + solutions[current-2]
return fibonacci_helper(solutions, current + 1, n)
end
27 changes: 23 additions & 4 deletions lib/super_digit.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
require "pry"
# Superdigit

# Time Complexity - ?
# Space Complexity - ?
def super_digit(n)

# Time Complexity - super_digit_helper is O(log(n)) and this gets called O(n) in super_digit where n is the number of integers in num. So it's O(n).
# Space Complexity - O(1)


def super_digit_helper(n)
if n == 0
return 0
end

return n % 10 + super_digit_helper(n/10)
end

def super_digit(num)

Choose a reason for hiding this comment

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

👍

# binding.pry
super_num = super_digit_helper(num)
if super_num > 9
super_digit(super_num)

Choose a reason for hiding this comment

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

Suggested change
super_digit(super_num)
return super_digit(super_num)

else
return super_num
end

end



# Time Complexity - ?
Expand Down
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