From 445beb62b653b3eac1c8e1f8f3da54a1114b0295 Mon Sep 17 00:00:00 2001 From: Elizabeth Northrop Date: Sun, 17 Nov 2019 18:16:10 -0800 Subject: [PATCH 1/2] Finished fibonacci, passes all tests. --- lib/fibonacci.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..56abbeb 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -4,5 +4,22 @@ # Space Complexity - ? (should be O(n)) # Hint, you may want a recursive helper method def fibonacci(n) - + return fibonacci_helper([0,1], 2, n) end + +def fibonacci_helper(solutions, current, n) + if n < 0 + raise ArgumentError + end + + if n == 0 || n == 1 + return n + end + + if current == n + return solutions[n-1] + solutions[n-2] + end + + solutions << solutions[current-1] + solutions[current-2] + return fibonacci_helper(solutions, current + 1, n) +end \ No newline at end of file From e86b1847cd332f44a8ec7500cf82b8c016e56eba Mon Sep 17 00:00:00 2001 From: Elizabeth Northrop Date: Sun, 17 Nov 2019 19:53:22 -0800 Subject: [PATCH 2/2] Super_digit complete, passes all tests. --- lib/fibonacci.rb | 4 ++-- lib/super_digit.rb | 27 +++++++++++++++++++++++---- test/super_digit_test.rb | 4 ++-- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 56abbeb..5ddde0d 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,7 +1,7 @@ # Improved Fibonacci -# Time Complexity - ? -# Space Complexity - ? (should be O(n)) +# Time Complexity - O(n) n being the number given +# Space Complexity - O(n) (should be O(n)) # Hint, you may want a recursive helper method def fibonacci(n) return fibonacci_helper([0,1], 2, n) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..448c6de 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,10 +1,29 @@ +require "pry" # Superdigit -# Time Complexity - ? -# Space Complexity - ? -def super_digit(n) - +# Time Complexity - super_digit_helper is O(log(n)) and this gets called O(n) in super_digit where n is the number of integers in num. So it's O(n). +# Space Complexity - O(1) + + +def super_digit_helper(n) + if n == 0 + return 0 + end + + return n % 10 + super_digit_helper(n/10) end + +def super_digit(num) + # binding.pry + super_num = super_digit_helper(num) + if super_num > 9 + super_digit(super_num) + else + return super_num + end + +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)