From a97bfe25f31e8a2dee785e6e0849e2f370b2c9b1 Mon Sep 17 00:00:00 2001 From: Ana Gabriele Date: Sun, 19 Jun 2022 20:13:45 -0700 Subject: [PATCH 1/6] implement max_sub_array and pass tests --- lib/max_subarray.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..ad16f82 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -9,4 +9,13 @@ def max_sub_array(nums): return 0 if len(nums) == 0: return 0 - pass + # initialize maxSub -> first nsumber in the array + maxSub = nums[0] + currentSum = nums[0] + for index in range(1, len(nums)): + # start at index 1 + # compare current value with current sum + currentSum = max(nums[index], currentSum + nums[index]) + # assign to the max of itself compared to the currentSum + maxSub = max(maxSub, currentSum) + return maxSub From 7fd84879b62f34c3ec54ac1c2d4c06d69b361109 Mon Sep 17 00:00:00 2001 From: Ana Gabriele Date: Sun, 19 Jun 2022 21:33:57 -0700 Subject: [PATCH 2/6] implement newman_conway function and pass tests --- lib/newman_conway.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..bee9508 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -7,4 +7,37 @@ def newman_conway(num): Time Complexity: ? Space Complexity: ? """ - pass + # P(1) = 1 + # P(2) = 1 + # for all n > 2 + # P(n) = P(P(n - 1)) + P(n - P(n - 1)) + # use memoization table to store values fot each calcula item + if num == 0: + raise ValueError() + if num == 1: + return "1" + if num == 2: + return "1 1" + record = [0] * (num + 1) + record[0] = 0 + record[1] = 1 + result = [] + if num > 2: + record[2] = 1 + + i = 3 + # first 3 items + while i<=num: + record[i] + # P(n) = P(P(n - 1)) + P(n - P(n - 1)) + record[i] = record[record[i-1]] + record[i- record[i-1]] + i +=1 + + i = 1 + result = [] + while (i <= num) : + # Display the sequence element + result.append(record[i]) + i += 1 + resultOutput = [str(item) for item in result] + return " ".join(resultOutput) From d08abf0e7d01608f57563590306518be0921848f Mon Sep 17 00:00:00 2001 From: Ana Gabriele Date: Mon, 27 Jun 2022 17:32:19 -0700 Subject: [PATCH 3/6] add time complexity and 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 ad16f82..59d8e98 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 062862ef583d42fa1ddebdd34b726494cc8ddd60 Mon Sep 17 00:00:00 2001 From: Ana Gabriele Date: Mon, 27 Jun 2022 17:33:28 -0700 Subject: [PATCH 4/6] add time and time complexity --- lib/newman_conway.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index bee9508..b72eac6 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(1) """ # P(1) = 1 # P(2) = 1 From f8330490c383052635175d635d4408e278c2de14 Mon Sep 17 00:00:00 2001 From: Ana Gabriele Date: Mon, 27 Jun 2022 17:45:51 -0700 Subject: [PATCH 5/6] edit time and space complexity --- lib/newman_conway.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index b72eac6..14d6d63 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,17 +1,14 @@ - -# Time complexity: ? -# Space Complexity: ? def newman_conway(num): """ Returns a list of the Newman Conway numbers for the given value. Time Complexity: O(n) - Space Complexity: O(1) + Space Complexity: O(n) - it adds elements to result variav while (i <= num) """ # P(1) = 1 # P(2) = 1 # for all n > 2 # P(n) = P(P(n - 1)) + P(n - P(n - 1)) - # use memoization table to store values fot each calcula item + # use memoization table to store values for each calcula item if num == 0: raise ValueError() if num == 1: From a14ab9e4491c0af33e5e51efba6236a8a6d4ed91 Mon Sep 17 00:00:00 2001 From: Ana Gabriele Date: Mon, 27 Jun 2022 17:54:22 -0700 Subject: [PATCH 6/6] fix typo --- lib/newman_conway.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 14d6d63..6a7ee2e 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -2,7 +2,7 @@ def newman_conway(num): """ Returns a list of the Newman Conway numbers for the given value. Time Complexity: O(n) - Space Complexity: O(n) - it adds elements to result variav while (i <= num) + Space Complexity: O(n) - it adds elements to result variable while (i <= num) """ # P(1) = 1 # P(2) = 1