-
Notifications
You must be signed in to change notification settings - Fork 48
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
Leaves - Katie K #38
base: master
Are you sure you want to change the base?
Leaves - Katie K #38
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,39 @@ | |
# Time Complexity - ? | ||
# Space Complexity - ? (should be O(n)) | ||
# Hint, you may want a recursive helper method | ||
|
||
|
||
def fibonacci(n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
if n < 0 | ||
raise ArgumentError | ||
end | ||
if n == 0 | ||
return 0 | ||
end | ||
if n==1 || n==2 | ||
return 1 | ||
end | ||
fibonacci_helper(0, n, {1=> 1, 2=> 1}) | ||
end | ||
|
||
def fibonacci_helper(current, n, look_up) | ||
if n == current | ||
return look_up[current-1] + look_up[current-2] | ||
end | ||
if current >= 3 | ||
look_up[current] = look_up[current-1] + look_up[current-2] | ||
look_up.delete(current - 2) | ||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clever |
||
end | ||
current += 1 | ||
fibonacci_helper(current, n, look_up) | ||
end | ||
|
||
|
||
|
||
|
||
# 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 | ||
#1.618 the golden ratio | ||
|
||
# Fib(0) = 0 Fib(1) = 1 | ||
|
||
# Fib(n) = Fib(n-2) + Fib(n-1), for all n >= 2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,27 @@ | |
# Time Complexity - ? | ||
# Space Complexity - ? | ||
def super_digit(n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 but no time/space complexity? |
||
|
||
end | ||
if n.class != Array | ||
n = n.to_s.split('') | ||
end | ||
|
||
sum = 0 | ||
n.each do |value| | ||
sum += value.to_i | ||
end | ||
|
||
if sum < 10 | ||
return sum | ||
else | ||
return super_digit(sum) | ||
end | ||
end | ||
|
||
|
||
# Time Complexity - ? | ||
# Space Complexity - ? | ||
def refined_super_digit(n, k) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 But you can go further with dynamic programming. |
||
|
||
number = n * k | ||
final = super_digit(number) | ||
return final | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
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) | ||
|
||
# Assert | ||
expect(answer).must_equal 2 | ||
end | ||
|
||
it "will return 5 for super_digit(5)" do | ||
# Act | ||
answer = super_digit(5) | ||
|
||
# Assert | ||
expect(answer).must_equal 5 | ||
end | ||
|
||
end | ||
it "will return 6 for super_digit(123)" do | ||
# Act | ||
answer = super_digit(123) | ||
|
||
# Assert | ||
expect(answer).must_equal 6 | ||
end | ||
|
||
it "will return 6 for super_digit(12327)" do | ||
# Act | ||
answer = super_digit(12327) | ||
|
||
# Assert | ||
expect(answer).must_equal 6 | ||
end | ||
|
||
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 | ||
end | ||
|
||
it "will return 8 for n=9875 and k = 4" do | ||
# Act | ||
answer = refined_super_digit(9875, 4) | ||
|
||
# Assert | ||
expect(answer).must_equal 8 | ||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no answer here?