From e2b5726fe73a32b65101a0293a5772b51597d659 Mon Sep 17 00:00:00 2001 From: Rachael Date: Thu, 9 Jun 2022 18:05:32 -0400 Subject: [PATCH 1/3] Passes newman_conway tests --- lib/newman_conway.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..aa924c5 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,15 @@ - - -# Time complexity: ? -# Space Complexity: ? def newman_conway(num): """ Returns a list of the Newman Conway numbers for the given value. - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(n) """ - pass + if num <= 0: + raise ValueError + elif num == 1: + return '1' + else: + memo = [0, 1, 1] + for i in range(3, num+1): + next_num = memo[memo[-1]] + memo[i-memo[i-1]] + memo.append(next_num) + return ' '.join([str(i) for i in memo if i]) From 0298bf5b096c45f24033136db819c03ab131bf1b Mon Sep 17 00:00:00 2001 From: Rachael Date: Thu, 9 Jun 2022 18:13:46 -0400 Subject: [PATCH 2/3] Passes all max_subarray tests --- lib/max_subarray.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..33671f3 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,21 @@ def max_sub_array(nums): """ Returns the max subarray of the given list of numbers. Returns 0 if nums is None or an empty list. - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(1) """ - if nums == None: - return 0 - if len(nums) == 0: - return 0 - pass + if not nums: + return 0 + + max_so_far = 0 + max_ending_here = 0 + + for num in nums: + max_ending_here = max_ending_here + num + if max_ending_here < 0: + max_ending_here = 0 + if max_so_far < max_ending_here: + max_so_far = max_ending_here + + return max(nums) if max_so_far == 0 else max_so_far + From 19d71fe619ead61218ee29da15698f0d996b495c Mon Sep 17 00:00:00 2001 From: Rachael Date: Thu, 9 Jun 2022 18:22:25 -0400 Subject: [PATCH 3/3] Fixes accidental deletion of edge case guard clause in max_sub_array --- lib/max_subarray.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 33671f3..994ec56 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -7,6 +7,8 @@ def max_sub_array(nums): """ if not nums: return 0 + if not len(nums): + return 0 max_so_far = 0 max_ending_here = 0