-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Enhancement/issue 8427 #8811
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
elsayed2440
wants to merge
16
commits into
TheAlgorithms:master
from
linushenkel:enhancement/issue-8427
Closed
Enhancement/issue 8427 #8811
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
37f5771
Create TestShiva
ShivaDahal99 baf8d49
Delete TestShiva
ShivaDahal99 99013f8
Merge branch 'TheAlgorithms:master' into master
jlhuhn e8c960a
Add the maximum subarray sum algorithm
0681a2c
Add the sliding window algorithm
bb8d992
Add the sliding window algorithm using generators
7010d78
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e5e1eb8
Pre-commit Changes
2bdff9b
Format Fixing
106c48a
format
124a0e2
rename files
8339368
Add black version
ba7576a
Correct file names
2323737
Delete black file
1e07a78
Add the sliding window algorithm
26eae99
Move the sliding window algorithms to other folder
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
def max_sub_array_sum(arr: list[int], size: int) -> int: | ||
""" | ||
Finds the maximum sum of a subarray within the given array using Kadane's algorithm. | ||
|
||
Args: | ||
arr (list): The input array of numbers. | ||
size (int): The size of the array. | ||
|
||
Returns: | ||
int: The maximum sum of a subarray within the array. | ||
|
||
Example: | ||
>>> arr = [-2, -3, 4, -1, -2, 5, -3] | ||
>>> max_sub_array_sum(arr, len(arr)) | ||
6 | ||
In this example, the input array is [-2, -3, 4, -1, -2, 5, -3]. | ||
The maximum sum of a subarray within this array is 6, | ||
which corresponds to the subarray [4, -1, -2, 5]. | ||
|
||
>>> arr = [-3, -4, 5, -1, 2, -4, 6, -1] | ||
>>> max_sub_array_sum(arr, len(arr)) | ||
8 | ||
|
||
References: | ||
https://en.wikipedia.org/wiki/Maximum_subarray_problem | ||
""" | ||
max_till_now = arr[0] | ||
max_ending = 0 | ||
|
||
for i in range(size): | ||
max_ending = max_ending + arr[i] | ||
if max_ending < 0: | ||
max_ending = 0 | ||
elif max_till_now < max_ending: | ||
max_till_now = max_ending | ||
|
||
return max_till_now | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,36 @@ | ||||||||
def sliding_window(elements: list[int], window_size: int) -> list[list[int]]: | ||||||||
""" | ||||||||
Generate sliding windows of a specified size over a list of elements. | ||||||||
|
||||||||
Args: | ||||||||
elements (list): The input list of elements. | ||||||||
window_size (int): The size of the sliding window. | ||||||||
|
||||||||
Returns: | ||||||||
list: A list of sliding windows. | ||||||||
|
||||||||
Example: | ||||||||
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8] | ||||||||
>>> result = sliding_window(lst, 3) | ||||||||
>>> print(result) | ||||||||
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8]] | ||||||||
|
||||||||
References: | ||||||||
https://stackoverflow.com/questions/8269916/what-is-sliding-window-algorithm-examples | ||||||||
""" | ||||||||
|
||||||||
if len(elements) <= window_size: | ||||||||
return [elements] | ||||||||
|
||||||||
windows = [] | ||||||||
for i in range(len(elements) - window_size + 1): | ||||||||
window = elements[i : i + window_size] | ||||||||
windows.append(window) | ||||||||
Comment on lines
+27
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
return windows | ||||||||
|
||||||||
|
||||||||
if __name__ == "__main__": | ||||||||
import doctest | ||||||||
|
||||||||
doctest.testmod() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,37 @@ | ||||||||||||||
from collections.abc import Generator | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
def sliding_window( | ||||||||||||||
elements: list[int], window_size: int | ||||||||||||||
) -> Generator[list[int], None, None]: | ||||||||||||||
""" | ||||||||||||||
Generate sliding windows of size window_size from the given elements. | ||||||||||||||
|
||||||||||||||
Args: | ||||||||||||||
elements (list): The input list of elements. | ||||||||||||||
window_size (int): The size of the sliding window. | ||||||||||||||
|
||||||||||||||
Returns: | ||||||||||||||
generator: A generator that yields sublists of size window_size. | ||||||||||||||
|
||||||||||||||
Example: | ||||||||||||||
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8] | ||||||||||||||
>>> sw_gen = sliding_window(lst, 3) | ||||||||||||||
>>> print(next(sw_gen)) | ||||||||||||||
[1, 2, 3] | ||||||||||||||
>>> print(next(sw_gen)) | ||||||||||||||
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
[2, 3, 4] | ||||||||||||||
|
||||||||||||||
References: | ||||||||||||||
https://stackoverflow.com/questions/8269916/what-is-sliding-window-algorithm-examples | ||||||||||||||
""" | ||||||||||||||
if len(elements) <= window_size: | ||||||||||||||
yield elements | ||||||||||||||
for i in range(len(elements) - window_size + 1): | ||||||||||||||
yield elements[i : i + window_size] | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
if __name__ == "__main__": | ||||||||||||||
import doctest | ||||||||||||||
|
||||||||||||||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a blank line to the tests pass.
As discussed in
CONTRIBUTING.md
please test your code by running on your local machine:%
python3 -m doctest -v dynamic_programming/max_sub_array_sum.py