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

Daniela - Leaves #27

Open
wants to merge 5 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
16 changes: 14 additions & 2 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
# 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 don't need to keep the entire list of fibonacci numbers for the entire length of the recursion. Instead you only need to keep the last two fibonacci numbers.


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

def fib_helper( solutions, current, n )
return n if n == 0 || n == 1
raise ArgumentError if n < 0

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

solutions << solutions[current - 1] + solutions[current -2]
return fib_helper(solutions, current + 1, n)
end
41 changes: 34 additions & 7 deletions lib/super_digit.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
# Superdigit

# Time Complexity - ?
# Space Complexity - ?
# Time Complexity : O(n), depends on number of digits
# Space Complexity : O(1) as the array of digits will always be same and I will always have same variables (sum, reminder)
def super_digit(n)


return n if [0,1,2,3,4,5,6,7,8,9].include?(n) == true

Choose a reason for hiding this comment

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

Why not just:

Suggested change
return n if [0,1,2,3,4,5,6,7,8,9].include?(n) == true
return n if n < 10


sum = 0
until n == 0 do
reminder = n % 10
sum += reminder
n = n/10
end

super_digit(sum)
end


# Time Complexity - ?
# Space Complexity - ?
# Time Complexity - O(n), depends on number of digits
# Space Complexity : O(1) as the array of digits will always be same and I will always have same variables (sum, reminder, superdigit)
def refined_super_digit(n, k)

Choose a reason for hiding this comment

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

This works, but you can use the super_digit method previously to help solve the problem.



return n if [0,1,2,3,4,5,6,7,8,9].include?(n) == true && k == 1

sum = 0
until n == 0 do
reminder = n % 10
sum += reminder
n = n/10
end

redifined_number = sum * k
refined_super_digit(redifined_number, 1)
end








2 changes: 1 addition & 1 deletion 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