From 39793810fd88d7aae50833a95357037f301dcebd Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Sun, 17 Nov 2019 19:22:18 -0800 Subject: [PATCH 1/5] Super Digit --- lib/super_digit.rb | 11 ++++++++++- test/fibonacci_test.rb | 2 +- test/super_digit_test.rb | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..75f94eb 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -3,7 +3,16 @@ # Time Complexity - ? # Space Complexity - ? def super_digit(n) - + + return n if [0,1,2,3,4,5,6,7,8,9].include?(n) == true + + num = n.digits + sum = 0 + num.each do |digit| + sum += digit + end + + super_digit(sum) end 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..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 b04daeff9d12c02dcfa3ec17ee585dbd60afca66 Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Sun, 17 Nov 2019 19:47:54 -0800 Subject: [PATCH 2/5] Redifined Super Digit --- lib/super_digit.rb | 30 ++++++++++++++++++++++++------ test/super_digit_test.rb | 2 +- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 75f94eb..9fe18ed 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -3,13 +3,14 @@ # Time Complexity - ? # Space Complexity - ? def super_digit(n) - + return n if [0,1,2,3,4,5,6,7,8,9].include?(n) == true - num = n.digits sum = 0 - num.each do |digit| - sum += digit + until n == 0 do + reminder = n % 10 + sum += reminder + n = n/10 end super_digit(sum) @@ -19,6 +20,23 @@ def super_digit(n) # Time Complexity - ? # Space Complexity - ? def refined_super_digit(n, k) - + + return n if [0,1,2,3,4,5,6,7,8,9].include?(n) == true && k == 1 + + sum = 0 + until n == 0 do + reminder = n % 10 + sum += reminder + n = n/10 + end + + super_digit = sum * k + refined_super_digit(super_digit, 1) end - \ No newline at end of file + + + + + + + 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 ba6891582334528ff1810c023da00a4f13001d89 Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Mon, 18 Nov 2019 09:14:44 -0800 Subject: [PATCH 3/5] Fibonacci tests --- lib/fibonacci.rb | 6 +++++- lib/super_digit.rb | 12 ++++++------ test/fibonacci_test.rb | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..94e337e 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -4,5 +4,9 @@ # Space Complexity - ? (should be O(n)) # Hint, you may want a recursive helper method def fibonacci(n) - + return 0 if n == 0 + return 1 if n == 1 + raise ArgumentError if n < 0 + + return fibonacci(n-2) + fibonacci(n-1) end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 9fe18ed..5e3b3e1 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,7 +1,7 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity : O(n), depends on number of digits +# Space Complexity : O(1) as the array of digits will always be same and I will always have same variables (sum, reminder) def super_digit(n) return n if [0,1,2,3,4,5,6,7,8,9].include?(n) == true @@ -17,8 +17,8 @@ def super_digit(n) end -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(n), depends on number of digits +# Space Complexity : O(1) as the array of digits will always be same and I will always have same variables (sum, reminder, superdigit) def refined_super_digit(n, k) return n if [0,1,2,3,4,5,6,7,8,9].include?(n) == true && k == 1 @@ -30,8 +30,8 @@ def refined_super_digit(n, k) n = n/10 end - super_digit = sum * k - refined_super_digit(super_digit, 1) + redifined_number = sum * k + refined_super_digit(redifined_number, 1) end diff --git a/test/fibonacci_test.rb b/test/fibonacci_test.rb index e6c2a7b..639f3b1 100644 --- a/test/fibonacci_test.rb +++ b/test/fibonacci_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -xdescribe "Fibonacci" do +describe "Fibonacci" do it "will raise an error for n < 0" do expect { # Act From 6a4eecdc75dc6f0f9adff75cb8c10177589ad271 Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Mon, 18 Nov 2019 09:16:05 -0800 Subject: [PATCH 4/5] Minor change --- lib/fibonacci.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 94e337e..5d617bf 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,12 +1,12 @@ # Improved Fibonacci -# Time Complexity - ? +# Time Complexity - O(n) # Space Complexity - ? (should be O(n)) # Hint, you may want a recursive helper method def fibonacci(n) return 0 if n == 0 return 1 if n == 1 raise ArgumentError if n < 0 - + return fibonacci(n-2) + fibonacci(n-1) end From 68f7f7b343ae879e306433d6da09d523344a7005 Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Mon, 18 Nov 2019 20:55:36 -0800 Subject: [PATCH 5/5] Fibonnaci with recursive solution and dynamic programming --- lib/fibonacci.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 5d617bf..2be243a 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -4,9 +4,17 @@ # Space Complexity - ? (should be O(n)) # Hint, you may want a recursive helper method def fibonacci(n) - return 0 if n == 0 - return 1 if n == 1 + return fib_helper( [0,1], 2, n ) +end + +def fib_helper( solutions, current, n ) + return n if n == 0 || n == 1 raise ArgumentError if n < 0 - return fibonacci(n-2) + fibonacci(n-1) + if current == n + return solutions[n - 1] + solutions[n - 2] + end + + solutions << solutions[current - 1] + solutions[current -2] + return fib_helper(solutions, current + 1, n) end