Определенно не самый аккуратный способ ведения дел, но с использованием ответа на этот вопрос:
https://stackoverflow.com/a/36343769/9742036
Как проверять наличие равных соседей в списке, это должно сделатьхитрость.
Сначала мы создаем список, представляющий вашу комбинацию шаров, просматриваем все их комбинации и затем отфильтровываем те, которые недопустимы.
from itertools import permutations
from operator import eq
input = (2, 1, 3)
# Produce a list to represent the objects to order. (2,1,3) -> [0, 0, 1, 2, 2, 2]
list_of_items = []
current_item = 1
for value in input:
for number in range(value):
list_of_items.append(current_item)
current_item += 1
count_of_valid_permutations = 0
set_of_valid_permutations = set()
# Generate all permutations of our list
for permutation in permutations(list_of_items):
# Permutations treat each ball as unique, even if the same colour, so check we're not over counting.
if permutation not in set_of_valid_permutations:
# Use the eq operator and map it to the permutation to see if any neighbours are equal.
hasEqualNeigbours = any(map(eq, permutation, permutation[1:]))
# If not, then this is a valid permutation.
if not hasEqualNeigbours:
# Record the permutation as seen.
set_of_valid_permutations.add(permutation)
count_of_valid_permutations += 1
print(count_of_valid_permutations)
print(set_of_valid_permutations)