-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcombinatorics_Permutations.py
150 lines (108 loc) · 3.19 KB
/
combinatorics_Permutations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 10 22:35:57 2020
@author: rayde
Combinatorics and permutations make me feel... [inadequate. ] like I need to study more. ;)
"""
'''
Get all the possible permutations for a set of objects.
'''
import math
from itertools import product
def perm_possible(items, repeat=5):
perm = product(items, repeat=repeat)
return [i for i in list(perm)]
perm = perm_possible([0,1,2,3,4,5,6,7,8,9])
n=8
count = 0
for i in range(n):
for j in range(n):
for k in range(n):
if i < j and j < k:
count += 1
print(count)
#C(n,r)=n!/(n−r)!r!
def combination_without_repetition(n, k):
n_factorial = math.factorial(n)
k_factorial = math.factorial(k)
n_minus_k_factorial = math.factorial(n-k)
denominator = k_factorial*n_minus_k_factorial
return n_factorial/denominator
def combination(n, k):
numerator = math.factorial(n+k-1)
denominator = math.factorial(k)*math.factorial(n-1)
return numerator/denominator
def permutation_without_repetition(n, k):
numerator = math.factorial(n)
denominator = math.factorial(n-k)
return numerator/denominator
def permutation(n, k):
return n**k
def combinatorics(n, k):
# Calculate n-factorial
n_list = list(range(2, n))
n_list.reverse()
n_factorial = n
for each in n_list:
n_factorial = n_factorial*(each)
# Calculate n minus k factorial
n_r = n-k
n_r_list = list(range(2, n_r))
n_r_list.reverse()
n_r_factorial = n_r
for each in n_r_list:
n_r_factorial = each*n_r_factorial
# Calculate k factorial
k_list = list(range(2, k))
k_list.reverse()
k_factorial = k
for each in k_list:
k_factorial = k_factorial*(each)
# Plug into formula and return
return n_factorial/(n_r_factorial*k_factorial)
import numpy as np
C = dict() # C[n,k] is equal to n choose k
output = []
for n in range(33):
C[n, 0] = 1
C[n, n] = 1
for k in range(1, n):
C[n, k] = C[n - 1, k - 1] + C[n - 1, k]
output.append(C[n, k])
print(np.sum(output))
print(C[5, 3] + C[5, 4] + C[5, 5])
for n in range(6):
[2 ** n for n in range(6)]
# Permutations(ordered, with repetitions)
# Just Tuples n**k
from itertools import product
for c in product("abc", repeat=2):
print("".join(c))
# Permutations(ordered, without repetitions)
from itertools import permutations
for c in permutations("abc", 2):
print("".join(c))
# Combinations (Unordered without repetitions)
from itertools import combinations
for c in combinations("abcd", 9):
print("".join(c))
'''
# Combinations (Unordered with repetitions)
from itertools import combinations_with_replacement
for c in combinations_with_replacement("abc", 2):
print("".join(c))
# Fixed Sum of Digits
import itertools as it
count = 0
for d in it.product(range(10), repeat=4):
if sum(d) == 10:
count += 1
print(count)
from itertools import combinations
output = []
for a in combinations(["A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "A7 ", "A8 "], 3):
for b in combinations(["B1 ", "B2 ", "B3 ", "B4 ", "B5 "], 2):
c = a + b
output.append(c)
print(len(output))
'''