From 5b6bda336e677cec9e46ca30d30737609652d9ed Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Tue, 7 Jun 2022 19:44:34 -0700 Subject: [PATCH] all tests passing --- .vscode/settings.json | 7 +++++++ README.md | 3 ++- lib/max_subarray.py | 17 ++++++++++++++--- lib/newman_conway.py | 40 +++++++++++++++++++++++++++++++++++----- 4 files changed, 58 insertions(+), 9 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/README.md b/README.md index 2fd88dc..8800180 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,8 @@ for all n > 2 P(n) = P(P(n - 1)) + P(n - P(n - 1)) ``` -Given a number n then print n terms of Newman-Conway Sequence +For a given a number: n +print n terms of Newman-Conway Sequence Examples: diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..82f0bcd 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,22 @@ 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 + if len(nums) == 1: + return nums[0] + max_sum = nums[0] + current_sum = nums[0] + for i in range(1, len(nums)): + if current_sum + nums[i] > nums[i]: + current_sum += nums[i] + else: + current_sum = nums[i] + if current_sum > max_sum: + max_sum = current_sum + return max_sum \ No newline at end of file diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..27073a7 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,40 @@ + # # P(1) = 1 + # # P(2) = 1 + # # for all n > 2 + # # P(n) = P(P(n - 1)) + P(n - P(n - 1)) + + # for i in range(2, num + 1): + # new_nc_num = nc_nums[nc_nums[i - 1]] + nc_nums[i - nc_nums[i - 1]] + # nc_nums.append(new_nc_num) + + + # string_list = [] + # for n in nc_nums: + # string_list.append(str(n)) + +def sequence(num): + if num == 1 or num == 2: + return 1 + else: + return sequence(sequence(num-1)) + sequence(num -sequence(num-1)) -# Time complexity: ? -# Space Complexity: ? def newman_conway(num): """ Returns a list of the Newman Conway numbers for the given value. - Time Complexity: ? - Space Complexity: ? + Time Comnewmanslexity: O(n) + Space Complexity: O(n) """ - pass + + nc_nums = [1, 1] + + if num < 1: + raise ValueError("Num must be greater than 0.") + + for i in range(2, num + 1): + nc_nums.append(nc_nums[nc_nums[i - 1] - 1] + nc_nums[i - nc_nums[i - 1]]) + + string_list = [] + for n in nc_nums: + string_list.append(str(n)) + + return " ".join(string_list[0:num]) \ No newline at end of file