Skip to content

Enhancement/issue 8427 #8804

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 8 commits into from
Closed
Show file tree
Hide file tree
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
42 changes: 42 additions & 0 deletions dynamic_programming/max_sub_array_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
def maxSubArraySum(arr, size):

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: maxSubArraySum

Please provide return type hint for the function: maxSubArraySum. 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

Please provide type hint for the parameter: size

"""
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]
>>> maxSubArraySum(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]
>>> maxSubArraySum(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()
35 changes: 35 additions & 0 deletions web_programming/sliding_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def sliding_window(elements, window_size):

Choose a reason for hiding this comment

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

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

Please provide type hint for the parameter: elements

Please provide type hint for the parameter: window_size

"""
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)

return windows

if __name__ == "__main__":
import doctest

doctest.testmod()
32 changes: 32 additions & 0 deletions web_programming/sliding_window_using_generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def sliding_window(elements, window_size):

Choose a reason for hiding this comment

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

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

Please provide type hint for the parameter: elements

Please provide type hint for the parameter: window_size

"""
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]

References:
https://stackoverflow.com/questions/8269916/what-is-sliding-window-algorithm-examples
"""
if len(elements) <= window_size:
return elements
for i in range(len(elements) - window_size + 1):
yield elements[i:i + window_size]


if __name__ == "__main__":
import doctest

doctest.testmod()