Skip to content
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
36 changes: 36 additions & 0 deletions dynamic_programming/kadanes_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
Kadane's Algorithm
Find the maximum sum of a contiguous subarray.

Time Complexity: O(n)
Space Complexity: O(1)
"""

from typing import List

Check failure on line 9 in dynamic_programming/kadanes_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP035)

dynamic_programming/kadanes_algorithm.py:9:1: UP035 `typing.List` is deprecated, use `list` instead


def max_subarray_sum(arr: List[int]) -> int:

Check failure on line 12 in dynamic_programming/kadanes_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP006)

dynamic_programming/kadanes_algorithm.py:12:27: UP006 Use `list` instead of `List` for type annotation help: Replace with `list`
"""
Find the maximum sum of a contiguous subarray.

>>> max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4])
6
>>> max_subarray_sum([1])
1
>>> max_subarray_sum([5, 4, -1, 7, 8])
23
"""
if not arr:
raise ValueError("Array must not 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


if __name__ == "__main__":
import doctest

doctest.testmod()
21 changes: 21 additions & 0 deletions maths/abs_max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Find the absolute maximum element in a list."""


def abs_max(arr: list[int]) -> int:
"""
Find the element with maximum absolute value.

>>> abs_max([-10, 20, -30, 40])
-30
>>> abs_max([1, 2, 3])
3
"""
if not arr:
raise ValueError("List is empty")
return max(arr, key=abs)


if __name__ == "__main__":
import doctest

doctest.testmod()
21 changes: 21 additions & 0 deletions maths/average.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Compute average of a list of numbers."""


def average(numbers: list[float]) -> float:
"""
Compute the arithmetic mean.

>>> average([1, 2, 3, 4, 5])
3.0
>>> average([10, 20])
15.0
"""
if not numbers:
raise ValueError("List is empty")
return sum(numbers) / len(numbers)


if __name__ == "__main__":
import doctest

doctest.testmod()
20 changes: 20 additions & 0 deletions maths/gcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Greatest Common Divisor using Euclidean algorithm."""


def gcd(a: int, b: int) -> int:
"""
Find GCD of two numbers.

>>> gcd(12, 8)
4
>>> gcd(54, 24)
6
"""
while b:
a, b = b, a % b
return a


if __name__ == "__main__":
import doctest
doctest.testmod()
19 changes: 19 additions & 0 deletions maths/lcm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Least Common Multiple using GCD."""


def lcm(a: int, b: int) -> int:
"""
Find LCM of two numbers.

>>> lcm(4, 6)
12
>>> lcm(3, 7)
21
"""
from math import gcd
return abs(a * b) // gcd(a, b)


if __name__ == "__main__":
import doctest
doctest.testmod()
Loading