From 49c0619196db81365dc394270ef673df71df673a Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Sun, 17 Nov 2019 16:52:54 -0800 Subject: [PATCH 1/3] Updated test code to for 'return 5 for fib(5) to reflect test name and passing all fibonacci tests. --- lib/fibonacci.rb | 25 +++++++++++++++++++++---- test/fibonacci_test.rb | 4 ++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..0f2dd0a 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -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) - + 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) +end + diff --git a/test/fibonacci_test.rb b/test/fibonacci_test.rb index 639f3b1..64282e5 100644 --- a/test/fibonacci_test.rb +++ b/test/fibonacci_test.rb @@ -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 From bf62b0a5e7ce83a955232acff43513c3f13c62e7 Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Mon, 18 Nov 2019 16:59:07 -0800 Subject: [PATCH 2/3] Passing tests for super digit --- lib/super_digit.rb | 18 ++++++++++++------ test/super_digit_test.rb | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..250ce0e 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,15 +1,21 @@ # Superdigit -# Time Complexity - ? +# Time Complexity - O log n # Space Complexity - ? -def super_digit(n) - +def super_digit(n) + # 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 - \ No newline at end of file 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) From c4345c75f68a0bb2d3d37285ae2d758fbcb82ae2 Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Mon, 18 Nov 2019 17:05:42 -0800 Subject: [PATCH 3/3] Added time and space complexity to super digit. --- lib/super_digit.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 250ce0e..5ed2b80 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,7 +1,7 @@ # Superdigit -# Time Complexity - O log n -# Space Complexity - ? +# 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) # base case if n <= 9