From 25ab8451f4ea0e3b25da857be6e80286861aeb32 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Tue, 6 Oct 2020 21:08:57 -0700 Subject: [PATCH 1/3] Adds max_subarray method. --- lib/max_subarray.rb | 24 ++++++++++++++++++++---- test/max_sub_array_test.rb | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..e3d3baa 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,24 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(1) def max_sub_array(nums) - return 0 if nums == nil + return nil if nums.nil? || nums.empty? + return nums[0] if nums.length == 1 + + highest_peak = nums[0] + highest = nums[0] + i = 1 + + while i < nums.length + highest_peak += nums[i] + + highest_peak = nums[i] if highest_peak < nums[i] + highest = highest_peak if highest < highest_peak + i += 1 + end + + return highest - raise NotImplementedError, "Method not implemented yet!" end + + diff --git a/test/max_sub_array_test.rb b/test/max_sub_array_test.rb index 3253cdf..e27e1ca 100644 --- a/test/max_sub_array_test.rb +++ b/test/max_sub_array_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -xdescribe "max subarray" do +describe "max subarray" do it "will work for [-2,1,-3,4,-1,2,1,-5,4]" do # Arrange input = [-2,1,-3,4,-1,2,1,-5,4] From 2efaf47ea482fc85d76ee54593fd9a63cf4bb755 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Tue, 6 Oct 2020 22:06:03 -0700 Subject: [PATCH 2/3] Adds newman_conway method. --- lib/newman_conway.rb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..a504a70 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,26 @@ - - # Time complexity: ? # Space Complexity: ? + def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file + raise ArgumentError.new if num == 0 + + return "1" if num == 1 + return "1 1" if num == 2 + + array = [] + array[0] = 0 + array[1] = 1 + array[2] = 1 + tally = "1 1" + i = 3 + + until i > num + array[i] = array[array[i - 1]] + array[i - array[i - 1]] + tally += " #{array[i]}" + i += 1 + end + + return tally +end + + From 86947a98b95b87ff8ebde7a87d25859c8734c364 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Tue, 6 Oct 2020 22:09:55 -0700 Subject: [PATCH 3/3] Adds complexity and citation. --- lib/newman_conway.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index a504a70..a71d2fe 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,5 +1,5 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: O(n) +# Space Complexity: O(n) def newman_conway(num) raise ArgumentError.new if num == 0 @@ -23,4 +23,4 @@ def newman_conway(num) return tally end - +#research source: https://www.geeksforgeeks.org/print-n-terms-newman-conway-sequence/