From f7200d614f031aa4e522ede1f9b8f580842e0c69 Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 17 Oct 2019 17:26:07 -0700 Subject: [PATCH 1/4] newman conway method --- lib/newman_conway.rb | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..5a44fd2 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,22 @@ +def newman_conway(num) +raise ArgumentError, "number must be greater than or equal to zero" if num <= 0 + return "1" if num == 1 + m = Array.new(num) + m[0] = 0 + m[1] = 1 + m[2] = 1 + output = "1" -# Time complexity: ? -# Space Complexity: ? -def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file + count = 3 + while count <= num + m[count] = m[m[count - 1]] + m[count - m[count - 1]] + count += 1 + end + + (2..num).each do |i| + output += "#{m[i]}" + end + + return output +end From 66140971ae29f8ba6dc1f614e9a4aa8ed88ed06e Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 17 Oct 2019 17:41:13 -0700 Subject: [PATCH 2/4] Update newman_conway.rb --- lib/newman_conway.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 5a44fd2..2cd7f43 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,14 +1,14 @@ def newman_conway(num) -raise ArgumentError, "number must be greater than or equal to zero" if num <= 0 +raise ArgumentError, "must be a natural number" if num <= 0 +return "1" if num == 1 - return "1" if num == 1 m = Array.new(num) m[0] = 0 m[1] = 1 m[2] = 1 output = "1" - count = 3 + while count <= num m[count] = m[m[count - 1]] + m[count - m[count - 1]] count += 1 @@ -17,6 +17,5 @@ def newman_conway(num) (2..num).each do |i| output += "#{m[i]}" end - return output end From fdacf03dd65f57af24fcf18125b4e3bf717a5d5c Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 17 Oct 2019 17:43:13 -0700 Subject: [PATCH 3/4] max_subarray dynamic programming --- lib/max_subarray.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..bad7d7e 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,15 @@ +def max(num1, num2) + return num1 > num2 ? num1 : num2 +end -# Time Complexity: ? -# Space Complexity: ? def max_sub_array(nums) return 0 if nums == nil - - raise NotImplementedError, "Method not implemented yet!" + + max_now = max = nums[0] + + (1...nums.length).each do |i| + max_now = nums[i] > max_now + nums[i] ? nums[i] : max_now + nums[i] + max = max_now if max_now > max + end + return max end From f2538cc0000fa07d9567ee43209959efbeb063e0 Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 17 Oct 2019 17:44:48 -0700 Subject: [PATCH 4/4] Update max_subarray.rb --- lib/max_subarray.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index bad7d7e..41c0b72 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,3 +1,5 @@ +# Four-space tabs courtesey of github's editor ... doing this one straight in the browser because of internet issues + def max(num1, num2) return num1 > num2 ? num1 : num2 end