Permutasi dan Kombinasi dengan Python
In [1]:
import itertools
In [2]:
data = ['A', 'B', 'C']
permutasi = itertools.permutations(data)
for perm in permutasi:
print(perm)
In [3]:
data = ['A', 'B', 'C', 'D']
In [4]:
permutasi = itertools.permutations(data, 2)
for perm in permutasi:
print(perm)
In [5]:
permutasi = itertools.permutations(range(4), 2)
len(tuple(permutasi))
Out[5]:
In [6]:
permutasi = itertools.permutations(range(10), 4)
len(tuple(permutasi))
Out[6]:
In [7]:
permutasi = itertools.permutations(range(43), 3)
len(tuple(permutasi))
Out[7]:
In [8]:
data = ['A', 'B', 'C', 'D', 'E']
In [9]:
kombinasi = itertools.combinations(data, 3)
for comb in kombinasi:
print(comb)
In [10]:
kombinasi = itertools.combinations(range(5), 3)
len(tuple(kombinasi))
Out[10]:
In [11]:
kombinasi = itertools.combinations(range(16), 4)
len(tuple(kombinasi))
Out[11]:
In [15]:
# kondisi 1 = (2 merah dari 4 merah) * (2 bukan merah dari 10 bukan merah)
k1 = len(tuple(itertools.combinations(range(4),2))) * len(tuple(itertools.combinations(range(10),2)))
# kondisi 2 = (3 merah dari 4 merah) * (1 bukan merah dari 10 bukan merah)
k2 = len(tuple(itertools.combinations(range(4),3))) * len(tuple(itertools.combinations(range(10),1)))
# kondisi 3 = keempatnya merah
k3 = len(tuple(itertools.combinations(range(4),4)))
jawab = k1 + k2 + k3
jawab
Out[15]:
In [16]:
#kondisi 1 = 1h (1 hijau) * 3sh (3 selain hijau):tanpa merah
#kondisi 2 = 2h * 2sh: tanpa merah
#kondisi 3 = 3h * 1sh: tanpa merah
#kondisi 4 = 4h
#jawab = k1 + k2 + k3 + k4
k1 = len(tuple(itertools.combinations(range(4),1))) * len(tuple(itertools.combinations(range(6),3)))
k2 = len(tuple(itertools.combinations(range(4),2))) * len(tuple(itertools.combinations(range(6),2)))
k3 = len(tuple(itertools.combinations(range(4),3))) * len(tuple(itertools.combinations(range(6),1)))
k4 = len(tuple(itertools.combinations(range(4),4)))
jawab = k1 + k2 + k3 + k4
jawab
Out[16]:
Tidak ada komentar:
Posting Komentar