From f2850daed8768c148f8c397f510faad744f4e81d Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Sat, 5 Oct 2019 14:07:46 -0700 Subject: [PATCH 1/4] Completed newman conway. --- lib/newman_conway.rb | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..8456645 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,29 @@ +# Time complexity: O(n) +# Space Complexity: O(n) - -# Time complexity: ? -# Space Complexity: ? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + solution = [1] + raise ArgumentError, "Number must be 1 or greater" if num <1 + p(num, solution) + + string_solution = "" + solution.each do |char| + string_solution << "#{char.to_s} " + end + + string_solution.delete_suffix!(" ") + + return string_solution +end + +def p(n, s) + return s[n-1] if s[n-1] + + if (n == 2) && !s[2-1] + s[2-1] = 1 + else + s[n-1] = p(p(n - 1, s), s) + p(n - p(n - 1, s), s) + end + + return s[n-1] end \ No newline at end of file From f89ed7a75b27c0d57680c7c87b2dab7bbbdd18d5 Mon Sep 17 00:00:00 2001 From: Elle Kosling Date: Sat, 5 Oct 2019 18:54:17 -0700 Subject: [PATCH 2/4] Wrote max_subarray method. Passes provided tests, fails one I wrote. --- lib/max_subarray.rb | 25 +++++++++++++++++++++---- lib/newman_conway.rb | 4 +--- test/max_sub_array_test.rb | 15 ++++++++++++++- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..5ea2dc5 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,25 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) where n is the length of the array +# Space Complexity: O(1) + def max_sub_array(nums) - return 0 if nums == nil + return nil if nums == [] + + max_so_far = nums[0] + max_ending_here = nums[0] + + nums[1..-1].each do |num| + + max_ending_here = max_ending_here + num + + if max_ending_here < 0 + max_so_far = max_ending_here = [max_so_far, num].max + elsif max_so_far < max_ending_here + max_so_far = max_ending_here + else max_so_far < 0 && num >=0 + max_so_far = num + end + end - raise NotImplementedError, "Method not implemented yet!" + return max_so_far end diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 8456645..4d7a47b 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -11,9 +11,7 @@ def newman_conway(num) string_solution << "#{char.to_s} " end - string_solution.delete_suffix!(" ") - - return string_solution + return string_solution[0...-1] end def p(n, s) diff --git a/test/max_sub_array_test.rb b/test/max_sub_array_test.rb index 3253cdf..b0daa9c 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] @@ -67,4 +67,17 @@ expect(answer).must_equal 50 end + it "will work for [50, -51, 50]" do + # This test (which I added) is failing + + # Arrange + input = [50, -51, 50] + + # Act + answer = max_sub_array(input) + + # Assert + expect(answer).must_equal 50 + end + end \ No newline at end of file From bceadb1dfcbf8537ef3c8293bc362c6088549480 Mon Sep 17 00:00:00 2001 From: Elle Kosling Date: Sat, 12 Oct 2019 13:48:20 -0700 Subject: [PATCH 3/4] Fixed, function passes my new tests. --- lib/max_subarray.rb | 14 ++++---------- test/max_sub_array_test.rb | 11 +++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5ea2dc5..6b303b5 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -6,18 +6,12 @@ def max_sub_array(nums) return nil if nums == [] max_so_far = nums[0] - max_ending_here = nums[0] + sum_ending_here = nums[0] nums[1..-1].each do |num| - - max_ending_here = max_ending_here + num - - if max_ending_here < 0 - max_so_far = max_ending_here = [max_so_far, num].max - elsif max_so_far < max_ending_here - max_so_far = max_ending_here - else max_so_far < 0 && num >=0 - max_so_far = num + sum_ending_here = [sum_ending_here + num, num].max + if max_so_far < sum_ending_here + max_so_far = sum_ending_here end end diff --git a/test/max_sub_array_test.rb b/test/max_sub_array_test.rb index b0daa9c..37635f0 100644 --- a/test/max_sub_array_test.rb +++ b/test/max_sub_array_test.rb @@ -80,4 +80,15 @@ expect(answer).must_equal 50 end + it "will work for [-2,1,-3,4,-1,2,1,-5,4,12]" do + # Arrange + input = [4,-2,1,-3,4,-1,2,1,-5,4,12] + + # Act + answer = max_sub_array(input) + + # Assert + expect(answer).must_equal 17 + end + end \ No newline at end of file From c27c87488c3dd28d7fe1fa3082c80db274554e64 Mon Sep 17 00:00:00 2001 From: Elle Kosling Date: Sat, 12 Oct 2019 14:12:50 -0700 Subject: [PATCH 4/4] Added solution for my first approach. --- lib/max_subarray.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 6b303b5..d451574 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -8,12 +8,32 @@ def max_sub_array(nums) max_so_far = nums[0] sum_ending_here = nums[0] + # ----------------------------------------------------- + # SOLUTION 1 nums[1..-1].each do |num| + sum_ending_here = [sum_ending_here + num, num].max + if max_so_far < sum_ending_here max_so_far = sum_ending_here end end + + # ----------------------------------------------------- + + # SOLUTION 2 + + # nums[1..-1].each do |num| + + # sum_ending_here = sum_ending_here + num + + # if max_so_far < sum_ending_here + # max_so_far = sum_ending_here + # elsif sum_ending_here < 0 + # sum_ending_here = 0 + # end + # end + # ----------------------------------------------------- return max_so_far end