From c873f8d2de7b06c4d1e864d60c1997e762e78e9f Mon Sep 17 00:00:00 2001 From: Angela Fan Date: Mon, 18 Jul 2022 10:53:10 -0700 Subject: [PATCH] all tests passing --- .vscode/settings.json | 7 +++++++ lib/max_subarray.py | 26 +++++++++++++++++++++++++- lib/newman_conway.py | 23 ++++++++++++++++++++--- 3 files changed, 52 insertions(+), 4 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..fdacd97 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -9,4 +9,28 @@ def max_sub_array(nums): return 0 if len(nums) == 0: return 0 - pass + if len(nums) == 1: + return nums[0] + + # Initialize the max_sum to the first element in the list. + max_sum = nums[0] + # Initialize the current_sum to the first element in the list. + current_sum = nums[0] + # Iterate through the list. + for i in range(1, len(nums)): + # If the current sum is greater than 0, add the next element to it. + if current_sum > 0: + current_sum += nums[i] + # If the current sum is less than 0, set it to the next element. + elif current_sum < 0: + current_sum = nums[i] + # If the current sum is 0, set it to the next element. + elif current_sum == 0: + current_sum = nums[i] + # If the current sum is greater than the max sum, set the max sum to it. + if current_sum > max_sum: + max_sum = current_sum + # Return the max sum. + return max_sum + + diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..9d6dcb5 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,7 +4,24 @@ # 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("Input must be greater than 0.") + + if num == 1: + return "1" + + if num == 2: + return "1 1" + + # Initialize the list of numbers. + numbers = [1, 1] + + for i in range(2, num + 1): + numbers.append(numbers[numbers[i - 1] - 1] + numbers[i - numbers[i - 1]]) + + # Return the list of numbers. + return " ".join(str(x) for x in numbers [0:num]) + \ No newline at end of file