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

Branches - Farah #42

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
24 changes: 21 additions & 3 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
# Improved Fibonacci

# Time Complexity - ?
# Space Complexity - ? (should be O(n))
# Time Complexity - O(n) where n integer passed through fibonacci(n) method
# Space Complexity - ? (should be O(n)); It is O(n) where n is the number of times the method is called and placed on the stack
# 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.

👍


return fibonacci_helper(0, 1, 2, n)
end

def fibonacci_helper(second_solution, first_solution, current, n)
if n < 0
raise ArgumentError
end

return n if n == 0 || n == 1

if current == n
return first_solution + second_solution
end

new_solution = first_solution + second_solution
second_solution = first_solution
first_solution = new_solution
Comment on lines +21 to +23

Choose a reason for hiding this comment

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

Nice!


return fibonacci_helper(second_solution, first_solution, current + 1, n)
end
32 changes: 26 additions & 6 deletions lib/super_digit.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
# Superdigit

# Time Complexity - ?
# Space Complexity - ?
# Time Complexity - O(n) where n is equal to the number of digits in the n-number passed in the method
# Space Complexity - O(n) where n is equal to the number of times the super_digit method is placed on the call stack
def super_digit(n)

Choose a reason for hiding this comment

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

👍


# first modulo (n % 10) and add to a counter(number)
# until n%10 is equal 0

total = 0
until n % 10 == 0
counter = n % 10
total += counter
n = n/10
end

if total > 9
return super_digit(total)
else
return total
end
end


# Time Complexity - ?
# Space Complexity - ?
# Time Complexity - O(n) where n is equal to the number of digits in the n-number passed in the super_digit method or the number of digits in the result-number passed in the super_digit method.
# Space Complexity - O(n) where n is equal to the number of times the suger_digit method is placed on the call stack
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.

👍 Nice work!


if k == 1
return super_digit(n)
else
result = super_digit(n)
result *= k
return super_digit(result)
end
end

28 changes: 14 additions & 14 deletions test/fibonacci_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@
# Assert
expect(answer).must_equal 3
end
it "will return 5 for fib(5)" do
# Act
answer = fibonacci(4)

# Assert
expect(answer).must_equal 3
end
it "will return 55 for fib(10)" do
# Act
answer = fibonacci(10)

# Assert
expect(answer).must_equal 55
end
# it "will return 5 for fib(5)" do
# # Act
# answer = fibonacci(4)

# # Assert
# expect(answer).must_equal 3
# end
# it "will return 55 for fib(10)" do
# # Act
# answer = fibonacci(10)

# # Assert
# expect(answer).must_equal 55
# end
end
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 All @@ -15,7 +15,7 @@

# Assert
expect(answer).must_equal 5
end
end

it "will return 6 for super_digit(123)" do
# Act
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require "minitest/skip_dsl"
require 'pry'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

Expand Down