-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutations.py
44 lines (34 loc) · 998 Bytes
/
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
from itertools import combinations
import copy
def __add_permutation(a, collection):
permutation = copy.deepcopy(a)
collection.append(permutation)
return collection
def __heap_permutation(a, size, n, collection):
if (size == 1):
collection = __add_permutation(a, collection)
return
for i in range(size):
__heap_permutation(a, size-1, n, collection)
if size & 1:
a[0], a[size-1] = a[size-1], a[0]
else:
a[i], a[size-1] = a[size-1], a[i]
def generate_all_permutations(a):
"""
Generate all possible combinations of a list
of a numbers, using all of them, with no
repetitions.
"""
n = len(a)
collection = []
__heap_permutation(a, n, n, collection)
return collection
def generate_all_combinations(a, n):
"""
Generate all possible combinations of length n
from a list of a numbers
"""
if n > len(a):
return []
return list(combinations(a, n))