Skip to content

Commit b2b9e08

Browse files
committed
adding init, fixing ruff errors
1 parent 6d8852a commit b2b9e08

File tree

2 files changed

+46
-100
lines changed

2 files changed

+46
-100
lines changed

combinatorics/__init__.py

Whitespace-only changes.

combinatorics/combinations.py

+46-100
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,82 @@
1-
from math import factorial, comb
1+
from math import comb
2+
from math import factorial
23

3-
"""
4-
https://en.wikipedia.org/wiki/Combination
5-
"""
6-
7-
8-
def validate_elements_count(
9-
total_elements_count: int, selected_elements_count: int
10-
) -> None:
11-
# If either of the conditions are true, the function is being asked
12-
# to calculate a factorial of a negative number, which is not possible
4+
def validate_elements_count(total_elements_count: int, selected_elements_count: int) -> None:
5+
""" Validate that the number of elements are positive and the total is greater than or equal to selected. """
136
if total_elements_count < selected_elements_count or selected_elements_count < 0:
147
raise ValueError(
15-
"Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count"
8+
"Please enter positive integers for total_elements_count and selected_elements_count "
9+
"where total_elements_count >= selected_elements_count"
1610
)
1711

1812

19-
def combinations_iterative(
20-
total_elements_count: int, selected_elements_count: int
21-
) -> int:
13+
def combinations_iterative(total_elements_count: int, selected_elements_count: int) -> int:
2214
"""
23-
Returns the number of different combinations of selected_elements_count length which can
24-
be made from total_elements_count values, where total_elements_count >= selected_elements_count.
15+
Returns the number of combinations that can be made from a total set of elements.
2516
2617
Examples:
27-
>>> combinations_iterative(10,5)
18+
>>> combinations_iterative(10, 5)
2819
252
29-
30-
>>> combinations_iterative(6,3)
20+
>>> combinations_iterative(6, 3)
3121
20
32-
33-
>>> combinations_iterative(20,5)
22+
>>> combinations_iterative(20, 5)
3423
15504
35-
3624
>>> combinations_iterative(52, 5)
3725
2598960
38-
3926
>>> combinations_iterative(0, 0)
4027
1
41-
4228
>>> combinations_iterative(-4, -5)
4329
Traceback (most recent call last):
4430
...
4531
ValueError: Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count
4632
"""
4733
validate_elements_count(total_elements_count, selected_elements_count)
4834
combinations_count = 1
49-
for processing_at_this_moment_elements_count in range(selected_elements_count):
50-
combinations_count *= (
51-
total_elements_count - processing_at_this_moment_elements_count
52-
)
53-
combinations_count //= processing_at_this_moment_elements_count + 1
35+
for i in range(selected_elements_count):
36+
combinations_count *= (total_elements_count - i)
37+
combinations_count //= (i + 1)
5438
return combinations_count
5539

5640

57-
def multiset_combinations(
58-
total_elements_count: int, selected_elements_count: int
59-
) -> int:
41+
def multiset_combinations(total_elements_count: int, selected_elements_count: int) -> int:
6042
"""
61-
Returns the number of different combinations of selected_elements_count length which can
62-
be made from total_elements_count values, where total_elements_count >= selected_elements_count.
43+
Returns the number of combinations from a multiset of elements.
6344
6445
Examples:
65-
>>> multiset_combinations(10,5)
46+
>>> multiset_combinations(10, 5)
6647
2002
67-
68-
>>> multiset_combinations(6,3)
48+
>>> multiset_combinations(6, 3)
6949
56
70-
71-
>>> multiset_combinations(20,5)
50+
>>> multiset_combinations(20, 5)
7251
42504
73-
7452
>>> multiset_combinations(52, 5)
7553
3819816
76-
7754
>>> multiset_combinations(0, 0)
78-
Traceback (most recent call last):
79-
...
80-
ValueError: n must be a non-negative integer
81-
55+
1
8256
>>> multiset_combinations(-4, -5)
8357
Traceback (most recent call last):
8458
...
8559
ValueError: n must be a non-negative integer
8660
"""
87-
88-
return comb(
89-
total_elements_count + selected_elements_count - 1, selected_elements_count
90-
)
61+
validate_elements_count(total_elements_count, selected_elements_count)
62+
return comb(total_elements_count + selected_elements_count - 1, selected_elements_count)
9163

9264

93-
def combinations_formula(
94-
total_elements_count: int, selected_elements_count: int
95-
) -> int:
65+
def combinations_formula(total_elements_count: int, selected_elements_count: int) -> int:
9666
"""
97-
Returns the number of different combinations of selected_elements_count length which can
98-
be made from total_elements_count values, where total_elements_count >= selected_elements_count.
67+
Calculate combinations using the formula for n choose k.
9968
10069
Examples:
101-
>>> combinations_formula(10,5)
70+
>>> combinations_formula(10, 5)
10271
252
103-
104-
>>> combinations_formula(6,3)
72+
>>> combinations_formula(6, 3)
10573
20
106-
107-
>>> combinations_formula(20,5)
74+
>>> combinations_formula(20, 5)
10875
15504
109-
11076
>>> combinations_formula(52, 5)
11177
2598960
112-
11378
>>> combinations_formula(0, 0)
11479
1
115-
11680
>>> combinations_formula(-4, -5)
11781
Traceback (most recent call last):
11882
...
@@ -121,34 +85,26 @@ def combinations_formula(
12185
validate_elements_count(total_elements_count, selected_elements_count)
12286
remaining_elements_count = total_elements_count - selected_elements_count
12387
return int(
124-
factorial(total_elements_count)
125-
/ (factorial(selected_elements_count) * factorial(remaining_elements_count))
88+
factorial(total_elements_count) /
89+
(factorial(selected_elements_count) * factorial(remaining_elements_count))
12690
)
12791

12892

129-
def combinations_with_repetitions(
130-
total_elements_count: int, selected_elements_count: int
131-
) -> int:
93+
def combinations_with_repetitions(total_elements_count: int, selected_elements_count: int) -> int:
13294
"""
133-
Returns the number of different combinations of selected_elements_count length which can
134-
be made from total_elements_count values, where total_elements_count >= selected_elements_count.
95+
Calculate combinations with repetitions allowed.
13596
13697
Examples:
137-
>>> combinations_with_repetitions(10,5)
98+
>>> combinations_with_repetitions(10, 5)
13899
2002
139-
140-
>>> combinations_with_repetitions(6,3)
100+
>>> combinations_with_repetitions(6, 3)
141101
56
142-
143-
>>> combinations_with_repetitions(20,5)
102+
>>> combinations_with_repetitions(20, 5)
144103
42504
145-
146104
>>> combinations_with_repetitions(52, 5)
147105
3819816
148-
149106
>>> combinations_with_repetitions(0, 0)
150107
1
151-
152108
>>> combinations_with_repetitions(-4, -5)
153109
Traceback (most recent call last):
154110
...
@@ -158,31 +114,26 @@ def combinations_with_repetitions(
158114
if total_elements_count + selected_elements_count == 0:
159115
return 1
160116
return int(
161-
factorial(total_elements_count + selected_elements_count - 1)
162-
/ (factorial(selected_elements_count) * factorial(total_elements_count - 1))
117+
factorial(total_elements_count + selected_elements_count - 1) /
118+
(factorial(selected_elements_count) * factorial(total_elements_count - 1))
163119
)
164120

165121

166122
def permutations(total_elements_count: int, selected_elements_count: int) -> int:
167123
"""
168-
https://en.wikipedia.org/wiki/Permutation
124+
Calculate the number of permutations of selecting k elements from n elements.
169125
170126
Examples:
171-
>>> permutations(10,5)
127+
>>> permutations(10, 5)
172128
30240
173-
174-
>>> permutations(6,3)
129+
>>> permutations(6, 3)
175130
120
176-
177-
>>> permutations(20,5)
131+
>>> permutations(20, 5)
178132
1860480
179-
180133
>>> permutations(52, 5)
181134
311875200
182-
183135
>>> permutations(0, 0)
184136
1
185-
186137
>>> permutations(-4, -5)
187138
Traceback (most recent call last):
188139
...
@@ -195,32 +146,27 @@ def permutations(total_elements_count: int, selected_elements_count: int) -> int
195146

196147
def possible_selections(total_elements_count: int, selected_elements_count: int) -> int:
197148
"""
198-
https://en.wikipedia.org/wiki/Permutation
149+
Calculate the number of possible selections of k items from n available items, with replacement.
199150
200151
Examples:
201-
>>> possible_selections(10,5)
152+
>>> possible_selections(10, 5)
202153
100000
203-
204-
>>> possible_selections(6,3)
154+
>>> possible_selections(6, 3)
205155
216
206-
207-
>>> possible_selections(20,5)
156+
>>> possible_selections(20, 5)
208157
3200000
209-
210158
>>> possible_selections(52, 5)
211159
380204032
212-
213160
>>> possible_selections(0, 0)
214161
1
215-
216162
>>> possible_selections(-4, -5)
217163
Traceback (most recent call last):
218164
...
219165
ValueError: Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count
220166
"""
221167
validate_elements_count(total_elements_count, selected_elements_count)
222-
return int(total_elements_count**selected_elements_count)
168+
return int(total_elements_count ** selected_elements_count)
223169

224170

225171
if __name__ == "__main__":
226-
__import__("doctest").testmod()
172+
__import__("doctest").testmod()

0 commit comments

Comments
 (0)