44
55@author: rayde
66"""
7+
78'''
8- Two ways to manually code combinatorics in python without replacement.
9+ Get all the possible permutations for a set of objects.
10+ '''
11+
12+ from itertools import product
13+ import pandas as pd
14+
15+ def perm_possible (items , repeat = 5 ):
16+ perm = product (items , repeat )
17+ return [i for i in list (perm )]
18+
19+ perm = perm_possible ([0 ,1 ])
20+
21+
922
10- This isn't from leetcode, but an exercise in combinatorics from math for data science
11- specialization on Coursera.
1223'''
24+ Two ways to manually code combinatorics in python without replacement.
1325
26+ '''
1427
28+ #Method 1
1529n = 8
1630count = 0
1731
2337
2438print (count )
2539
40+ #Method 2
2641#C(n,r)=n!/(n−r)!r!
2742
2843def combinatorics (n , k ):
44+
45+ # Calculate n-factorial
2946 n_list = list (range (2 , n ))
3047 n_list .reverse ()
3148 n_factorial = n
3249
3350 for each in n_list :
3451 n_factorial = n_factorial * (each )
3552
36-
53+ # Calculate n minus k factorial
3754 n_r = n - k
3855
3956 n_r_list = list (range (2 , n_r ))
@@ -44,11 +61,13 @@ def combinatorics(n, k):
4461 for each in n_r_list :
4562 n_r_factorial = each * n_r_factorial
4663
64+ # Calculate k factorial
4765 k_list = list (range (2 , k ))
4866 k_list .reverse ()
4967 k_factorial = k
5068
5169 for each in k_list :
5270 k_factorial = k_factorial * (each )
5371
72+ # Plug into formula and return
5473 return n_factorial / (n_r_factorial * k_factorial )
0 commit comments