From ad1e83386c1510220a237d577f695701b16376d8 Mon Sep 17 00:00:00 2001 From: Alie Ibarra Date: Thu, 14 Jul 2022 14:54:27 -0700 Subject: [PATCH 1/4] Pass Newman Conway Tests --- lib/newman_conway.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..ed9acb5 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,7 +4,20 @@ # 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 < 1: + raise ValueError("invalid num") + + if num == 1: + return "1" + + list = [0,1,1] + count = 3 + while num >= count: + list.append(list[list[count-1]]+list[count - list[count-1]]) + count += 1 + + answer = [str(item) for item in list] + return " ".join(answer[1:]) From 7f01e8511940010072e62b9196bdb61ce0dbdfcd Mon Sep 17 00:00:00 2001 From: Alie Ibarra Date: Thu, 14 Jul 2022 15:04:37 -0700 Subject: [PATCH 2/4] Pass Max Sub Array Tests --- lib/max_subarray.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..cdab3ac 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -5,8 +5,15 @@ def max_sub_array(nums): Time Complexity: ? Space Complexity: ? """ - if nums == None: + if nums == None or len(nums) == 0: return 0 - if len(nums) == 0: - return 0 - pass + + current_sub = 0 + max_sub = nums[0] + + for num in nums: + if current_sub < 0: + current_sub = 0 + current_sub += num + max_sub = max(max_sub, current_sub) + return max_sub From cf8506a068cb2e7429451eb70bc39e32ed913885 Mon Sep 17 00:00:00 2001 From: Alie Ibarra Date: Thu, 14 Jul 2022 15:06:16 -0700 Subject: [PATCH 3/4] Pytest config --- .vscode/settings.json | 7 +++++++ 1 file changed, 7 insertions(+) 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 From 035ef9c5583d434120390f53f650ea3845f2d51d Mon Sep 17 00:00:00 2001 From: Alie Ibarra Date: Thu, 14 Jul 2022 15:08:39 -0700 Subject: [PATCH 4/4] Refactor --- lib/max_subarray.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index cdab3ac..2a26792 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,8 +2,8 @@ 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 or len(nums) == 0: return 0