From 216a4d6d36fb6c8b4e743ad6c454cce087e0c55b Mon Sep 17 00:00:00 2001 From: Ivette Fernandez Date: Wed, 6 Jul 2022 12:03:40 -0400 Subject: [PATCH 1/2] completed newman conway doo hicky lol lets fffff go --- .vscode/settings.json | 7 +++++++ lib/newman_conway.py | 26 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) 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..8531a13 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -6,5 +6,29 @@ def newman_conway(num): """ Returns a list of the Newman Conway numbers for the given value. Time Complexity: ? Space Complexity: ? + + P(1) = 1 + P(2) = 1 + for all n > 2 + P(n) = P(P(n - 1)) + P(n - P(n - 1)) """ - pass + + # make a dict with key as n, and the value as P(n) (aka the output) + + outputs = {} + + res = [] + if num <= 0: + raise ValueError + + for n in range(1, num + 1): + if n == 1 or n == 2: + outputs[n] = 1 + res.append("1") + continue + # P(n -1) -> outputs[ n - 1] + value = outputs[outputs[n - 1]] + outputs[n - outputs[n - 1]] + outputs[n] = value + res.append(str(value)) + + return " ".join(res) From 8ea58b85a87e8c33d3592670a25eddeaf8c03418 Mon Sep 17 00:00:00 2001 From: Ivette Fernandez Date: Thu, 7 Jul 2022 15:25:39 -0400 Subject: [PATCH 2/2] add complexity analysis and finish max sub array --- lib/max_subarray.py | 19 ++++++++++++++++--- lib/newman_conway.py | 4 ++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..e1a9f24 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -1,12 +1,25 @@ +from json.encoder import INFINITY + + 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 + + maximum = - INFINITY + current_max = 0 + + for num in nums: + current_max += num + if current_max > maximum: + maximum = current_max + if current_max < 0: + current_max = 0 + return maximum diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 8531a13..20e92be 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,8 +4,8 @@ # 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) P(1) = 1 P(2) = 1