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

Leaves - Julia K #40

Open
wants to merge 3 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
25 changes: 21 additions & 4 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))
# Hint, you may want a recursive helper method
# Time Complexity - O(n) because it performs n operations
# Space Complexity - O(n) because it makes n calls on the stack

def fibonacci(n)

Choose a reason for hiding this comment

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

👍


return fib_helper(n, n_minus_2 = 0, n_minus_1 = 1, 2)
end

def fib_helper(n, n_minus_2, n_minus_1, current) # starting values (4, 0, 1, 2)

# base cases

raise ArgumentError if n < 0
return n if n == 0 || n == 1
return n_minus_1 + n_minus_2 if current == n

# recursive step -- increment current and reassign n_minus_2 and n_minus_1
current += 1
fibber = n_minus_1
n_minus_1 = n_minus_1 + n_minus_2
n_minus_2 = fibber
return fib_helper(n, n_minus_2, n_minus_1, current)
Comment on lines +19 to +23

Choose a reason for hiding this comment

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

nice!

end

20 changes: 13 additions & 7 deletions lib/super_digit.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# Superdigit

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

# Time Complexity - O log n because with each operation we are cutting the total of n by at worst 10
# Space Complexity - O log n
def super_digit(n)

Choose a reason for hiding this comment

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

👍

# base case
if n <= 9
return n
else
# recursive step -- convert number to array of integers and add them all together
n_array = n.digits
n = n_array.sum
super_digit(n)
end
end


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

end

4 changes: 2 additions & 2 deletions test/fibonacci_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
end
it "will return 5 for fib(5)" do
# Act
answer = fibonacci(4)
answer = fibonacci(5)

# Assert
expect(answer).must_equal 3
expect(answer).must_equal 5
end
it "will return 55 for fib(10)" do
# Act
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