From b8d70e7a620fb027f90a99bf8d0b5c672699f2fa Mon Sep 17 00:00:00 2001 From: Yasmin Date: Thu, 19 Mar 2020 15:41:11 -0700 Subject: [PATCH 1/6] cloned dynamic-programming --- 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 5204edb..eb679e4 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -4,5 +4,5 @@ def max_sub_array(nums) return 0 if nums == nil - raise NotImplementedError, "Method not implemented yet!" + # raise NotImplementedError, "Method not implemented yet!" end From e8b061eefc61054a2b57b2bd267ce02ca0a21285 Mon Sep 17 00:00:00 2001 From: Yasmin Date: Sun, 29 Mar 2020 13:47:05 -0700 Subject: [PATCH 2/6] implemented newman-conway method, passing tests --- lib/newman_conway.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..7eef999 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,23 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: o(n) +# Space Complexity: o(n) def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + # raise NotImplementedError, "newman_conway isn't implemented" + raise ArgumentError if num < 1 + return "1" if num == 1 + return "1 1" if num == 2 + + newman_conway = [0, 1, 1] + output = "1 1" + + i = 3 + + while i <= num + #p(i) = p(p(i-1)) + p(i - p(i - 1)) + newman_conway[i] = newman_conway[newman_conway[i - 1]] + newman_conway [i - newman_conway[i - 1]] + output += " #{newman_conway[i]}" + i += 1 + end + return output end \ No newline at end of file From dac3677868ae69d241b1619cdc55a06d99b5d2c9 Mon Sep 17 00:00:00 2001 From: Yasmin Date: Sun, 29 Mar 2020 13:49:09 -0700 Subject: [PATCH 3/6] changed output to sequence --- 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 7eef999..66135c6 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -9,15 +9,15 @@ def newman_conway(num) return "1 1" if num == 2 newman_conway = [0, 1, 1] - output = "1 1" + sequence = "1 1" i = 3 while i <= num #p(i) = p(p(i-1)) + p(i - p(i - 1)) newman_conway[i] = newman_conway[newman_conway[i - 1]] + newman_conway [i - newman_conway[i - 1]] - output += " #{newman_conway[i]}" + sequence += " #{newman_conway[i]}" i += 1 end - return output + return sequence end \ No newline at end of file From 1fd92a17a6490397444f939813af69d04c384208 Mon Sep 17 00:00:00 2001 From: Yasmin Date: Sun, 29 Mar 2020 13:53:47 -0700 Subject: [PATCH 4/6] fixed indentation --- lib/newman_conway.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 66135c6..c7e1aa6 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -19,5 +19,5 @@ def newman_conway(num) sequence += " #{newman_conway[i]}" i += 1 end - return sequence + return sequence end \ No newline at end of file From 982b88b79726ccf305e75c3da2f2cd4d7a63c031 Mon Sep 17 00:00:00 2001 From: Yasmin Date: Sun, 29 Mar 2020 16:31:22 -0700 Subject: [PATCH 5/6] implemented Largest Sum Contiguous, passing tests --- lib/max_subarray.rb | 33 +++++++++++++++++++++++++++++---- lib/newman_conway.rb | 3 +-- test/max_sub_array_test.rb | 2 +- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index eb679e4..1b28d08 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,33 @@ +# Time Complexity: o(n) +# Space Complexity: o(1) -# Time Complexity: ? -# Space Complexity: ? def max_sub_array(nums) - return 0 if nums == nil - + return 0 if nums == nil # raise NotImplementedError, "Method not implemented yet!" + max_so_far = nums[0] + max_ending_here = 0 + + nums.each do |num| + max_ending_here = max_ending_here + num + + if max_ending_here < num + max_ending_here = num + end + if max_so_far < max_ending_here + max_so_far = max_ending_here + end + end + return max_so_far end + +# Initialize: +# max_so_far = 0 +# max_ending_here = 0 + +# Loop for each element of the array +# (a) max_ending_here = max_ending_here + a[i] +# (b) if(max_ending_here < 0) +# max_ending_here = 0 +# (c) if(max_so_far < max_ending_here) +# max_so_far = max_ending_here +# return max_so_far diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index c7e1aa6..85eb714 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,6 @@ - - # Time complexity: o(n) # Space Complexity: o(n) + def newman_conway(num) # raise NotImplementedError, "newman_conway isn't implemented" raise ArgumentError if num < 1 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 2038ded53e498c65b3af5707cf17347cbab321aa Mon Sep 17 00:00:00 2001 From: Yasmin Date: Sun, 29 Mar 2020 16:36:57 -0700 Subject: [PATCH 6/6] fixed indentations --- lib/max_subarray.rb | 2 +- lib/newman_conway.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 1b28d08..4ff07a5 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,3 +1,4 @@ + # Time Complexity: o(n) # Space Complexity: o(1) @@ -9,7 +10,6 @@ def max_sub_array(nums) nums.each do |num| max_ending_here = max_ending_here + num - if max_ending_here < num max_ending_here = num end diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 85eb714..23ac6dc 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,3 +1,4 @@ + # Time complexity: o(n) # Space Complexity: o(n)