From 4d87ba88226d268f3316e479d6bf9543ffb5d944 Mon Sep 17 00:00:00 2001 From: evangeline spracklin Date: Thu, 21 Jul 2022 20:04:17 -0700 Subject: [PATCH 1/4] max contiguous array --- .vscode/settings.json | 7 +++++++ lib/max_subarray.py | 16 +++++++++++++--- lib/newman_conway.py | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 5 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..24cec20 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,21 @@ 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), iterates through relative to the size of the list given. + Space Complexity: ? O(1), we will only ever make 2 """ + #guard clauses if nums == None: return 0 if len(nums) == 0: return 0 - pass + + # initialize /memoize: + max_subarray_so_far = nums[0] + max_this_subarray = nums[0] + + for i in range(1, len(nums)): # O(n) + max_this_subarray = max(max_this_subarray + nums[i], nums[i]) + max_subarray_so_far = max(max_this_subarray, max_subarray_so_far) + + return max_subarray_so_far \ No newline at end of file diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..16f9525 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,4 +1,10 @@ - +''' +P(1) = 1 +P(2) = 1 +for all n > 2 +P(n) = P(P(n - 1)) + P(n - P(n - 1)) +''' +import array # Time complexity: ? # Space Complexity: ? @@ -7,4 +13,27 @@ def newman_conway(num): Time Complexity: ? Space Complexity: ? """ - pass + NCnums = [['i']] + +newman_conway(5) + +# ''' Python program to find the n-th element of +# Newman-Conway Sequence''' + +def sequence(n): + f = array.array('i', [0, 1, 1]) + +# # To store values of sequence in array + for i in range(3, n + 1): + r = f[f[i-1]]+f[i-f[i-1]] + f.append(r); + +# return r + +# # Driver code +# def main(): +# n = 10 +# print sequence(n) + +# if __name__ == '__main__': +# main() \ No newline at end of file From f24335ac60a039d41de5cb4f9c0af03391bf20c4 Mon Sep 17 00:00:00 2001 From: evangeline spracklin Date: Thu, 21 Jul 2022 20:10:33 -0700 Subject: [PATCH 2/4] base cases passing --- lib/newman_conway.py | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 16f9525..ca85853 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,7 +4,6 @@ for all n > 2 P(n) = P(P(n - 1)) + P(n - P(n - 1)) ''' -import array # Time complexity: ? # Space Complexity: ? @@ -13,27 +12,13 @@ def newman_conway(num): Time Complexity: ? Space Complexity: ? """ - NCnums = [['i']] + # base case guard clauses + if num < 1: + raise ValueError('newman conway number cant be zero nums long') + elif num == 1: + return '1' + elif num == 2: + return '1 1' -newman_conway(5) -# ''' Python program to find the n-th element of -# Newman-Conway Sequence''' - -def sequence(n): - f = array.array('i', [0, 1, 1]) - -# # To store values of sequence in array - for i in range(3, n + 1): - r = f[f[i-1]]+f[i-f[i-1]] - f.append(r); - -# return r - -# # Driver code -# def main(): -# n = 10 -# print sequence(n) - -# if __name__ == '__main__': -# main() \ No newline at end of file + NCnums = [['i']] \ No newline at end of file From 252beab6dfcd72a84a0b605a9a855fa3e52ccbeb Mon Sep 17 00:00:00 2001 From: evangeline spracklin Date: Thu, 21 Jul 2022 20:28:46 -0700 Subject: [PATCH 3/4] newman conwa 13 nad 20 --- lib/newman_conway.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index ca85853..75aea10 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -18,7 +18,22 @@ def newman_conway(num): elif num == 1: return '1' elif num == 2: + # print ('1 1') return '1 1' + memo = [0,1,1] + count = 3 - NCnums = [['i']] \ No newline at end of file + while count <= num: + # P(P(n - 1)) + P (n - P(n - 1)) + memo.append(memo[memo[count-1]] + memo[count - memo[count-1]]) + count += 1 + + NCnums = [] + for num in memo: + NCnums.append(str(num)) + # print(NCnums[1:]) + return " ".join(NCnums[1:]) + # return NCnums[1:] + +newman_conway(10) \ No newline at end of file From 6aa13554a581c46fee04b1c5afe71b4393e5822e Mon Sep 17 00:00:00 2001 From: evangeline spracklin Date: Thu, 21 Jul 2022 20:31:35 -0700 Subject: [PATCH 4/4] time and space complexity --- lib/newman_conway.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 75aea10..91e77b1 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,27 +4,21 @@ for all n > 2 P(n) = P(P(n - 1)) + P(n - P(n - 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 Complexity: ? O(n) + Space Complexity: ? O(n) """ # base case guard clauses if num < 1: raise ValueError('newman conway number cant be zero nums long') elif num == 1: return '1' - elif num == 2: - # print ('1 1') - return '1 1' memo = [0,1,1] count = 3 - - while count <= num: + + while count <= num: # P(P(n - 1)) + P (n - P(n - 1)) memo.append(memo[memo[count-1]] + memo[count - memo[count-1]]) count += 1 @@ -33,7 +27,4 @@ def newman_conway(num): for num in memo: NCnums.append(str(num)) # print(NCnums[1:]) - return " ".join(NCnums[1:]) - # return NCnums[1:] - -newman_conway(10) \ No newline at end of file + return " ".join(NCnums[1:]) \ No newline at end of file