From 193dc1b9bfe0c2287fe9640d5d39767e20037432 Mon Sep 17 00:00:00 2001 From: Eve Le Date: Mon, 18 Nov 2019 16:57:38 -0800 Subject: [PATCH 1/4] Implemented fibonacci method --- lib/fibonacci.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..2415499 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,8 +1,22 @@ # Improved Fibonacci -# Time Complexity - ? -# Space Complexity - ? (should be O(n)) +# Time Complexity - O(n) +# Space Complexity - O(n) stack space # Hint, you may want a recursive helper method def fibonacci(n) - + return fib_helper([0, 1], 2, n) end + +def fib_helper(results, current, n) + raise ArgumentError if n < 0 + return results[n] if n == 0 || n == 1 + + if current == n + return results[0] + results[1] + end + + temp = results[0] + results[1] + results[0] = results[1] + results[1] = temp + return fib_helper(results, current + 1, n) +end \ No newline at end of file From 3b6501faae4a59d3baf2ba35dae490b93e52a8c1 Mon Sep 17 00:00:00 2001 From: Eve Le Date: Mon, 18 Nov 2019 21:38:47 -0800 Subject: [PATCH 2/4] Implemented super_digit method --- lib/super_digit.rb | 14 ++++++++++---- test/super_digit_test.rb | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..b78e71c 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -3,13 +3,19 @@ # Time Complexity - ? # Space Complexity - ? def super_digit(n) - + return n if n/10 == 0 + + sum = n % 10 + while n/10 != 0 + n = n/10 + sum += n % 10 + end + return super_digit(sum) 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 29cc616d7f7c1581704022f877d00b2c1e6fbc43 Mon Sep 17 00:00:00 2001 From: Eve Le Date: Mon, 18 Nov 2019 22:16:11 -0800 Subject: [PATCH 3/4] Implemented refined_super_digit --- lib/super_digit.rb | 15 ++++++++++----- test/super_digit_test.rb | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index b78e71c..a853cbd 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,7 +1,7 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(logn) n is the number itself +# Space Complexity - O(logn) def super_digit(n) return n if n/10 == 0 @@ -14,8 +14,13 @@ def super_digit(n) end -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(logn) n is the number itself +# Space Complexity - O(logn) def refined_super_digit(n, k) - + return n if k == 1 && n/10 == 0 + sum = 0 + k.times do + sum += super_digit(n) + end + return super_digit(sum) end diff --git a/test/super_digit_test.rb b/test/super_digit_test.rb index 20973f1..8eb6475 100644 --- a/test/super_digit_test.rb +++ b/test/super_digit_test.rb @@ -33,7 +33,7 @@ expect(answer).must_equal 6 end - xdescribe "refined superdigit" do + describe "refined superdigit" do it "will return 1 for n = 1 and k = 1" do # Act answer = refined_super_digit(1, 1) From 55a0e7fdb80626439fad4a3de9b43aaa546d5d54 Mon Sep 17 00:00:00 2001 From: Eve Le Date: Tue, 19 Nov 2019 22:26:51 -0800 Subject: [PATCH 4/4] Simplified refined_super_digit --- lib/super_digit.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index a853cbd..a21339d 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -4,7 +4,7 @@ # Space Complexity - O(logn) def super_digit(n) return n if n/10 == 0 - + sum = n % 10 while n/10 != 0 n = n/10 @@ -19,8 +19,6 @@ def super_digit(n) def refined_super_digit(n, k) return n if k == 1 && n/10 == 0 sum = 0 - k.times do - sum += super_digit(n) - end + sum += super_digit(n) * k return super_digit(sum) end