-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinations.py
More file actions
26 lines (19 loc) · 908 Bytes
/
combinations.py
File metadata and controls
26 lines (19 loc) · 908 Bytes
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
from itertools import product
def combo_from_itertools(product_in: list[int], length: int) -> list[list[int]]:
""" Генерация листа комбинаций с помощью стандартной библиотеки itertools + list comprehensions"""
return [list(combination) for combination in product(product_in, repeat=length)]
def combo_from_recurs(product_in: list[int], length: int) -> list[list[int]]:
""" Разворот itertools"""
base = len(product_in)
total = base ** length
combinations = []
for i in range(total):
combo = []
num = i
for _ in range(length):
combo.append(product_in[num % base])
num = num // base
combinations.append(combo[::-1])
return combinations
print(combo_from_itertools(product_in = [0, 1], length = 4))
print(combo_from_recurs(product_in = [0, 1], length = 4))