From 7b79c62e253bc045d9199269ca974805f5d3322c Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 3 Oct 2019 16:10:01 -0700 Subject: [PATCH 1/4] added sequence --- lib/newman_conway.rb | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..c678fd5 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,25 @@ -# Time complexity: ? +# Time complexity: ? O(n) # Space Complexity: ? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file + sequence = [] + n = num + while n > 0 + sequence.push(get_newman_num(n)) + n -= 1 + end + return sequence +end + +def get_newman_num(num) + if num < 3 + return 1 + else + num = get_newman_num(get_newman_num(num - 1)) + get_newman_num(num - get_newman_num(num - 1)) + # P(n) = P(P(n - 1)) + P(n - P(n - 1)) + return num + end +end + +# puts newman_conway(20) \ No newline at end of file From 08e57832199a51ae73422e6f39a0cac58566bd56 Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 3 Oct 2019 16:20:06 -0700 Subject: [PATCH 2/4] fixed to reverse --- lib/newman_conway.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index c678fd5..9f79828 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -4,12 +4,12 @@ # Space Complexity: ? def newman_conway(num) sequence = [] - n = num - while n > 0 + n = 1 + while n <= num sequence.push(get_newman_num(n)) - n -= 1 + n += 1 end - return sequence + return sequence.join(" ") end def get_newman_num(num) @@ -22,4 +22,5 @@ def get_newman_num(num) end end -# puts newman_conway(20) \ No newline at end of file +# puts newman_conway(20) + From fd3b48baf9b36fd7d315a95064a0ec906201f3c6 Mon Sep 17 00:00:00 2001 From: Nidhi Date: Sat, 5 Oct 2019 12:18:01 -0700 Subject: [PATCH 3/4] finished newman conway --- lib/max_subarray.rb | 33 ++++++++++++++++++++++++++++----- lib/newman_conway.rb | 10 +++------- test/max_sub_array_test.rb | 2 +- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..c775275 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,31 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(1) def max_sub_array(nums) - return 0 if nums == nil - - raise NotImplementedError, "Method not implemented yet!" + max_so_far = 0 + max_ending_here = 0 + i = 1 + while i < nums.length + max_ending_here = max_ending_here + nums[i] + if max_so_far < max_ending_here + max_so_far = max_ending_here + end + if max_ending_here < 0 + max_ending_here = 0 + end + i += 1 + 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 \ No newline at end of file diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 9f79828..38bbdde 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,8 +1,7 @@ - - -# Time complexity: ? O(n) -# Space Complexity: ? +# Time complexity: O(n) +# Space Complexity: O(n) def newman_conway(num) + raise ArgumentError if num < 1 sequence = [] n = 1 while n <= num @@ -21,6 +20,3 @@ def get_newman_num(num) return num end end - -# puts newman_conway(20) - 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 8f770327ae040b7fef9802f65bd84da331b4dc2f Mon Sep 17 00:00:00 2001 From: Nidhi Date: Sat, 5 Oct 2019 12:19:27 -0700 Subject: [PATCH 4/4] finished max sub array --- lib/max_subarray.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index c775275..f78242d 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -2,8 +2,8 @@ # Time Complexity: O(n) # Space Complexity: O(1) def max_sub_array(nums) - max_so_far = 0 - max_ending_here = 0 + max_so_far = nums[0] + max_ending_here = nums[0] i = 1 while i < nums.length max_ending_here = max_ending_here + nums[i]