From 40d4ab7c422c050daedaf03e9a9767407b09fcd6 Mon Sep 17 00:00:00 2001 From: Lilly Date: Sun, 10 Jul 2022 13:09:24 -0700 Subject: [PATCH] Lilly Pine C16 --- .vscode/settings.json | 7 +++++++ lib/max_subarray.py | 16 +++++++++++++--- lib/newman_conway.py | 27 +++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3e99ede --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "." + ], + "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..fe0b930 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 + n = len(nums) + max_sum = -10000000000 + curr_sum = 0 + + for i in range(n): + curr_sum += nums[i] + if curr_sum > max_sum: + max_sum = curr_sum + if curr_sum < 0: + curr_sum = 0 + return max_sum diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..0f8dbc9 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -2,9 +2,28 @@ # Time complexity: ? # Space Complexity: ? -def newman_conway(num): +def newman_conway(nums): """ Returns a list of the Newman Conway numbers for the given value. - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(n^2) """ - pass + if nums == 0: + raise ValueError + + if nums == 1: + return "1" + + if nums == 2: + return "1 1" + + seq = "" + start_list = [0, 1, 1] + for i in range(3,nums+1): + start_list.append( start_list[start_list[i - 1]] + start_list[i - start_list[i - 1]]) + + for num in start_list[1:]: + seq += str(num) + " " + return seq.strip() + + +