From 640851aefd4de11cdc6e15b708dcdc6952468279 Mon Sep 17 00:00:00 2001 From: Lux Barker Date: Thu, 12 May 2022 17:03:08 -0700 Subject: [PATCH 1/3] Implement newman-conway --- lib/newman_conway.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..7f94103 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,21 @@ - - # Time complexity: ? # Space Complexity: ? def newman_conway(num): - """ Returns a list of the Newman Conway numbers for the given value. - Time Complexity: ? - Space Complexity: ? + """Returns a list of the Newman Conway numbers for the given value. + Time Complexity: ? + Space Complexity: ? """ - pass + if num <= 0: + raise ValueError + + if num == 1: + return "1" + + nums = [0, 1, 1] + + i = 3 + while i <= num: + nums.append(nums[nums[i - 1]] + nums[i - nums[i - 1]]) + i += 1 + + return " ".join([str(n) for n in nums[1:]]) From 68ccfb7a9aa75373bcdfb089ac2cc4c11106c0e2 Mon Sep 17 00:00:00 2001 From: Lux Barker Date: Thu, 7 Jul 2022 17:13:30 -0700 Subject: [PATCH 2/3] implement max_subarray --- lib/max_subarray.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..f03eaf2 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,18 @@ 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) where n is len(nums) + Space Complexity: O(1) """ if nums == None: return 0 if len(nums) == 0: return 0 - pass + + max_so_far = nums[0] + curr_max = nums[0] + for i in range(1, len(nums)): + curr_max = max(curr_max + nums[i], nums[i]) + max_so_far = max(max_so_far, curr_max) + return max_so_far + From 1f48bf572dccd471b89fa2f1a73704dcccfff547 Mon Sep 17 00:00:00 2001 From: Lux Barker Date: Thu, 7 Jul 2022 17:14:45 -0700 Subject: [PATCH 3/3] add time and space complexity for newman_conway --- lib/newman_conway.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 7f94103..9dff9b9 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -2,8 +2,8 @@ # 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) where n is num + Space Complexity: O(n) where n is num """ if num <= 0: raise ValueError