Skip to content

adding a combinatorics folder with basic algorithms #11398

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 3 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
Empty file added combinatorics/__init__.py
Empty file.
185 changes: 185 additions & 0 deletions combinatorics/combinations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
from math import comb
from math import factorial


def validate_elements_count(

Check failure on line 5 in combinatorics/combinations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

combinatorics/combinations.py:1:1: I001 Import block is un-sorted or un-formatted
total_elements_count: int, selected_elements_count: int
) -> None:
"""Validate that the number of elements are positive and the total is greater than or equal to selected."""

Check failure on line 8 in combinatorics/combinations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

combinatorics/combinations.py:8:89: E501 Line too long (111 > 88)
if total_elements_count < selected_elements_count or selected_elements_count < 0:
raise ValueError(
"Please enter positive integers for total_elements_count and selected_elements_count "

Check failure on line 11 in combinatorics/combinations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

combinatorics/combinations.py:11:89: E501 Line too long (98 > 88)
"where total_elements_count >= selected_elements_count"
)


def combinations_iterative(
total_elements_count: int, selected_elements_count: int
) -> int:
"""
Returns the number of combinations that can be made from a total set of elements.

Examples:
>>> combinations_iterative(10, 5)
252
>>> combinations_iterative(6, 3)
20
>>> combinations_iterative(20, 5)
15504
>>> combinations_iterative(52, 5)
2598960
>>> combinations_iterative(0, 0)
1
>>> combinations_iterative(-4, -5)
Traceback (most recent call last):
...
ValueError: Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count

Check failure on line 36 in combinatorics/combinations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

combinatorics/combinations.py:36:89: E501 Line too long (153 > 88)
"""
validate_elements_count(total_elements_count, selected_elements_count)
combinations_count = 1
for i in range(selected_elements_count):
combinations_count *= total_elements_count - i
combinations_count //= i + 1
return combinations_count


def multiset_combinations(
total_elements_count: int, selected_elements_count: int
) -> int:
"""
Returns the number of combinations from a multiset of elements.

Examples:
>>> multiset_combinations(10, 5)
2002
>>> multiset_combinations(6, 3)
56
>>> multiset_combinations(20, 5)
42504
>>> multiset_combinations(52, 5)
3819816
>>> multiset_combinations(0, 0)
1
>>> multiset_combinations(-4, -5)
Traceback (most recent call last):
...
ValueError: n must be a non-negative integer
"""
validate_elements_count(total_elements_count, selected_elements_count)
return comb(
total_elements_count + selected_elements_count - 1, selected_elements_count
)


def combinations_formula(
total_elements_count: int, selected_elements_count: int
) -> int:
"""
Calculate combinations using the formula for n choose k.

Examples:
>>> combinations_formula(10, 5)
252
>>> combinations_formula(6, 3)
20
>>> combinations_formula(20, 5)
15504
>>> combinations_formula(52, 5)
2598960
>>> combinations_formula(0, 0)
1
>>> combinations_formula(-4, -5)
Traceback (most recent call last):
...
ValueError: Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count

Check failure on line 94 in combinatorics/combinations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

combinatorics/combinations.py:94:89: E501 Line too long (153 > 88)
"""
validate_elements_count(total_elements_count, selected_elements_count)
remaining_elements_count = total_elements_count - selected_elements_count
return int(
factorial(total_elements_count)
/ (factorial(selected_elements_count) * factorial(remaining_elements_count))
)


def combinations_with_repetitions(
total_elements_count: int, selected_elements_count: int
) -> int:
"""
Calculate combinations with repetitions allowed.

Examples:
>>> combinations_with_repetitions(10, 5)
2002
>>> combinations_with_repetitions(6, 3)
56
>>> combinations_with_repetitions(20, 5)
42504
>>> combinations_with_repetitions(52, 5)
3819816
>>> combinations_with_repetitions(0, 0)
1
>>> combinations_with_repetitions(-4, -5)
Traceback (most recent call last):
...
ValueError: Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count

Check failure on line 124 in combinatorics/combinations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

combinatorics/combinations.py:124:89: E501 Line too long (153 > 88)
"""
validate_elements_count(total_elements_count, selected_elements_count)
if total_elements_count + selected_elements_count == 0:
return 1
return int(
factorial(total_elements_count + selected_elements_count - 1)
/ (factorial(selected_elements_count) * factorial(total_elements_count - 1))
)


def permutations(total_elements_count: int, selected_elements_count: int) -> int:
"""
Calculate the number of permutations of selecting k elements from n elements.

Examples:
>>> permutations(10, 5)
30240
>>> permutations(6, 3)
120
>>> permutations(20, 5)
1860480
>>> permutations(52, 5)
311875200
>>> permutations(0, 0)
1
>>> permutations(-4, -5)
Traceback (most recent call last):
...
ValueError: Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count

Check failure on line 153 in combinatorics/combinations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

combinatorics/combinations.py:153:89: E501 Line too long (153 > 88)
"""
validate_elements_count(total_elements_count, selected_elements_count)
remaining_elements_count = total_elements_count - selected_elements_count
return int(factorial(total_elements_count) / factorial(remaining_elements_count))


def possible_selections(total_elements_count: int, selected_elements_count: int) -> int:
"""
Calculate the number of possible selections of k items from n available items, with replacement.

Check failure on line 162 in combinatorics/combinations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

combinatorics/combinations.py:162:89: E501 Line too long (100 > 88)

Examples:
>>> possible_selections(10, 5)
100000
>>> possible_selections(6, 3)
216
>>> possible_selections(20, 5)
3200000
>>> possible_selections(52, 5)
380204032
>>> possible_selections(0, 0)
1
>>> possible_selections(-4, -5)
Traceback (most recent call last):
...
ValueError: Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count

Check failure on line 178 in combinatorics/combinations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

combinatorics/combinations.py:178:89: E501 Line too long (153 > 88)
"""
validate_elements_count(total_elements_count, selected_elements_count)
return int(total_elements_count**selected_elements_count)


if __name__ == "__main__":
__import__("doctest").testmod()
34 changes: 34 additions & 0 deletions combinatorics/derangements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from math import factorial

"""
https://en.wikipedia.org/wiki/Derangement
"""


def derangement(elements_count: int) -> int:
"""
Returns the number of different combinations of selected_elements_count length which can

Check failure on line 10 in combinatorics/derangements.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

combinatorics/derangements.py:10:89: E501 Line too long (92 > 88)
be made from total_elements_count values, where total_elements_count >= selected_elements_count.

Examples:
>>> derangement(6)
265
>>> derangement(7)
1854
>>> derangement(8)
14833

>>> derangement(-5)
Traceback (most recent call last):
...
ValueError: Please enter positive integers for elements_count
"""
if elements_count < 0:
raise ValueError("Please enter positive integers for elements_count")

derangement = sum([((-1) ** i) / factorial(i) for i in range(elements_count + 1)])
return int(factorial(elements_count) * derangement)


if __name__ == "__main__":
__import__("doctest").testmod()
53 changes: 53 additions & 0 deletions combinatorics/stirling_second_kind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind
"""


def validate_elements_count(

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file combinatorics/stirling_second_kind.py, please provide doctest for the function validate_elements_count

total_elements_count: int, selected_elements_count: int
) -> None:
# If either of the conditions are true, the function is being asked
# to calculate a factorial of a negative number, which is not possible
if total_elements_count < selected_elements_count or selected_elements_count < 0:
raise ValueError(
"Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count"
)


def stirling_second(total_elements_count: int, selected_elements_count: int) -> None:
"""
Returns the number of different combinations of selected_elements_count length which can
be made from total_elements_count values, where total_elements_count >= selected_elements_count.

Examples:
>>> stirling_second(6, 3)
122
>>> stirling_second(10, 5)
50682
>>> stirling_second(8, 3)
1389
>>> stirling_second(-5, 0)
Traceback (most recent call last):
...
ValueError: Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count
"""

validate_elements_count(total_elements_count, selected_elements_count)
permutations_count = [
[0] * (selected_elements_count + 1) for _ in range(total_elements_count + 1)
]

for i in range(total_elements_count + 1):
permutations_count[i][0] = 1

for i in range(1, total_elements_count + 1):
for j in range(1, selected_elements_count + 1):
permutations_count[i][j] = (
j * permutations_count[i - 1][j] + permutations_count[i - 1][j - 1]
)

return permutations_count[total_elements_count][selected_elements_count]


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