From 425cd8b54f5f88fc6cbf1a82aceac62a64bbaf06 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Sat, 28 Mar 2020 23:56:21 -0700 Subject: [PATCH 1/2] completed newman conway --- lib/newman_conway.rb | 28 ++++++++++++++---- test/newman_conway_test.rb | 58 +++++++++++++++++++------------------- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..16016b8 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,23 @@ - - -# Time complexity: ? -# Space Complexity: ? +# Time complexity: O(n * num) ? +# Space Complexity: O(num) ? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file + newman_conway_helper([0, 1, 1], 3, num) +end + +def newman_conway_helper(p, current, n) + if n <= 0 + raise ArgumentError, "Input must be greater than 0." + end + + return "1" if n == 1 + return "1 1" if n == 2 + + if current > n + p.shift # S: O(n)? T: O(n)? + return p.join(" ") + end + + p << p[p[current - 1]] + p[current - p[current - 1]] + + return newman_conway_helper(p, current + 1, n) # T: O(num) calls this recursively about num number of times +end diff --git a/test/newman_conway_test.rb b/test/newman_conway_test.rb index 537d376..c1661a6 100644 --- a/test/newman_conway_test.rb +++ b/test/newman_conway_test.rb @@ -4,51 +4,51 @@ it "works with 13" do # Arrange input = 13 - + # Act answer = newman_conway(input) - + # Assert expect(answer).must_equal "1 1 2 2 3 4 4 4 5 6 7 7 8" end - + it "works with 20" do # Arrange input = 20 - + # Act answer = newman_conway(input) - + # Assert expect(answer).must_equal "1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12" end - + it "works with base cases" do - # Arrange - input = 0 - - # Act-Assert - expect { - newman_conway(input) - }.must_raise ArgumentError + # Arrange + input = 0 - - # Arrange - input = 1 - - # Act - answer = newman_conway(input) + # Act-Assert + expect { + newman_conway(input) + }.must_raise ArgumentError - # Assert - expect(answer).must_equal "1" - - # Arrange - input = 2 - - # Act - answer = newman_conway(input) - # Assert - expect(answer).must_equal "1 1" + # 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 3937c4ed5d48e6344d37092a86f9730e9478ec6f Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Thu, 23 Apr 2020 23:25:46 -0700 Subject: [PATCH 2/2] completed max_subarray --- lib/max_subarray.rb | 21 +++++++++++++--- test/max_sub_array_test.rb | 50 +++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..4b7d414 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,23 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(nums) +# Space Complexity: O(1) def max_sub_array(nums) return 0 if nums == nil - raise NotImplementedError, "Method not implemented yet!" + max_so_far = nums[0] + max_ending_here = nums[0] + + counter = 1 + until counter > nums.length - 1 # T: O(nums) + max_ending_here = max_ending_here + nums[counter] + if max_ending_here < nums[counter] + max_ending_here = nums[counter] + end + + if max_ending_here > max_so_far + max_so_far = max_ending_here + end + counter += 1 + end + return max_so_far end diff --git a/test/max_sub_array_test.rb b/test/max_sub_array_test.rb index 3253cdf..5f930ef 100644 --- a/test/max_sub_array_test.rb +++ b/test/max_sub_array_test.rb @@ -1,70 +1,70 @@ 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] - + # Act answer = max_sub_array(input) - + # Assert expect(answer).must_equal 6 end - + it "will work with a totally negative array" do # Arrange input = [-3, -4, -5, -6, -7] - + # Act answer = max_sub_array(input) - + # Assert expect(answer).must_equal(-3) end - + it "will work with a totally negative array with the largest element at the rear" do # Arrange input = [ -4, -5, -6, -7, -3] - + # Act answer = max_sub_array(input) - + # Assert expect(answer).must_equal(-3) end - + it "will work with a 1-element array" do # Arrange input = [3] - + # Act answer = max_sub_array(input) - + # Assert expect(answer).must_equal 3 end - + it "will return nil for an empty array" do - # Arrange - input = [] - - # Act - answer = max_sub_array(input) - - # Assert - expect(answer).must_be_nil + # Arrange + input = [] + + # Act + answer = max_sub_array(input) + + # Assert + expect(answer).must_be_nil end - + it "will work for [50, -50, 50]" do # Arrange input = [50, -50, 50] - + # Act answer = max_sub_array(input) - + # Assert expect(answer).must_equal 50 end - + end \ No newline at end of file