From 7578028e79632ba2fcc0bfb7312aa3793623c9d2 Mon Sep 17 00:00:00 2001 From: Andrea Palacios Date: Tue, 25 Jan 2022 22:41:55 -0800 Subject: [PATCH 1/3] max subarray passing --- lib/max_subarray.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..639126e 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -1,4 +1,7 @@ +from re import M + + 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. @@ -7,6 +10,18 @@ def max_sub_array(nums): """ if nums == None: return 0 - if len(nums) == 0: + if len(nums)==0: return 0 - pass + + + max_so_far = nums[0] + max_ending_here = nums[0] + + for i in range(1,len(nums)): + max_ending_here = max_ending_here + nums[i] + if(max_ending_here < 0): + max_ending_here = max(max_ending_here,nums[i]) + if(max_so_far < max_ending_here): + max_so_far = max_ending_here + + return max_so_far From 8a2036bb1e408f17b1297539399ab39933aff351 Mon Sep 17 00:00:00 2001 From: Andrea Palacios Date: Tue, 25 Jan 2022 23:17:05 -0800 Subject: [PATCH 2/3] newman conway tests passing/ big-o added --- lib/max_subarray.py | 5 +++-- lib/newman_conway.py | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 639126e..8ced63a 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -5,8 +5,9 @@ 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(n) + """ if nums == None: return 0 diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..0c22976 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,19 @@ -# Time complexity: ? -# Space Complexity: ? +# Time comnewman_conwaylexity: ? +# Snewman_conwayace Comnewman_conwaylexity: ? 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 < 1: + raise ValueError + elif num == 1: + return "1" + + sequence = [0,1,1] + + for i in range(3, num+1): + sequence.append(sequence[sequence[i - 1]] + sequence[i - sequence[i - 1]]) + return (" ".join(map(str, sequence[1:]))) \ No newline at end of file From 380df8b7c5628db4fba7b0c2671c3d5221479862 Mon Sep 17 00:00:00 2001 From: Andrea Palacios Date: Tue, 25 Jan 2022 23:27:31 -0800 Subject: [PATCH 3/3] fixed spelling error --- lib/max_subarray.py | 16 +++++++--------- lib/newman_conway.py | 11 +++++------ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 8ced63a..af760bb 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -1,20 +1,18 @@ - -from re import M +#Time Complexity: O(n) +#Space Complexity: O(n) 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: O(n) - Space Complexity: O(n) - - """ + ''' + Time Complexity: O(n) + Space Complexity: O(n) + + ''' if nums == None: return 0 if len(nums)==0: return 0 - max_so_far = nums[0] max_ending_here = nums[0] diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 0c22976..01174ac 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,12 +1,11 @@ -# Time comnewman_conwaylexity: ? -# Snewman_conwayace Comnewman_conwaylexity: ? def newman_conway(num): - """ Returns a list of the Newman Conway numbers for the given value. - Time Complexity: O(n) - Space Complexity: O(n) - """ + ''' + Time Complexity: O(n) + Space Complexity: O(n) + + ''' if num < 1: raise ValueError elif num == 1: