From ad562f7de0bcffdb1fbfe59b3e41b1498061a3ba Mon Sep 17 00:00:00 2001 From: Faith Date: Sat, 16 Jul 2022 18:28:10 -0700 Subject: [PATCH] all tests passing --- .vscode/settings.json | 7 +++++++ lib/max_subarray.py | 14 ++++++++++++-- lib/newman_conway.py | 29 ++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9b38853 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..c0d9e7b 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 + + max_sum = nums[0] + current_sum = nums[0] + for i in range(1, len(nums)): + if current_sum < 0: + current_sum = 0 + current_sum += nums[i] + if current_sum > max_sum: + max_sum = current_sum + return max_sum \ No newline at end of file diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..fe0f2ad 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,7 +4,30 @@ # 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 + def newman_helper(cur_num, prev_seq = 1): + if cur_num > num: + return + if cur_num == 1 or cur_num == 2: + newman_nums[cur_num] = 1 + newman_helper(cur_num + 1) + else: + cur_seq = newman_nums[prev_seq] + newman_nums[cur_num - prev_seq] + newman_nums[cur_num] = cur_seq + newman_helper(cur_num + 1, cur_seq) + + if num <= 0: + raise ValueError + + newman_nums = [None] * (num + 1) + + newman_helper(1) + + newman_nums = newman_nums[1:] + final_string = f'{newman_nums.pop(0)}' + for num in newman_nums: + final_string += ' ' + str(num) + + return final_string