From d1212324d7448fa75fd8991328a7c42f8912d123 Mon Sep 17 00:00:00 2001 From: Khandice Date: Tue, 7 Jun 2022 13:58:50 -0700 Subject: [PATCH 1/4] max_sub_array tests passing --- lib/max_subarray.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..9953cd0 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -9,4 +9,12 @@ def max_sub_array(nums): return 0 if len(nums) == 0: return 0 - pass + + final_max = nums[0] + temp_max = nums[0] + + for i in range(1, len(nums)): + temp_max = max(temp_max + nums[i], nums[i]) + if temp_max > final_max: + final_max = temp_max + return final_max From 62a279877a7e1f2a802eab02bf4b89fb43271409 Mon Sep 17 00:00:00 2001 From: Khandice Date: Tue, 7 Jun 2022 14:01:19 -0700 Subject: [PATCH 2/4] add time/space complexity --- 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 9953cd0..b166015 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: return 0 From 3d8320209d94900f7d4a81562822e0ccb8b95655 Mon Sep 17 00:00:00 2001 From: Khandice Date: Wed, 8 Jun 2022 08:23:44 -0700 Subject: [PATCH 3/4] all tests passing --- lib/newman_conway.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..104b0fe 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -7,4 +7,29 @@ def newman_conway(num): Time Complexity: ? Space Complexity: ? """ - pass + + # p1 = 1 + # p2 = 1 + # for all num > 2 + # P(n) = P(P(n - 1)) + P(n - P(n - 1)) + + if num <= 0: + raise ValueError("Number must be greater than 0!") + + if num == 1: + return "1" + + newman_seq = [None] * (num) + newman_seq[0] = 1 + newman_seq[1] = 1 + + n = 2 + while n < num: + newman_seq[n] = newman_seq[newman_seq[n -1 ] - 1] + newman_seq[n-newman_seq[n - 1]] + n += 1 + + output_seq = " ".join(map(str, newman_seq)) + return output_seq + + + From 04f58573cd6b580c8978873f086c950fef4459d3 Mon Sep 17 00:00:00 2001 From: Khandice Date: Wed, 8 Jun 2022 08:24:03 -0700 Subject: [PATCH 4/4] extras --- .vscode/settings.json | 7 +++++++ package-lock.json | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 package-lock.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/package-lock.json b/package-lock.json new file mode 100644 index 0000000..7ae438f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "dynamic-programming", + "lockfileVersion": 2, + "requires": true, + "packages": {} +}