1
- from math import factorial , comb
1
+ from math import comb
2
+ from math import factorial
2
3
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. """
13
6
if total_elements_count < selected_elements_count or selected_elements_count < 0 :
14
7
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"
16
10
)
17
11
18
12
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 :
22
14
"""
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.
25
16
26
17
Examples:
27
- >>> combinations_iterative(10,5)
18
+ >>> combinations_iterative(10, 5)
28
19
252
29
-
30
- >>> combinations_iterative(6,3)
20
+ >>> combinations_iterative(6, 3)
31
21
20
32
-
33
- >>> combinations_iterative(20,5)
22
+ >>> combinations_iterative(20, 5)
34
23
15504
35
-
36
24
>>> combinations_iterative(52, 5)
37
25
2598960
38
-
39
26
>>> combinations_iterative(0, 0)
40
27
1
41
-
42
28
>>> combinations_iterative(-4, -5)
43
29
Traceback (most recent call last):
44
30
...
45
31
ValueError: Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count
46
32
"""
47
33
validate_elements_count (total_elements_count , selected_elements_count )
48
34
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 )
54
38
return combinations_count
55
39
56
40
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 :
60
42
"""
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.
63
44
64
45
Examples:
65
- >>> multiset_combinations(10,5)
46
+ >>> multiset_combinations(10, 5)
66
47
2002
67
-
68
- >>> multiset_combinations(6,3)
48
+ >>> multiset_combinations(6, 3)
69
49
56
70
-
71
- >>> multiset_combinations(20,5)
50
+ >>> multiset_combinations(20, 5)
72
51
42504
73
-
74
52
>>> multiset_combinations(52, 5)
75
53
3819816
76
-
77
54
>>> multiset_combinations(0, 0)
78
- Traceback (most recent call last):
79
- ...
80
- ValueError: n must be a non-negative integer
81
-
55
+ 1
82
56
>>> multiset_combinations(-4, -5)
83
57
Traceback (most recent call last):
84
58
...
85
59
ValueError: n must be a non-negative integer
86
60
"""
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 )
91
63
92
64
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 :
96
66
"""
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.
99
68
100
69
Examples:
101
- >>> combinations_formula(10,5)
70
+ >>> combinations_formula(10, 5)
102
71
252
103
-
104
- >>> combinations_formula(6,3)
72
+ >>> combinations_formula(6, 3)
105
73
20
106
-
107
- >>> combinations_formula(20,5)
74
+ >>> combinations_formula(20, 5)
108
75
15504
109
-
110
76
>>> combinations_formula(52, 5)
111
77
2598960
112
-
113
78
>>> combinations_formula(0, 0)
114
79
1
115
-
116
80
>>> combinations_formula(-4, -5)
117
81
Traceback (most recent call last):
118
82
...
@@ -121,34 +85,26 @@ def combinations_formula(
121
85
validate_elements_count (total_elements_count , selected_elements_count )
122
86
remaining_elements_count = total_elements_count - selected_elements_count
123
87
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 ))
126
90
)
127
91
128
92
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 :
132
94
"""
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.
135
96
136
97
Examples:
137
- >>> combinations_with_repetitions(10,5)
98
+ >>> combinations_with_repetitions(10, 5)
138
99
2002
139
-
140
- >>> combinations_with_repetitions(6,3)
100
+ >>> combinations_with_repetitions(6, 3)
141
101
56
142
-
143
- >>> combinations_with_repetitions(20,5)
102
+ >>> combinations_with_repetitions(20, 5)
144
103
42504
145
-
146
104
>>> combinations_with_repetitions(52, 5)
147
105
3819816
148
-
149
106
>>> combinations_with_repetitions(0, 0)
150
107
1
151
-
152
108
>>> combinations_with_repetitions(-4, -5)
153
109
Traceback (most recent call last):
154
110
...
@@ -158,31 +114,26 @@ def combinations_with_repetitions(
158
114
if total_elements_count + selected_elements_count == 0 :
159
115
return 1
160
116
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 ))
163
119
)
164
120
165
121
166
122
def permutations (total_elements_count : int , selected_elements_count : int ) -> int :
167
123
"""
168
- https://en.wikipedia.org/wiki/Permutation
124
+ Calculate the number of permutations of selecting k elements from n elements.
169
125
170
126
Examples:
171
- >>> permutations(10,5)
127
+ >>> permutations(10, 5)
172
128
30240
173
-
174
- >>> permutations(6,3)
129
+ >>> permutations(6, 3)
175
130
120
176
-
177
- >>> permutations(20,5)
131
+ >>> permutations(20, 5)
178
132
1860480
179
-
180
133
>>> permutations(52, 5)
181
134
311875200
182
-
183
135
>>> permutations(0, 0)
184
136
1
185
-
186
137
>>> permutations(-4, -5)
187
138
Traceback (most recent call last):
188
139
...
@@ -195,32 +146,27 @@ def permutations(total_elements_count: int, selected_elements_count: int) -> int
195
146
196
147
def possible_selections (total_elements_count : int , selected_elements_count : int ) -> int :
197
148
"""
198
- https://en.wikipedia.org/wiki/Permutation
149
+ Calculate the number of possible selections of k items from n available items, with replacement.
199
150
200
151
Examples:
201
- >>> possible_selections(10,5)
152
+ >>> possible_selections(10, 5)
202
153
100000
203
-
204
- >>> possible_selections(6,3)
154
+ >>> possible_selections(6, 3)
205
155
216
206
-
207
- >>> possible_selections(20,5)
156
+ >>> possible_selections(20, 5)
208
157
3200000
209
-
210
158
>>> possible_selections(52, 5)
211
159
380204032
212
-
213
160
>>> possible_selections(0, 0)
214
161
1
215
-
216
162
>>> possible_selections(-4, -5)
217
163
Traceback (most recent call last):
218
164
...
219
165
ValueError: Please enter positive integers for total_elements_count and selected_elements_count where total_elements_count >= selected_elements_count
220
166
"""
221
167
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 )
223
169
224
170
225
171
if __name__ == "__main__" :
226
- __import__ ("doctest" ).testmod ()
172
+ __import__ ("doctest" ).testmod ()
0 commit comments