Skip to content

Kadane's Algorithm #11840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions data_structures/arrays/KadaneAlgo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def maximumSubarraySum(arr):

Check failure on line 1 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

data_structures/arrays/KadaneAlgo.py:1:1: N999 Invalid module name: 'KadaneAlgo'

Check failure on line 1 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

data_structures/arrays/KadaneAlgo.py:1:5: N802 Function name `maximumSubarraySum` should be lowercase

Check failure on line 1 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

data_structures/arrays/KadaneAlgo.py:1:1: N999 Invalid module name: 'KadaneAlgo'

Check failure on line 1 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

data_structures/arrays/KadaneAlgo.py:1:5: N802 Function name `maximumSubarraySum` should be lowercase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: maximumSubarraySum

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/KadaneAlgo.py, please provide doctest for the function maximumSubarraySum

Please provide return type hint for the function: maximumSubarraySum. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: arr

n = len(arr)
maxSum = -1e8

Check failure on line 3 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/KadaneAlgo.py:3:5: N806 Variable `maxSum` in function should be lowercase

Check failure on line 3 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/KadaneAlgo.py:3:5: N806 Variable `maxSum` in function should be lowercase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: maxSum

currSum = 0

Check failure on line 4 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/KadaneAlgo.py:4:5: N806 Variable `currSum` in function should be lowercase

Check failure on line 4 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/KadaneAlgo.py:4:5: N806 Variable `currSum` in function should be lowercase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: currSum


for i in range(0, n):

Check failure on line 6 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PIE808)

data_structures/arrays/KadaneAlgo.py:6:20: PIE808 Unnecessary `start` argument in `range`

Check failure on line 6 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PIE808)

data_structures/arrays/KadaneAlgo.py:6:20: PIE808 Unnecessary `start` argument in `range`
currSum = currSum + arr[i]

Check failure on line 7 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/KadaneAlgo.py:7:9: N806 Variable `currSum` in function should be lowercase

Check failure on line 7 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/KadaneAlgo.py:7:9: N806 Variable `currSum` in function should be lowercase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: currSum

if currSum > maxSum:
maxSum = currSum

Check failure on line 9 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

data_structures/arrays/KadaneAlgo.py:8:9: PLR1730 Replace `if` statement with `maxSum = max(currSum, maxSum)`

Check failure on line 9 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/KadaneAlgo.py:9:13: N806 Variable `maxSum` in function should be lowercase

Check failure on line 9 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

data_structures/arrays/KadaneAlgo.py:8:9: PLR1730 Replace `if` statement with `maxSum = max(currSum, maxSum)`

Check failure on line 9 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/KadaneAlgo.py:9:13: N806 Variable `maxSum` in function should be lowercase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: maxSum

if currSum < 0:
currSum = 0

Check failure on line 11 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

data_structures/arrays/KadaneAlgo.py:10:9: PLR1730 Replace `if` statement with `currSum = max(currSum, 0)`

Check failure on line 11 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/KadaneAlgo.py:11:13: N806 Variable `currSum` in function should be lowercase

Check failure on line 11 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

data_structures/arrays/KadaneAlgo.py:10:9: PLR1730 Replace `if` statement with `currSum = max(currSum, 0)`

Check failure on line 11 in data_structures/arrays/KadaneAlgo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

data_structures/arrays/KadaneAlgo.py:11:13: N806 Variable `currSum` in function should be lowercase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: currSum


return maxSum


if __name__ == "__main__":
# Your code goes here
arr = [1, 2, -54, 34, 22, 55, -22] # input your array here
print(maximumSubarraySum(arr))
Loading