From de60027ab716744dfe595623430ffb02ab201b1f Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Fri, 20 Mar 2020 17:42:50 -0700 Subject: [PATCH 1/5] Passing base case assertions. --- lib/newman_conway.rb | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..0c099f4 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -3,5 +3,36 @@ # Time complexity: ? # Space Complexity: ? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + raise ArgumentError, "Argument must be an integer greater than zero." if num <= 0 + + if num == 1 + return "1" + elsif num == 2 + return "1 1" + else + p_helper(num) + end +end + +def p_helper(num) + sequence = [1, 1] + + # when do we memo???? I guess we only need to keep track of previous_p and p_previous_p values + + # p(num) = p(p(num - 1)) + p(num - p(num - 1)) + + # need to find p of all whole integers preceding num and add them to sequence + # code below seems to work if I provide n and sequence for integers preceding n + build = num - 2 + build.times do + # if num is 4 previous_p is at index 2 (value: 2) + previous_p = sequence[num - 2] + # now we need to look up p of the previous_p value + p_previous_p = sequence[previous_p - 1] + # find p of number minus previous number's p, then add it to p_previous_p + p = p_previous_p + sequence[(num - previous_p) - 1] + sequence << p + end + + return sequence end \ No newline at end of file From 18401889b944a70b57ec6cfea8bcc6ee3568ca8a Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Fri, 20 Mar 2020 18:08:35 -0700 Subject: [PATCH 2/5] Passing all newman_conway tests which is very exciting because this is the first time I've coded a regressive function without ANY outside help. --- lib/newman_conway.rb | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 0c099f4..2c2cda2 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -10,29 +10,26 @@ def newman_conway(num) elsif num == 2 return "1 1" else - p_helper(num) + p_helper(num, [1, 1]) end end -def p_helper(num) - sequence = [1, 1] - - # when do we memo???? I guess we only need to keep track of previous_p and p_previous_p values - - # p(num) = p(p(num - 1)) + p(num - p(num - 1)) +def p_helper(num, sequence) # need to find p of all whole integers preceding num and add them to sequence - # code below seems to work if I provide n and sequence for integers preceding n - build = num - 2 - build.times do - # if num is 4 previous_p is at index 2 (value: 2) - previous_p = sequence[num - 2] - # now we need to look up p of the previous_p value - p_previous_p = sequence[previous_p - 1] - # find p of number minus previous number's p, then add it to p_previous_p - p = p_previous_p + sequence[(num - previous_p) - 1] - sequence << p - end - return sequence + # BASE CASE: stop when count of numbers in sequence array is the same as `num` + return sequence.join(" ") if num == sequence.length + + # REGRESSIVE CASE: + # forumula is p(num) = p(p(num - 1)) + p(num - p(num - 1)) + building_num = sequence.length + 1 + # if num is 4 previous_p is at index 2 (value: 2) + previous_p = sequence[building_num - 2] + # now we need to look up p of the previous_p value + p_previous_p = sequence[previous_p - 1] + # find p of number minus previous number's p, then add it to p_previous_p + p = p_previous_p + sequence[(building_num - previous_p) - 1] + sequence << p + return p_helper(num, sequence) end \ No newline at end of file From d798c7b110b25735a0297febcc15844cf8fa7fd2 Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Fri, 20 Mar 2020 18:14:07 -0700 Subject: [PATCH 3/5] Adding time/space complexity and tweaks to notes. --- lib/newman_conway.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 2c2cda2..9d4019d 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,7 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: O(n) because we need to perform our "what is the p?" steps n times +# Space Complexity: also O(n) because we are adding an element to the sequence array for every n def newman_conway(num) raise ArgumentError, "Argument must be an integer greater than zero." if num <= 0 @@ -16,13 +16,11 @@ def newman_conway(num) def p_helper(num, sequence) - # need to find p of all whole integers preceding num and add them to sequence - # BASE CASE: stop when count of numbers in sequence array is the same as `num` return sequence.join(" ") if num == sequence.length - # REGRESSIVE CASE: - # forumula is p(num) = p(p(num - 1)) + p(num - p(num - 1)) + # REGRESSIVE CASE: add p for next integer in sequence + # forumula: p(num) = p(p(num - 1)) + p(num - p(num - 1)) building_num = sequence.length + 1 # if num is 4 previous_p is at index 2 (value: 2) previous_p = sequence[building_num - 2] From 05a3097db18911d90e50f0d20de544f2a8c3936e Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Fri, 20 Mar 2020 18:16:39 -0700 Subject: [PATCH 4/5] Changing not-so-great variable name to more descriptive --- 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 9d4019d..403a677 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -21,13 +21,13 @@ def p_helper(num, sequence) # REGRESSIVE CASE: add p for next integer in sequence # forumula: p(num) = p(p(num - 1)) + p(num - p(num - 1)) - building_num = sequence.length + 1 + next_integer = sequence.length + 1 # if num is 4 previous_p is at index 2 (value: 2) - previous_p = sequence[building_num - 2] + previous_p = sequence[next_integer - 2] # now we need to look up p of the previous_p value p_previous_p = sequence[previous_p - 1] # find p of number minus previous number's p, then add it to p_previous_p - p = p_previous_p + sequence[(building_num - previous_p) - 1] + p = p_previous_p + sequence[(next_integer - previous_p) - 1] sequence << p return p_helper(num, sequence) end \ No newline at end of file From e3420dbcae57a9a290ab17f112cf396f5ce544b0 Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Tue, 31 Mar 2020 18:32:57 -0700 Subject: [PATCH 5/5] Passing max subarray tests with time/space complexity added. --- lib/max_subarray.rb | 26 ++++++++++++++++++++++---- test/max_sub_array_test.rb | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..1618086 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,26 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) because we have do our if/else operations on every element in the array +# Space Complexity: O(1) because we are re-using the same variable def max_sub_array(nums) return 0 if nums == nil - - raise NotImplementedError, "Method not implemented yet!" + return nil if nums.empty? + return nums[0] if nums.length == 1 + + max_so_far = max_ending_here = nums[0] + + nums[1..-1].each do |number| + + if max_ending_here + number < number + max_ending_here = number + else + max_ending_here += number + end + + if max_so_far < max_ending_here + max_so_far = max_ending_here + end + end + + return max_so_far + 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]