From ecba7dd478cc3d08bc787134e165f6d84843f890 Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Tue, 31 Mar 2020 00:45:06 -0700 Subject: [PATCH 1/4] newman_conway --- lib/newman_conway.rb | 51 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..379b18e 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -2,6 +2,55 @@ # Time complexity: ? # Space Complexity: ? + + + +def newman_conway_helper(num) + if num == 0 + return 0 + elsif + num == 1 || num == 2 + return 1 + else + temp = num - 1 + return temp + end + +end + def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + + if num == 0 || num == nil + raise ArgumentError + elsif num == 1 + return "1" + elsif num == 2 + return "1 1" + end + + if num >= 3 + return_array = [1, 1] + end + + i = 3 + + until i > num do + + # P(n) = P(P(n - 1)) + P(n - P(n - 1)) + + temp_a = return_array[(return_array[i-2])-1] + + temp_b = return_array[(i-(return_array[i-2]))-1] + + temp = temp_a + temp_b + + i += 1 + + return_array.push temp + end + + return_string = return_array.join(" ") + + return return_string + end \ No newline at end of file From 4cbf94106c0b80ab5c7d6fd410b9f4f6c24c5271 Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Tue, 31 Mar 2020 00:47:57 -0700 Subject: [PATCH 2/4] newman_conway clean-up with complexities and removal of abandoned method --- lib/newman_conway.rb | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 379b18e..c5f4910 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,22 +1,9 @@ -# Time complexity: ? -# Space Complexity: ? - - - -def newman_conway_helper(num) - if num == 0 - return 0 - elsif - num == 1 || num == 2 - return 1 - else - temp = num - 1 - return temp - end - -end +# Time complexity: O(n) +## As the number goes to infinity, its values have to be assessed relative to whatever's already in the array, but because it's based on an array, the look-up cost is very low given that an index value is being passed in. +# Space Complexity: O(n) +## As the number goes to infinity, the amount of space will grow at the same rate def newman_conway(num) From 41dadb58c3c5fb33bce2ef66ab7b4734a7ca3cf1 Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Tue, 31 Mar 2020 01:20:03 -0700 Subject: [PATCH 3/4] max_subarray basic functionality --- lib/max_subarray.rb | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..3ef8603 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,34 @@ # Time Complexity: ? # Space Complexity: ? -def max_sub_array(nums) - return 0 if nums == nil +def max_sub_array(number_array) + length = number_array.length + if length == 0 + return nil + elsif length == 1 + return number_array[0] + end + + max_so_far = number_array[0] + max_ending_here = 0 + + i = 0 + + until i == length do + + max_ending_here = max_ending_here + number_array[i] + + if max_so_far < max_ending_here + max_so_far = max_ending_here + i += 1 + elsif max_ending_here < 0 + max_ending_here = 0 + i += 1 + else + i+= 1 + end + end + + return max_so_far - raise NotImplementedError, "Method not implemented yet!" end From 101bb67cd294fdf5c150f7308c9774198c0a4b88 Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Tue, 31 Mar 2020 01:21:37 -0700 Subject: [PATCH 4/4] max_subarray with complexities --- lib/max_subarray.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 3ef8603..c694158 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,6 +1,10 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +## The method iterates over the entire array but does so only once + +# Space Complexity: O(1) +## There are a limited number of variables that are constantly being re-assessed, so as the input goes to infitity the space requirements do not grow. + def max_sub_array(number_array) length = number_array.length if length == 0