forked from AdaGold/recursive-dynamic-programming
-
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 - Sabrina #31
Open
Galaxylaughing
wants to merge
7
commits into
Ada-C12:master
Choose a base branch
from
Galaxylaughing:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Leaves - Sabrina #31
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aab8077
implemented fibonacci function
Galaxylaughing 0442f4d
implemented super_digit
Galaxylaughing 0da2838
added refined_super_digit
Galaxylaughing af367b1
added a test case for super_digit that adds up to a three digit numbe…
Galaxylaughing 97d8c17
refactored super_digit, added time/space complexity
Galaxylaughing 041b26f
added time/space complexity for super super_digit
Galaxylaughing 40b9030
removed puts statements
Galaxylaughing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,30 @@ | ||
# Improved Fibonacci | ||
|
||
# Time Complexity - ? | ||
# Space Complexity - ? (should be O(n)) | ||
# Time Complexity - O(n) | ||
# Space Complexity - O(n) | ||
# you only keep the last two solutions, so the solutions array does not grow, | ||
# but you're still effectiving the stack by adding recursive calls | ||
# Hint, you may want a recursive helper method | ||
def fibonacci(n) | ||
|
||
if n < 0 | ||
raise ArgumentError.new('Number cannot be less than zero') | ||
end | ||
|
||
return fib_helper([0, 1], 2, n) | ||
end | ||
|
||
def fib_helper(solutions, current, n) | ||
if n == 0 || n == 1 | ||
return n | ||
end | ||
|
||
last_solution = solutions[-1] | ||
new_solution = solutions[0] + solutions[1] | ||
|
||
if current == n | ||
return new_solution | ||
end | ||
|
||
solutions = [last_solution, new_solution] | ||
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. 👍 |
||
return fib_helper(solutions, current + 1, n) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,31 @@ | ||
# Superdigit | ||
|
||
# Time Complexity - ? | ||
# Space Complexity - ? | ||
# Time Complexity - O(n) where n is the number of digits in the input number | ||
# Space Complexity - O(log n) | ||
def super_digit(n) | ||
|
||
if (n / 10) == 0 | ||
return n | ||
else | ||
sum = 0 | ||
while ((n / 10.0) > 0) | ||
sum += (n % 10) | ||
n = (n / 10) | ||
end | ||
return super_digit(sum) | ||
end | ||
end | ||
|
||
|
||
# Time Complexity - ? | ||
# Space Complexity - ? | ||
# Time Complexity - O(n) | ||
# because the while loop to create the concatenated number | ||
# and the while loop adding the digits of the number are not nested. | ||
# Space Complexity - O(log k * n) | ||
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. This is the brute-force solution here. Can you think of a better way to do this with dynamic programming? |
||
|
||
concatenated_n = '' | ||
while k > 0 | ||
concatenated_n += n.to_s | ||
k -= 1 | ||
end | ||
|
||
concatenated_n = concatenated_n.to_i | ||
return super_digit(concatenated_n) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,69 @@ | ||
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 | ||
|
||
|
||
it "will return 9 for super_digit(999_999_999_999)" do | ||
# Act | ||
answer = super_digit(999_999_999_999) | ||
|
||
# Assert | ||
expect(answer).must_equal 9 | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍