From 54649603daea90d8ea97f7d80de2c031e98736c1 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 7 Oct 2019 19:26:19 -0700 Subject: [PATCH 1/3] wrote method for newman_conway --- lib/newman_conway.rb | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..9fc15ae 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,26 @@ - - # Time complexity: ? # Space Complexity: ? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + if num <= 0 + raise ArgumentError, "Must pass in integer greater than zero, received: #{num}" + end + + hash = {1 => 1, 2 => 1} + result = [] + count = 1 + + while count <= num + if count == 1 || count == 2 + result.push(1) + else + last = hash[count - 1] + current = hash[last] + hash[count - last] + hash[count] = current + result.push(current) + end + + count += 1 + end + + return result.join(" ") end \ No newline at end of file From 2bd6c6e45d3d01af72a2d44cb60f0d86da5a2878 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 7 Oct 2019 20:06:59 -0700 Subject: [PATCH 2/3] max sub array method completed --- lib/max_subarray.rb | 17 +++++++++++++++-- test/max_sub_array_test.rb | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..bb64091 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -2,7 +2,20 @@ # 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 = nums[0] + current_max = 0 + count = 0 + + while count < nums.length + current_max = current_max + nums[count] + + max = current_max if max < current_max + current_max = 0 if current_max < 0 + + count += 1 + end + + return max end 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 cd956e1eae40d5f98b464b700c3854a66635ab0b Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Tue, 8 Oct 2019 09:09:49 -0700 Subject: [PATCH 3/3] added space and time complexities --- lib/max_subarray.rb | 4 ++-- lib/newman_conway.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index bb64091..619a48d 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,6 +1,6 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n), where n is the number of items in nums +# Space Complexity: O(1) def max_sub_array(nums) return 0 if nums == nil diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 9fc15ae..b9475a4 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,5 +1,5 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: O(n), where n is directly proportional to the size of num +# Space Complexity: O(n), where n is directly proportional to the size of num def newman_conway(num) if num <= 0 raise ArgumentError, "Must pass in integer greater than zero, received: #{num}"