From a7512469dcd04fb2f24d2a513b6c81d3183f2daf Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Sun, 6 Oct 2019 16:44:58 -0700 Subject: [PATCH 1/6] working solution fo newman conway --- lib/newman_conway.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..41331eb 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,19 @@ +# Time complexity: O(n) where n is the size of num +# Space Complexity: +def newman_conway(num) + raise ArgumentError if num == 0 + output = [] + i = 1 -# Time complexity: ? -# Space Complexity: ? -def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file + while i <= num + output << conway_helper(i) + i += 1 + end + return output.join(" ") +end + +def conway_helper(num) + return 1 if num < 3 + conway_helper(conway_helper(num - 1)) + conway_helper(num - conway_helper(num - 1)) +end From bee849900730dce8a83d6d043873d060c7a6a3d3 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Mon, 7 Oct 2019 23:47:26 -0700 Subject: [PATCH 2/6] faster run time --- lib/newman_conway.rb | 8 +++--- test/max_sub_array_test.rb | 19 +++++++------- test/newman_conway_test.rb | 51 +++++++++++++++++++------------------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 41331eb..1cce641 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -7,13 +7,15 @@ def newman_conway(num) i = 1 while i <= num - output << conway_helper(i) + output << conway_helper(i, output) i += 1 end return output.join(" ") end -def conway_helper(num) +def conway_helper(num, output) return 1 if num < 3 - conway_helper(conway_helper(num - 1)) + conway_helper(num - conway_helper(num - 1)) + return output[num - 1] if output[num - 1] + helper = conway_helper(num - 1, output) + conway_helper(helper, output) + conway_helper(num - helper, output) end diff --git a/test/max_sub_array_test.rb b/test/max_sub_array_test.rb index 3253cdf..ba616ef 100644 --- a/test/max_sub_array_test.rb +++ b/test/max_sub_array_test.rb @@ -3,7 +3,7 @@ xdescribe "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] + input = [-2, 1, -3, 4, -1, 2, 1, -5, 4] # Act answer = max_sub_array(input) @@ -25,7 +25,7 @@ it "will work with a totally negative array with the largest element at the rear" do # Arrange - input = [ -4, -5, -6, -7, -3] + input = [-4, -5, -6, -7, -3] # Act answer = max_sub_array(input) @@ -46,14 +46,14 @@ end it "will return nil for an empty array" do - # Arrange - input = [] + # Arrange + input = [] - # Act - answer = max_sub_array(input) + # Act + answer = max_sub_array(input) - # Assert - expect(answer).must_be_nil + # Assert + expect(answer).must_be_nil end it "will work for [50, -50, 50]" do @@ -66,5 +66,4 @@ # Assert expect(answer).must_equal 50 end - -end \ No newline at end of file +end diff --git a/test/newman_conway_test.rb b/test/newman_conway_test.rb index 537d376..5476d10 100644 --- a/test/newman_conway_test.rb +++ b/test/newman_conway_test.rb @@ -24,31 +24,30 @@ end it "works with base cases" do - # Arrange - input = 0 - - # Act-Assert - expect { - newman_conway(input) - }.must_raise ArgumentError - - - # Arrange - input = 1 - - # Act - answer = newman_conway(input) - - # Assert - expect(answer).must_equal "1" - - # Arrange - input = 2 - - # Act - answer = newman_conway(input) - - # Assert - expect(answer).must_equal "1 1" + # Arrange + input = 0 + + # Act-Assert + expect { + newman_conway(input) + }.must_raise ArgumentError + + # Arrange + input = 1 + + # Act + answer = newman_conway(input) + + # Assert + expect(answer).must_equal "1" + + # Arrange + input = 2 + + # Act + answer = newman_conway(input) + + # Assert + expect(answer).must_equal "1 1" end end From 064574b1c7c9c0420a5edaea7e23e3a8d75c1a6b Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Tue, 8 Oct 2019 00:00:28 -0700 Subject: [PATCH 3/6] space and time --- lib/newman_conway.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 1cce641..4fb6e54 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,5 +1,5 @@ -# Time complexity: O(n) where n is the size of num -# Space Complexity: +# Time complexity: O(n) where n is the value of num +# Space Complexity: O(n) where n is the value of num def newman_conway(num) raise ArgumentError if num == 0 From 3000acab09fdd24551b50734a7483dbe61d9f0b7 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Tue, 8 Oct 2019 20:39:31 -0700 Subject: [PATCH 4/6] max subarray working --- lib/max_subarray.rb | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..d3455e5 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -2,7 +2,29 @@ # Time Complexity: ? # Space Complexity: ? def max_sub_array(nums) - return 0 if nums == nil - - raise NotImplementedError, "Method not implemented yet!" + return nil if nums[0] == nil + + i = 0 + max_so_far = nums[i] + + while nums[i] != nil + max_this_round = max_sub_array_helper(nums, i) + max_so_far = max_this_round > max_so_far ? max_this_round : max_so_far + i += 1 + end + return max_so_far +end + +def max_sub_array_helper(nums, i) + max_so_far = nums[i] + max_ending_here = 0 + + while nums[i] != nil + max_ending_here = max_ending_here + nums[i] + if max_so_far < max_ending_here + max_so_far = max_ending_here + end + i += 1 + end + return max_so_far end From 6612b7be4835aaace92e894133b7ba4066e0f14b Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Tue, 8 Oct 2019 23:23:29 -0700 Subject: [PATCH 5/6] time and space --- lib/max_subarray.rb | 30 ++++++++---------------------- test/max_sub_array_test.rb | 2 +- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index d3455e5..3daf7d8 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,30 +1,16 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) where n is the value of nums +# Space Complexity: O(1) def max_sub_array(nums) - return nil if nums[0] == nil + return 0 if nums == nil - i = 0 - max_so_far = nums[i] - - while nums[i] != nil - max_this_round = max_sub_array_helper(nums, i) - max_so_far = max_this_round > max_so_far ? max_this_round : max_so_far - i += 1 - end - return max_so_far -end - -def max_sub_array_helper(nums, i) - max_so_far = nums[i] + max_so_far = nil max_ending_here = 0 - while nums[i] != nil - max_ending_here = max_ending_here + nums[i] - if max_so_far < max_ending_here - max_so_far = max_ending_here - end - i += 1 + nums.each do |num| + max_ending_here += num + max_so_far = max_ending_here if !max_so_far || max_ending_here > max_so_far + max_ending_here = 0 if max_ending_here < 0 end return max_so_far end diff --git a/test/max_sub_array_test.rb b/test/max_sub_array_test.rb index ba616ef..c83ef94 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 5674e20c63df8532ce8d2dd37460a345d8837bec Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Tue, 8 Oct 2019 23:24:36 -0700 Subject: [PATCH 6/6] missed commit --- lib/max_subarray.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 3daf7d8..c6c6dee 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -8,7 +8,7 @@ def max_sub_array(nums) max_ending_here = 0 nums.each do |num| - max_ending_here += num + max_ending_here = max_ending_here + num max_so_far = max_ending_here if !max_so_far || max_ending_here > max_so_far max_ending_here = 0 if max_ending_here < 0 end