Skip to content

feat: add pyds.maximum_subarray_sum_1 #597

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
test-ubuntu-py38:
runs-on: ${{matrix.os}}
timeout-minutes: 20
gfdf
strategy:
fail-fast: false
matrix:
Expand All @@ -33,7 +34,7 @@ jobs:
run: |
pip install -r requirements.txt
pip install -r docs/requirements.txt

- name: Install lcov
run: |
sudo apt-get update
Expand All @@ -42,15 +43,15 @@ jobs:
- name: Build package
run: |
CXXFLAGS=--coverage CFLAGS=--coverage python scripts/build/install.py
# coverage tests
# coverage tests
- name: Run tests
run: |
python -m pytest --doctest-modules --cov=./ --cov-report=xml -s

- name: Capture Coverage Data with lcov
run: |
lcov --capture --directory . --output-file coverage.info --no-external

- name: Generate HTML Coverage Report with genhtml
run: |
genhtml coverage.info --output-directory coverage_report
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ Algorithms

.. autofunction:: pydatastructs.jump_search

.. autofunction:: pydatastructs.intro_sort
.. autofunction:: pydatastructs.intro_sort

.. autofunction:: pydatastructs.maximum_subarray_sum_1
3 changes: 2 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
jump_search,
selection_sort,
insertion_sort,
intro_sort
intro_sort,
maximum_subarray_sum_1
)
__all__.extend(algorithms.__all__)
64 changes: 63 additions & 1 deletion pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
'jump_search',
'selection_sort',
'insertion_sort',
'intro_sort'
'intro_sort',
'maximum_subarray_sum_1'
]

def _merge(array, sl, el, sr, er, end, comp):
Expand Down Expand Up @@ -1850,3 +1851,64 @@ def partition(array, lower, upper):
intro_sort(array, start=p+1, end=upper, maxdepth=maxdepth-1, ins_threshold=ins_threshold)

return array

def maximum_subarray_sum_1(array, **kwargs):
"""
Finds the maximum subarray sum of the given array using a brute force approach.
Parameters
==========
array: OneDimensionalArray
The array for which the maximum subarray sum
has to be found.
start: int
The starting index of the portion
which is to be considered.
Optional, by default 0
end: int
The ending index of the portion which
is to be considered.
Optional, by default the index
of the last position filled.
comp: lambda/function
The comparator which is to be used
for performing comparisons.
Optional, by default, less than or
equal to is used for comparing two
values.
backend: pydatastructs.Backend
The backend to be used.
Optional, by default, the best available
backend is used.
Returns
=======
output: int
The maximum subarray sum.
Examples
========
>>> from pydatastructs import OneDimensionalArray as ODA, maximum_subarray_sum_1
>>> arr = ODA(int, [-2, 1, -3, 4, -1, 2, 1, -5, 4])
>>> maximum_subarray_sum_1(arr)
6
>>> arr = ODA(int, [1, 2, 3, 4, 5])
>>> maximum_subarray_sum_1(arr)
15

References
==========
.. [1] https://en.wikipedia.org/wiki/Maximum_subarray_problem
python -m doctest -v pydatastructs/linear_data_structures/algorithms.py
"""
raise_if_backend_is_not_python(
maximum_subarray_sum_1, kwargs.get('backend', Backend.PYTHON))
start = kwargs.get('start', 0)
end = kwargs.get('end', len(array) - 1)
comp = kwargs.get('comp', lambda u, v: u <= v)

max_sum = float('-inf')
for i in range(start, end + 1):
curr_sum = 0
for j in range(i, end + 1):
curr_sum += array[j]
max_sum = max(max_sum, curr_sum)

return max_sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_ordered,
upper_bound, lower_bound, longest_increasing_subsequence, next_permutation,
prev_permutation, bubble_sort, linear_search, binary_search, jump_search,
selection_sort, insertion_sort, intro_sort, Backend)
selection_sort, insertion_sort, intro_sort, maximum_subarray_sum_1, Backend)

from pydatastructs.utils.raises_util import raises
import random
Expand Down
2 changes: 1 addition & 1 deletion pydatastructs/utils/tests/test_code_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _apis():
pyds.Trie, pyds.TrieNode, pyds.SkipList, pyds.RangeQueryStatic, pyds.RangeQueryDynamic, pyds.SparseTable,
pyds.miscellaneous_data_structures.segment_tree.OneDimensionalArraySegmentTree,
pyds.bubble_sort, pyds.linear_search, pyds.binary_search, pyds.jump_search,
pyds.selection_sort, pyds.insertion_sort, pyds.quick_sort, pyds.intro_sort]
pyds.selection_sort, pyds.insertion_sort, pyds.quick_sort, pyds.intro_sort, pyds.maximum_subarray_sum_1]

def test_public_api():
pyds = pydatastructs
Expand Down