From aab8077d60020c814f9b45fedcca7abe5d014fc9 Mon Sep 17 00:00:00 2001 From: Sabrina Lowney Date: Fri, 15 Nov 2019 10:49:23 -0800 Subject: [PATCH 1/7] implemented fibonacci function --- lib/fibonacci.rb | 28 +++++++++++++++++++++++++--- test/fibonacci_test.rb | 4 ++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..0ca1e7b 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,8 +1,30 @@ # Improved Fibonacci -# Time Complexity - ? -# Space Complexity - ? (should be O(n)) +# Time Complexity - O(n) +# Space Complexity - O(n) +# you only keep the last two solutions, so the solutions array does not grow, +# but you're still effectiving the stack by adding recursive calls # Hint, you may want a recursive helper method def fibonacci(n) - + if n < 0 + raise ArgumentError.new('Number cannot be less than zero') + end + + return fib_helper([0, 1], 2, n) +end + +def fib_helper(solutions, current, n) + if n == 0 || n == 1 + return n + end + + last_solution = solutions[-1] + new_solution = solutions[0] + solutions[1] + + if current == n + return new_solution + end + + solutions = [last_solution, new_solution] + return fib_helper(solutions, current + 1, n) 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 0442f4d334a4cefcd27df6cc25af30ae51f6dbfd Mon Sep 17 00:00:00 2001 From: Sabrina Lowney Date: Fri, 15 Nov 2019 11:14:37 -0800 Subject: [PATCH 2/7] implemented super_digit --- lib/super_digit.rb | 19 +++++++++++++++---- test/super_digit_test.rb | 4 ++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..f58fbfb 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -3,13 +3,24 @@ # Time Complexity - ? # Space Complexity - ? def super_digit(n) - + while (n / 10) != 0 + n = super_digit_helper(n) + end + return n +end + +def super_digit_helper(n) + if (n / 10) == 0 + return n + else + last_digit = (n % 10) + n = (n / 10) + return last_digit + super_digit_helper(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 0da2838966ec531da855e2fba7a5043f75f44c46 Mon Sep 17 00:00:00 2001 From: Sabrina Lowney Date: Fri, 15 Nov 2019 14:51:21 -0800 Subject: [PATCH 3/7] added refined_super_digit --- lib/super_digit.rb | 15 +++++++++++++++ test/super_digit_test.rb | 38 +++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index f58fbfb..c5f5a80 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -19,8 +19,23 @@ def super_digit_helper(n) end end +# def super_digit(n) +# if n < 10 +# return n +# else +# super_digit(n % 10 + super_digit(n / 10)) +# end +# end + # Time Complexity - ? # Space Complexity - ? def refined_super_digit(n, k) + concatenated_n = '' + while k > 0 + concatenated_n += n.to_s + k -= 1 + end + concatenated_n = concatenated_n.to_i + return super_digit(concatenated_n) end diff --git a/test/super_digit_test.rb b/test/super_digit_test.rb index 20973f1..ac68984 100644 --- a/test/super_digit_test.rb +++ b/test/super_digit_test.rb @@ -4,58 +4,58 @@ it "will return 2 for super_digit(9875)" do # Act answer = super_digit(9875) - + # Assert expect(answer).must_equal 2 end - + it "will return 5 for super_digit(5)" do # Act answer = super_digit(5) - + # Assert expect(answer).must_equal 5 - end - + end + it "will return 6 for super_digit(123)" do # Act answer = super_digit(123) - + # Assert expect(answer).must_equal 6 end - + it "will return 6 for super_digit(12327)" do # Act answer = super_digit(12327) - + # Assert 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) - + # Assert expect(answer).must_equal 1 end - + it "will return 8 for n=9875 and k = 4" do # Act answer = refined_super_digit(9875, 4) - + # Assert expect(answer).must_equal 8 end - + it "will return 3 for n=148 and k = 3" do - # Act - answer = refined_super_digit(148, 3) - - # Assert - expect(answer).must_equal 3 + # Act + answer = refined_super_digit(148, 3) + + # Assert + expect(answer).must_equal 3 end end end From af367b1900b9199f83781fb674e32650495abb4e Mon Sep 17 00:00:00 2001 From: Sabrina Lowney Date: Fri, 15 Nov 2019 14:55:15 -0800 Subject: [PATCH 4/7] added a test case for super_digit that adds up to a three digit number initially --- test/super_digit_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/super_digit_test.rb b/test/super_digit_test.rb index ac68984..ce03b56 100644 --- a/test/super_digit_test.rb +++ b/test/super_digit_test.rb @@ -33,6 +33,14 @@ expect(answer).must_equal 6 end + it "will return 9 for super_digit(999_999_999_999)" do + # Act + answer = super_digit(999_999_999_999) + + # Assert + expect(answer).must_equal 9 + end + describe "refined superdigit" do it "will return 1 for n = 1 and k = 1" do # Act From 97d8c170edae0b262760e8a5e1f57951f334c87b Mon Sep 17 00:00:00 2001 From: Sabrina Lowney Date: Sun, 17 Nov 2019 14:48:07 -0800 Subject: [PATCH 5/7] refactored super_digit, added time/space complexity --- lib/super_digit.rb | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index c5f5a80..be1703c 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,32 +1,22 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(n) +# Space Complexity - O(log2 n) def super_digit(n) - while (n / 10) != 0 - n = super_digit_helper(n) - end - return n -end - -def super_digit_helper(n) + puts "super_digit("+n.to_s+")" if (n / 10) == 0 return n else - last_digit = (n % 10) - n = (n / 10) - return last_digit + super_digit_helper(n) + sum = 0 + while ((n / 10.0) > 0) + puts " while" + sum += (n % 10) + n = (n / 10) + end + return super_digit(sum) end end -# def super_digit(n) -# if n < 10 -# return n -# else -# super_digit(n % 10 + super_digit(n / 10)) -# end -# end - # Time Complexity - ? # Space Complexity - ? def refined_super_digit(n, k) From 041b26fea7fe7edbd26e46d6d1cb3120a865a14d Mon Sep 17 00:00:00 2001 From: Sabrina Lowney Date: Sun, 17 Nov 2019 14:57:23 -0800 Subject: [PATCH 6/7] added time/space complexity for super super_digit --- lib/super_digit.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index be1703c..c445b71 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,7 +1,7 @@ # Superdigit -# Time Complexity - O(n) -# Space Complexity - O(log2 n) +# Time Complexity - O(n) where n is the number of digits in the input number +# Space Complexity - O(log n) def super_digit(n) puts "super_digit("+n.to_s+")" if (n / 10) == 0 @@ -17,8 +17,10 @@ def super_digit(n) end end -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(n) +# because the while loop to create the concatenated number +# and the while loop adding the digits of the number are not nested. +# Space Complexity - O(log k * n) def refined_super_digit(n, k) concatenated_n = '' while k > 0 From 40b903098fcc2a5219f384b00ca1531151842f9d Mon Sep 17 00:00:00 2001 From: Sabrina Lowney Date: Thu, 21 Nov 2019 11:02:58 -0800 Subject: [PATCH 7/7] removed puts statements --- lib/super_digit.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index c445b71..d2cdfc0 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -3,13 +3,11 @@ # Time Complexity - O(n) where n is the number of digits in the input number # Space Complexity - O(log n) def super_digit(n) - puts "super_digit("+n.to_s+")" if (n / 10) == 0 return n else sum = 0 while ((n / 10.0) > 0) - puts " while" sum += (n % 10) n = (n / 10) end