From 484a84153564421405aa91a3383a767caaa6d113 Mon Sep 17 00:00:00 2001 From: Dominique Date: Sun, 17 Nov 2019 18:46:57 -0800 Subject: [PATCH 1/2] first change --- lib/fibonacci.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..0e282e5 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,7 +1,7 @@ # Improved Fibonacci # Time Complexity - ? -# Space Complexity - ? (should be O(n)) +# Space Complexity O(n) (should be O(n)) # Hint, you may want a recursive helper method def fibonacci(n) From a60d4da0bc470fa7f2d24743bb0c6e85912b4535 Mon Sep 17 00:00:00 2001 From: Dominique Date: Mon, 18 Nov 2019 19:02:19 -0800 Subject: [PATCH 2/2] finished --- lib/fibonacci.rb | 12 +++++++++--- lib/super_digit.rb | 28 ++++++++++++++++++++++++---- test/super_digit_test.rb | 4 ++-- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 0e282e5..483a00d 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,8 +1,14 @@ # Improved Fibonacci -# Time Complexity - ? -# Space Complexity O(n) (should be O(n)) +# Time Complexity - O(n!) +# Space Complexity O(n) # Hint, you may want a recursive helper method def fibonacci(n) - + if n < 0 + raise ArgumentError, "Invalid number" + elsif n == 0 || n == 1 + return n + else + return fibonacci(n - 2) + fibonacci(n - 1) + end end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..3aa244c 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,10 +1,30 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? -def super_digit(n) - +# Time Complexity - O(n) +# Space Complexity - O(n) +def super_digit(x) + if x < 10 + return x + elsif + sum = integer_calcuation(x) + super_digit(sum) + end end + +def integer_calcuation(x) + + digit_array = x.to_s.split('') + + sum = 0 + + digit_array.each do |element| + sum += element.to_i + end + + return sum +end + + # Time Complexity - ? diff --git a/test/super_digit_test.rb b/test/super_digit_test.rb index 60da3a1..20973f1 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) @@ -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)