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 - Sabrina #31

Open
wants to merge 7 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
28 changes: 25 additions & 3 deletions lib/fibonacci.rb
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)

Choose a reason for hiding this comment

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

👍


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]

Choose a reason for hiding this comment

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

👍

return fib_helper(solutions, current + 1, n)
end
32 changes: 24 additions & 8 deletions lib/super_digit.rb
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)

Choose a reason for hiding this comment

The 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

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
46 changes: 27 additions & 19 deletions test/super_digit_test.rb
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