From 807cfed1fe26b7383a18f55dcece4ac25def314d Mon Sep 17 00:00:00 2001 From: Courtney Date: Sat, 8 Jan 2022 13:24:21 -0800 Subject: [PATCH 1/4] testing to see if push works --- lib/max_subarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..ecd0376 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -9,4 +9,4 @@ def max_sub_array(nums): return 0 if len(nums) == 0: return 0 - pass + From e11904b6cf405d463fe71b25d82d37d2db029706 Mon Sep 17 00:00:00 2001 From: Courtney Date: Sat, 8 Jan 2022 13:42:35 -0800 Subject: [PATCH 2/4] passing nc tests --- .vscode/settings.json | 7 +++++++ lib/newman_conway.py | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 2 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/newman_conway.py b/lib/newman_conway.py index 70a3353..12827a9 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -3,8 +3,22 @@ # Time complexity: ? # Space Complexity: ? def newman_conway(num): - """ Returns a list of the Newman Conway numbers for the given value. + """ Returns a string of the Newman Conway numbers for the given value. Time Complexity: ? Space Complexity: ? """ - pass + if num == 0: + raise ValueError + + if num == 1: + return "1" + + seq = [0, 1, 1] + + for i in range(3, num + 1): + next_num = seq[seq[i-1]] + seq[i - seq[i-1]] + seq.append(next_num) + + return ' '.join([str(n) for n in seq[1:num+1]]) + +print(newman_conway(9)) From 647338bd92701ec3b07ab2ee9808d0698adc2f7c Mon Sep 17 00:00:00 2001 From: Courtney Date: Sat, 8 Jan 2022 13:44:15 -0800 Subject: [PATCH 3/4] adds time and space complexity for nc --- lib/newman_conway.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 12827a9..d17dbe0 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,11 +1,11 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: O(n) +# Space Complexity: O(n) def newman_conway(num): """ Returns a string of the Newman Conway numbers for the given value. - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(n) """ if num == 0: raise ValueError @@ -20,5 +20,3 @@ def newman_conway(num): seq.append(next_num) return ' '.join([str(n) for n in seq[1:num+1]]) - -print(newman_conway(9)) From 990fe2b3d5138038d4685263076ac5733b15dd83 Mon Sep 17 00:00:00 2001 From: Courtney Date: Sat, 8 Jan 2022 14:01:55 -0800 Subject: [PATCH 4/4] passing tests for max_subarray --- lib/max_subarray.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index ecd0376..24f802b 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,23 @@ 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 + + max_so_far = 0 + max_ending_here = 0 + for num in nums: + max_ending_here = max_ending_here + num + if max_so_far < max_ending_here: + max_so_far = max_ending_here + + if max_so_far <= 0: + return max(nums) + + return max_so_far \ No newline at end of file