From 5211fdcab9b4fc2e7493c181354fa1e6d7393207 Mon Sep 17 00:00:00 2001 From: Stephanie Garcia Date: Mon, 18 Nov 2019 06:13:28 -0800 Subject: [PATCH] solutions to homework --- lib/fibonacci.rb | 19 ++++++++++++++++++- lib/super_digit.rb | 7 ++++++- test/fibonacci_test.rb | 2 +- test/super_digit_test.rb | 2 +- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..73deed9 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(n, solution = [0,1], current = 2) end + + def fibonacci_helper(n, s, current) + if n < 0 + raise ArgumentError + end + if n == 0 || n == 1 + return n + end + if current == n + return s[0] + s[1] + end + temp = s[1] + s[1] = s[0] + s[1] + s[0] = temp + return fibonacci_helper(n, s, current + 1) + end + diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..8c650b6 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -3,13 +3,18 @@ # Time Complexity - ? # Space Complexity - ? def super_digit(n) - + if n < 0 + return argumentError + end + return n if n.to_s.length == 1 + return super_digit(n.digits.sum) end # Time Complexity - ? # Space Complexity - ? def refined_super_digit(n, k) + 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)