diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..c98fa79 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -4,5 +4,6 @@ # Space Complexity - ? (should be O(n)) # Hint, you may want a recursive helper method def fibonacci(n) + end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..9b919dc 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,15 +1,33 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - o(n) +# Space Complexity - o(1) def super_digit(n) - + new_num = 0 + numbers = n.to_s.split("") + if numbers.length <= 1 + return n + end + numbers.each do |i| + new_num += i.to_i + end + return super_digit(new_num) end - + # Time Complexity - ? # Space Complexity - ? def refined_super_digit(n, k) - + new_num = 0 + times = k + numbers = n.to_s.split("") + if numbers.length <= 1 + return n + end + numbers.each do |i| + new_num += i.to_i + end + new_n = times * new_num + return super_digit(new_n) end \ No newline at end of file diff --git a/test/fibonacci_test.rb b/test/fibonacci_test.rb index 639f3b1..e6c2a7b 100644 --- a/test/fibonacci_test.rb +++ b/test/fibonacci_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -describe "Fibonacci" do +xdescribe "Fibonacci" do it "will raise an error for n < 0" do expect { # Act diff --git a/test/super_digit_test.rb b/test/super_digit_test.rb index 60da3a1..8eb6475 100644 --- a/test/super_digit_test.rb +++ b/test/super_digit_test.rb @@ -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)