From dd916d46877833b124a25ba131fc766759e0ad02 Mon Sep 17 00:00:00 2001 From: Rejoan Sardar Date: Thu, 6 Mar 2025 18:29:58 +0530 Subject: [PATCH 1/7] feat: add pyds.maximum_subarray_sum_1 --- .../linear_data_structures/algorithms.rst | 4 +- .../linear_data_structures/__init__.py | 3 +- .../linear_data_structures/algorithms.py | 62 ++++++++++++++++++- .../tests/test_algorithms.py | 2 +- .../utils/tests/test_code_quality.py | 2 +- 5 files changed, 68 insertions(+), 5 deletions(-) diff --git a/docs/source/pydatastructs/linear_data_structures/algorithms.rst b/docs/source/pydatastructs/linear_data_structures/algorithms.rst index 7ab521cfd..ae1e2db74 100644 --- a/docs/source/pydatastructs/linear_data_structures/algorithms.rst +++ b/docs/source/pydatastructs/linear_data_structures/algorithms.rst @@ -45,4 +45,6 @@ Algorithms .. autofunction:: pydatastructs.jump_search -.. autofunction:: pydatastructs.intro_sort \ No newline at end of file +.. autofunction:: pydatastructs.intro_sort + +.. autofunction:: pydatastructs.maximum_subarray_sum_1 \ No newline at end of file diff --git a/pydatastructs/linear_data_structures/__init__.py b/pydatastructs/linear_data_structures/__init__.py index 057adc169..000001b5a 100644 --- a/pydatastructs/linear_data_structures/__init__.py +++ b/pydatastructs/linear_data_structures/__init__.py @@ -47,6 +47,7 @@ jump_search, selection_sort, insertion_sort, - intro_sort + intro_sort, + maximum_subarray_sum_1 ) __all__.extend(algorithms.__all__) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 792bce855..95742be07 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -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): @@ -1850,3 +1851,62 @@ 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 + """ + 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 diff --git a/pydatastructs/linear_data_structures/tests/test_algorithms.py b/pydatastructs/linear_data_structures/tests/test_algorithms.py index 46609544b..fb19c76b0 100644 --- a/pydatastructs/linear_data_structures/tests/test_algorithms.py +++ b/pydatastructs/linear_data_structures/tests/test_algorithms.py @@ -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 diff --git a/pydatastructs/utils/tests/test_code_quality.py b/pydatastructs/utils/tests/test_code_quality.py index eafa80be1..14dfe1949 100644 --- a/pydatastructs/utils/tests/test_code_quality.py +++ b/pydatastructs/utils/tests/test_code_quality.py @@ -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 From 84f6ff70c00c026102f460ac3e251110cbe9ad34 Mon Sep 17 00:00:00 2001 From: Rejoan Sardar Date: Thu, 6 Mar 2025 19:29:49 +0530 Subject: [PATCH 2/7] test error --- pydatastructs/linear_data_structures/algorithms.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 95742be07..066a2f9ff 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -1892,10 +1892,12 @@ def maximum_subarray_sum_1(array, **kwargs): >>> 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) From f8cfec9e81a758df0c3eb382c8c3a7092ca16a17 Mon Sep 17 00:00:00 2001 From: Rejoan Sardar Date: Thu, 6 Mar 2025 20:20:15 +0530 Subject: [PATCH 3/7] let's check --- scripts/build/add_dummy_submodules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build/add_dummy_submodules.py b/scripts/build/add_dummy_submodules.py index ccc5a0df6..22f7f07ef 100644 --- a/scripts/build/add_dummy_submodules.py +++ b/scripts/build/add_dummy_submodules.py @@ -6,4 +6,4 @@ def add_dummy_submodules(): for dummy_submodule in dummy_submodules: open('/'.join([project, module, backend, cpp, dummy_submodule]), 'w+').close() -add_dummy_submodules() +add_dummy_submodules(); From 019047b5b550943cb8dbc44dcf25dff059bb6fb7 Mon Sep 17 00:00:00 2001 From: Rejoan Sardar Date: Thu, 6 Mar 2025 20:30:32 +0530 Subject: [PATCH 4/7] feature add --- scripts/build/add_dummy_submodules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build/add_dummy_submodules.py b/scripts/build/add_dummy_submodules.py index 22f7f07ef..ccc5a0df6 100644 --- a/scripts/build/add_dummy_submodules.py +++ b/scripts/build/add_dummy_submodules.py @@ -6,4 +6,4 @@ def add_dummy_submodules(): for dummy_submodule in dummy_submodules: open('/'.join([project, module, backend, cpp, dummy_submodule]), 'w+').close() -add_dummy_submodules(); +add_dummy_submodules() From ecb1b75bcbe3cd6bc032c4e062865f267ab83701 Mon Sep 17 00:00:00 2001 From: Rejoan Sardar Date: Thu, 6 Mar 2025 20:41:47 +0530 Subject: [PATCH 5/7] feature add --- .github/workflows/ci.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77e81bac5..84a51518a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,7 @@ jobs: run: | pip install -r requirements.txt pip install -r docs/requirements.txt - + - name: Install lcov run: | sudo apt-get update @@ -42,7 +42,7 @@ 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 @@ -50,7 +50,7 @@ jobs: - 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 @@ -177,7 +177,6 @@ jobs: update-conda: true python-version: ${{ matrix.python-version }} conda-channels: anaconda, conda-forge - - run: conda --version - run: which python - name: Upgrade pip version From 2573b0bf59a64d29f5c9d280aef9f88b6e92b179 Mon Sep 17 00:00:00 2001 From: RJ786 <119718513+Rejoan-Sardar@users.noreply.github.com> Date: Sat, 8 Mar 2025 22:22:57 +0530 Subject: [PATCH 6/7] Update ci.yml --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 84a51518a..b52e05df1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -177,6 +177,7 @@ jobs: update-conda: true python-version: ${{ matrix.python-version }} conda-channels: anaconda, conda-forge + - run: conda --version - run: which python - name: Upgrade pip version From 1889a478e465167abe76ed1d6861130e4d123e89 Mon Sep 17 00:00:00 2001 From: RJ786 <119718513+Rejoan-Sardar@users.noreply.github.com> Date: Thu, 27 Mar 2025 10:54:10 +0530 Subject: [PATCH 7/7] Update ci.yml --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ad7aac75c..f86048399 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,7 @@ jobs: test-ubuntu-py38: runs-on: ${{matrix.os}} timeout-minutes: 20 + gfdf strategy: fail-fast: false matrix: