From d9610338ef9e3440bfe41c7d02f1768b01ae8877 Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Thu, 14 Nov 2019 17:25:38 -0800 Subject: [PATCH 1/5] start fib, get tests passing on super_digit --- lib/fibonacci.rb | 21 ++++++++++++++------- lib/super_digit.rb | 16 ++++++++++++---- test/super_digit_test.rb | 8 ++++---- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..5e4d23b 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,8 +1,15 @@ -# Improved Fibonacci +# # Improved Fibonacci -# Time Complexity - ? -# Space Complexity - ? (should be O(n)) -# Hint, you may want a recursive helper method -def fibonacci(n) - -end +# # Time Complexity - ? +# # 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(n-1) + fib(n-2) +# end +# end + +# def fib_helper(n) +# fib_saver = [] +# end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..69c1ab3 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -3,13 +3,21 @@ # Time Complexity - ? # Space Complexity - ? def super_digit(n) - + return n if n < 10 + super_digit(digitize(n)) end - + +def digitize(n) + return n if n < 10 + sum = 0 + single_digit = n % 10 + sum += single_digit + return sum + digitize(n / 10) +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..4547940 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,13 +33,13 @@ expect(answer).must_equal 6 end - describe "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 + expect(answer).must_equal 1 end it "will return 8 for n=9875 and k = 4" do @@ -53,7 +53,7 @@ it "will return 3 for n=148 and k = 3" do # Act answer = refined_super_digit(148, 3) - + # Assert expect(answer).must_equal 3 end From 0ca11d93d9ab0cf6e30e5a58328b9a5ab0293320 Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sat, 16 Nov 2019 17:34:14 -0800 Subject: [PATCH 2/5] fib tests all passing --- lib/fibonacci.rb | 26 ++++++++++++++++---------- lib/super_digit.rb | 10 +++++++++- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 5e4d23b..16dcb1d 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -3,13 +3,19 @@ # # Time Complexity - ? # # 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(n-1) + fib(n-2) -# end -# end - -# def fib_helper(n) -# fib_saver = [] -# end + +def fibonacci(n) + raise ArgumentError if n < 0 + return fib_helper([0, 1], 2, n) +end + +def fib_helper(fib_saver, current, n) + return n if n == 0 || n == 1 + return fib_saver.sum if current == n + + new_sum = fib_saver.sum + fib_saver[0] = fib_saver[1] + fib_saver[1] = new_sum + # is this better/different than `fib_saver = [fib_saver[1], new_sum]`? + return fib_helper(fib_saver, current + 1, n) +end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 69c1ab3..51560a6 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -19,5 +19,13 @@ def digitize(n) # Time Complexity - ? # Space Complexity - ? def refined_super_digit(n, k) - + super_digit(n * k) end + +def digitize(n) + return n if n < 10 + sum = 0 + single_digit = n % 10 + sum += single_digit + return sum + digitize(n / 10) +end \ No newline at end of file From e68add479fff06f03ab7f3fb12042550ebdba891 Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 17 Nov 2019 18:44:24 -0800 Subject: [PATCH 3/5] tried to answer time and space complexity questions --- lib/fibonacci.rb | 4 +--- lib/super_digit.rb | 16 ++++------------ 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 16dcb1d..83c0c9b 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,6 +1,6 @@ # # Improved Fibonacci -# # Time Complexity - ? +# # Time Complexity - O(n) # # Space Complexity - ? (should be O(n)) # # Hint, you may want a recursive helper method @@ -12,10 +12,8 @@ def fibonacci(n) def fib_helper(fib_saver, current, n) return n if n == 0 || n == 1 return fib_saver.sum if current == n - new_sum = fib_saver.sum fib_saver[0] = fib_saver[1] fib_saver[1] = new_sum - # is this better/different than `fib_saver = [fib_saver[1], new_sum]`? return fib_helper(fib_saver, current + 1, n) end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 51560a6..a699c72 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,7 +1,7 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(nlogn)? +# Space Complexity - O(logn)? def super_digit(n) return n if n < 10 super_digit(digitize(n)) @@ -16,16 +16,8 @@ def digitize(n) end -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - I'm pretty sure this solution doesn't reduce the time complexity, unfortunately. So it's O(knlogn)? +# Space Complexity - O(logn)? def refined_super_digit(n, k) super_digit(n * k) end - -def digitize(n) - return n if n < 10 - sum = 0 - single_digit = n % 10 - sum += single_digit - return sum + digitize(n / 10) -end \ No newline at end of file From 46c6da2d046edf17456bcb21f79ad32719377581 Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 17 Nov 2019 21:01:30 -0800 Subject: [PATCH 4/5] minor tweaks --- lib/fibonacci.rb | 2 +- lib/super_digit.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 83c0c9b..12659e8 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,7 +1,7 @@ # # Improved Fibonacci # # Time Complexity - O(n) -# # Space Complexity - ? (should be O(n)) +# # Space Complexity - O(n) (should be O(n)) # # Hint, you may want a recursive helper method def fibonacci(n) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index a699c72..dbfed0e 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -16,7 +16,7 @@ def digitize(n) end -# Time Complexity - I'm pretty sure this solution doesn't reduce the time complexity, unfortunately. So it's O(knlogn)? +# Time Complexity - I'm pretty sure this solution doesn't reduce the time complexity, unfortunately. So it's O(nlogn) still? # Space Complexity - O(logn)? def refined_super_digit(n, k) super_digit(n * k) From 3b1c54867b6b78565aa693b38b582354cbc3c2be Mon Sep 17 00:00:00 2001 From: Janice Huang Date: Sun, 17 Nov 2019 21:03:47 -0800 Subject: [PATCH 5/5] forgot to specify n --- lib/super_digit.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index dbfed0e..ad7bf1d 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,7 +1,7 @@ # Superdigit -# Time Complexity - O(nlogn)? -# Space Complexity - O(logn)? +# Time Complexity - O(nlog10n)? +# Space Complexity - O(log10n)? def super_digit(n) return n if n < 10 super_digit(digitize(n)) @@ -16,8 +16,8 @@ def digitize(n) end -# Time Complexity - I'm pretty sure this solution doesn't reduce the time complexity, unfortunately. So it's O(nlogn) still? -# Space Complexity - O(logn)? +# Time Complexity - I'm pretty sure this solution doesn't reduce the time complexity, unfortunately. So it's O(nlog10n) still? +# Space Complexity - O(log10n)? def refined_super_digit(n, k) super_digit(n * k) end