From bba3a8451e78a3534a04d9e4e86d8b038e7cc3f9 Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Fri, 28 Oct 2022 01:20:57 +0530 Subject: [PATCH 1/8] Update maximum_subarray.py 1. Rectify documentation to indicate the correct output: function doesn't return the subarray, but rather returns a sum. 2. Make the function more Pythonic and optimal. 3. Make function annotation generic i.e. can accept any sequence. 4. Raise value error when the input sequence is empty. --- other/maximum_subarray.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/other/maximum_subarray.py b/other/maximum_subarray.py index 756e009444fe..4f8bdfca701c 100644 --- a/other/maximum_subarray.py +++ b/other/maximum_subarray.py @@ -1,20 +1,24 @@ -def max_subarray(nums: list[int]) -> int: - """ - Returns the subarray with maximum sum - >>> max_subarray([1,2,3,4,-2]) +from collections.abc import Sequence + + +def max_subarray_sum(nums: Sequence[int]) -> int: + """Returns the maximum possible sum amongst all non - empty subarrays + + Raises: + ValueError: when nums is empty. + + >>> max_subarray_sum([1,2,3,4,-2]) 10 - >>> max_subarray([-2,1,-3,4,-1,2,1,-5,4]) + >>> max_subarray_sum([-2,1,-3,4,-1,2,1,-5,4]) 6 """ + if not nums: + raise ValueError("Input sequence should not be empty") curr_max = ans = nums[0] - for i in range(1, len(nums)): - if curr_max >= 0: - curr_max = curr_max + nums[i] - else: - curr_max = nums[i] - + for num in nums[1:]: + curr_max = max(curr_max + num, num) ans = max(curr_max, ans) return ans @@ -23,4 +27,4 @@ def max_subarray(nums: list[int]) -> int: if __name__ == "__main__": n = int(input("Enter number of elements : ").strip()) array = list(map(int, input("\nEnter the numbers : ").strip().split()))[:n] - print(max_subarray(array)) + print(max_subarray_sum(array)) From 9f5c81a813d9dc59d252ad796efc083fb3c3a8e9 Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Fri, 28 Oct 2022 11:20:26 +0530 Subject: [PATCH 2/8] Update maximum_subarray.py 1. Use the conventions as mentioned in pep-0257. 2. Use negative infinity as the initial value for the current maximum and the answer. --- other/maximum_subarray.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/maximum_subarray.py b/other/maximum_subarray.py index 4f8bdfca701c..767eb35f523c 100644 --- a/other/maximum_subarray.py +++ b/other/maximum_subarray.py @@ -2,7 +2,7 @@ def max_subarray_sum(nums: Sequence[int]) -> int: - """Returns the maximum possible sum amongst all non - empty subarrays + """Return the maximum possible sum amongst all non - empty subarrays. Raises: ValueError: when nums is empty. @@ -15,9 +15,9 @@ def max_subarray_sum(nums: Sequence[int]) -> int: if not nums: raise ValueError("Input sequence should not be empty") - curr_max = ans = nums[0] + curr_max = ans = float("-inf") - for num in nums[1:]: + for num in nums: curr_max = max(curr_max + num, num) ans = max(curr_max, ans) From e39c09ef724f557898371bb8466b142d1d70c388 Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Fri, 28 Oct 2022 11:32:16 +0530 Subject: [PATCH 3/8] Update maximum_subarray.py Avoid type conflict by returning the answer cast to an integer. --- other/maximum_subarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/maximum_subarray.py b/other/maximum_subarray.py index 767eb35f523c..bd573dcdca1a 100644 --- a/other/maximum_subarray.py +++ b/other/maximum_subarray.py @@ -21,7 +21,7 @@ def max_subarray_sum(nums: Sequence[int]) -> int: curr_max = max(curr_max + num, num) ans = max(curr_max, ans) - return ans + return int(ans) if __name__ == "__main__": From 531153391be4f2a4b807e64fe535d2a2c20fc836 Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Fri, 28 Oct 2022 12:08:42 +0530 Subject: [PATCH 4/8] Update other/maximum_subarray.py Co-authored-by: Andrey --- other/maximum_subarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/maximum_subarray.py b/other/maximum_subarray.py index bd573dcdca1a..5ce02f3714b6 100644 --- a/other/maximum_subarray.py +++ b/other/maximum_subarray.py @@ -1,4 +1,4 @@ -from collections.abc import Sequence +from typing import Sequence def max_subarray_sum(nums: Sequence[int]) -> int: From 8ba65080a63d88c400b4318d7b32858513f75441 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Oct 2022 06:39:31 +0000 Subject: [PATCH 5/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/maximum_subarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/maximum_subarray.py b/other/maximum_subarray.py index 5ce02f3714b6..bd573dcdca1a 100644 --- a/other/maximum_subarray.py +++ b/other/maximum_subarray.py @@ -1,4 +1,4 @@ -from typing import Sequence +from collections.abc import Sequence def max_subarray_sum(nums: Sequence[int]) -> int: From 8b11df9cbbe0a39eb2447aa142112903fc1f6b88 Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Fri, 28 Oct 2022 12:18:43 +0530 Subject: [PATCH 6/8] Update maximum_subarray.py --- other/maximum_subarray.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/other/maximum_subarray.py b/other/maximum_subarray.py index bd573dcdca1a..73f872a18225 100644 --- a/other/maximum_subarray.py +++ b/other/maximum_subarray.py @@ -15,9 +15,11 @@ def max_subarray_sum(nums: Sequence[int]) -> int: if not nums: raise ValueError("Input sequence should not be empty") - curr_max = ans = float("-inf") - - for num in nums: + curr_max = ans = nums[0] + nums_len = len(nums) + + for i in range(1, nums_len): + num = nums[i] curr_max = max(curr_max + num, num) ans = max(curr_max, ans) From 9ac4b932b51af8c4c27af3b6b04ad89bcf2e08d5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Oct 2022 06:51:03 +0000 Subject: [PATCH 7/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/maximum_subarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/maximum_subarray.py b/other/maximum_subarray.py index 73f872a18225..d06eb69bec43 100644 --- a/other/maximum_subarray.py +++ b/other/maximum_subarray.py @@ -17,7 +17,7 @@ def max_subarray_sum(nums: Sequence[int]) -> int: curr_max = ans = nums[0] nums_len = len(nums) - + for i in range(1, nums_len): num = nums[i] curr_max = max(curr_max + num, num) From 310d3891a24031ad8ca038143242520fcd58f66d Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Fri, 28 Oct 2022 21:39:54 +0530 Subject: [PATCH 8/8] Update maximum_subarray.py Remove typecast to int for the final answer --- other/maximum_subarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/maximum_subarray.py b/other/maximum_subarray.py index d06eb69bec43..1c8c8cabcd2d 100644 --- a/other/maximum_subarray.py +++ b/other/maximum_subarray.py @@ -23,7 +23,7 @@ def max_subarray_sum(nums: Sequence[int]) -> int: curr_max = max(curr_max + num, num) ans = max(curr_max, ans) - return int(ans) + return ans if __name__ == "__main__":