-
Notifications
You must be signed in to change notification settings - Fork 5
/
combinatorics.py
41 lines (36 loc) · 1019 Bytes
/
combinatorics.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
# module associated with mastermind game
# import the modules we need, for creating a GUI...
import random
def fac(n):
if n == 0:
return 1
else:
return (fac(n-1) * n)
def permutations(items):
n = len(items)
if n==0: yield []
else:
for i in range(len(items)):
for cc in permutations(items[:i]+items[i+1:]):
yield [items[i]]+cc
def k_permutations(items, n):
if n==0: yield []
else:
for i in range(len(items)):
for ss in k_permutations(items, n-1):
if (not items[i] in ss):
yield [items[i]]+ss
def random_permutation(list):
length = len(list);
max = fac(length);
index = random.randrange(0, max)
i = 0
for p in permutations(list):
if i == index:
return p
i += 1
def all_colours(colours, positions):
colours = random_permutation(colours)
for s in k_permutations(colours, positions):
yield(s)
return