-
-
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
Enhancement/issue 8427 #8811
Changes from 14 commits
37f5771
baf8d49
99013f8
e8c960a
0681a2c
bb8d992
7010d78
e5e1eb8
2bdff9b
106c48a
124a0e2
8339368
ba7576a
2323737
1e07a78
26eae99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||||
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. Add a blank line to the tests pass.
Suggested change
As discussed in |
||||||||
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() |
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. Empty file, should be deleted 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. Thanks, I have modified and filled it in |
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. This algorithm doesn't fit in 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. Thank you for the clarification, I have moved the files to other folder |
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)) | ||
[2, 3, 4] | ||
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. Could you add a test to check whether the generator stops at the correct index when it reaches the end of the list? |
||
|
||
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() |
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.
Unfortunately we already have an implementation of max subarray sum in
maths/kadanes.py
,other/maximum_subarray.py
,maths/largest_subarray_sum.py
,other/maximum_subsequence.py
, anddynamic_programming/max_sum_contiguous_subsequence.py
. Furthermore,dynamic_programming/max_sub_array.py
anddivide_and_conquer/max_subarray_sum.py
both implement this algorithm has a subroutine.... I have no idea why there are so many near-duplicate implementations of the same algorithm, but this really shouldn't be the case. I'll open an issue to address this.
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.
Opened #8812