From da34432c38af94381f43f403c967ca9fcdecc8e2 Mon Sep 17 00:00:00 2001 From: Medha Date: Mon, 23 Dec 2024 15:33:03 +0530 Subject: [PATCH] Add Kadane's Algorithm implementation --- .pre-commit-config.yaml | 1 + data_structures/arrays/kadanes_algorithm.py | 56 +++++++++++++++++++++ pyproject.toml | 45 +++++++++-------- 3 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 data_structures/arrays/kadanes_algorithm.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0c8108ac55be..4e7c9eceb020 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,6 +32,7 @@ repos: rev: "v2.5.0" hooks: - id: pyproject-fmt + language_version: python3.12 - repo: local hooks: diff --git a/data_structures/arrays/kadanes_algorithm.py b/data_structures/arrays/kadanes_algorithm.py new file mode 100644 index 000000000000..446b1d6f0d10 --- /dev/null +++ b/data_structures/arrays/kadanes_algorithm.py @@ -0,0 +1,56 @@ +class KadaneAlgorithm: + """ + Kadane's Algorithm to find the maximum sum + of a contiguous subarray in a given array. + + Time Complexity: O(n) + Space Complexity: O(1) + + The function works efficiently with both positive and negative integers. + + Usage: + >>> kadane = KadaneAlgorithm() + >>> kadane.max_subarray_sum([1, 2, 3, -2, 5]) + 9 + >>> kadane.max_subarray_sum([-1, -2, -3, -4]) + -1 + >>> kadane.max_subarray_sum([1, 2, 3, 4]) + 10 + >>> kadane.max_subarray_sum([10, -10, 20, -5, 10]) + 25 + """ + + def __init__(self): + pass + + def max_subarray_sum(self, arr: list[int]) -> int: + """ + This function finds the maximum sum of a + contiguous subarray using Kadane's Algorithm. + + :param arr: List of integers. + :return: Maximum sum of a contiguous subarray. + + Raises: + ValueError: If the input array is empty. + + >>> kadane = KadaneAlgorithm() + >>> kadane.max_subarray_sum([1, 2, 3, -2, 5]) + 9 + >>> kadane.max_subarray_sum([-1, -2, -3, -4]) + -1 + >>> kadane.max_subarray_sum([1, 2, 3, 4]) + 10 + >>> kadane.max_subarray_sum([10, -10, 20, -5, 10]) + 25 + """ + if not arr: + raise ValueError("Input array cannot be empty.") + + max_sum = current_sum = arr[0] + + for num in arr[1:]: + current_sum = max(num, current_sum + num) + max_sum = max(max_sum, current_sum) + + return max_sum diff --git a/pyproject.toml b/pyproject.toml index 7b7176705c44..6fdc82564f3b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,6 +49,30 @@ euler-validate = [ [tool.ruff] target-version = "py313" +[tool.codespell] +ignore-words-list = "3rt,ans,bitap,crate,damon,fo,followings,hist,iff,kwanza,manuel,mater,secant,som,sur,tim,toi,zar" +skip = "./.*,*.json,*.lock,ciphers/prehistoric_men.txt,project_euler/problem_022/p022_names.txt,pyproject.toml,strings/dictionary.txt,strings/words.txt" + +[tool.pytest.ini_options] +markers = [ + "mat_ops: mark a test as utilizing matrix operations.", +] +addopts = [ + "--durations=10", + "--doctest-modules", + "--showlocals", +] + +[tool.coverage.report] +omit = [ + ".env/*", + "project_euler/*", +] +sort = "Cover" + +[tool.mypy] +python_version = "3.12" + output-format = "full" lint.select = [ # https://beta.ruff.rs/docs/rules @@ -158,27 +182,6 @@ lint.pylint.max-branches = 20 # default: 12 lint.pylint.max-returns = 8 # default: 6 lint.pylint.max-statements = 88 # default: 50 -[tool.codespell] -ignore-words-list = "3rt,ans,bitap,crate,damon,fo,followings,hist,iff,kwanza,manuel,mater,secant,som,sur,tim,toi,zar" -skip = "./.*,*.json,*.lock,ciphers/prehistoric_men.txt,project_euler/problem_022/p022_names.txt,pyproject.toml,strings/dictionary.txt,strings/words.txt" - -[tool.pytest.ini_options] -markers = [ - "mat_ops: mark a test as utilizing matrix operations.", -] -addopts = [ - "--durations=10", - "--doctest-modules", - "--showlocals", -] - -[tool.coverage.report] -omit = [ - ".env/*", - "project_euler/*", -] -sort = "Cover" - [tool.sphinx-pyproject] copyright = "2014, TheAlgorithms" autoapi_dirs = [