From dc9fbf7e360cf73c82198dddff477e2eb990814e Mon Sep 17 00:00:00 2001 From: Emily Colon Date: Mon, 18 Jul 2022 19:15:28 -0400 Subject: [PATCH] completed dynamic programming --- .vscode/settings.json | 7 +++++++ lib/max_subarray.py | 12 +++++++++++- lib/newman_conway.py | 22 +++++++++++++++++++++- 3 files changed, 39 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/max_subarray.py b/lib/max_subarray.py index 4e892e0..dc402b5 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -7,6 +7,16 @@ def max_sub_array(nums): """ if nums == None: return 0 + if len(nums) == 0: return 0 - pass + + max_til_now = nums[0] + max_ending = 0 + + for i in range(len(nums)): + max_ending += nums[i] + max_til_now = max(max_til_now, max_ending) + if(max_ending < 0): + max_ending = 0 + return max_til_now diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..96d224c 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -6,5 +6,25 @@ def newman_conway(num): """ Returns a list of the Newman Conway numbers for the given value. Time Complexity: ? Space Complexity: ? + + P(n) = P(P(n - 1)) + P(n - P(n - 1)) """ - pass + # base = ['1','1'] + if num == 0: + raise ValueError() + if num == 1: + return('1') + # for i in range(num): + # if i > 1: + # base.append(str(int(base[int(base[i - 1])]) + int(base[i - int(base[i - 1])]))) + + # return ' '.join(base) + + f = [0, 1, 1] + r = 1 + # To store values of sequence in array + for i in range(3, num + 1): + r = f[f[i-1]]+f[i-f[i-1]] + f.append(r) + + return ' '.join([str(j) for j in f[1:]]) \ No newline at end of file