Il seguente programma inizia con centinaia di permutazioni e le riduce a un piccolo set di risultati. Inoltre, sto solo cercando il primo risultato valido. Quali tecniche posso utilizzare per renderlo più efficiente?
#!/usr/bin/python3
from itertools import permutations, zip_longest
def get_result():
checked = []
for permutation in permutations_of_items_in_bins(items='aabbb', number_of_bins=3, items_per_bin=2):
if permutation in checked:
continue
checked.append(permutation)
if is_valid_result(permutation):
return permutation
# this is for debug only
for x in sorted(checked):
print(_format(x))
return None
def is_valid_result(result):
return False # in reality, this may return True
# from: http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
def _get_chunks(n, iterable, padvalue=None):
"grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
return zip_longest(*[iter(iterable)]*n, fillvalue=padvalue)
def _format(iterable):
return ','.join([''.join(c) for c in iterable])
def permutations_of_items_in_bins(items: str, items_per_bin: int, number_of_bins: int, pad='z'):
# if not enough items to fit in all the bins, pad them with a value representing "empty"
items = items.rjust(number_of_bins * items_per_bin, pad)
# get all possible sort orders of the items
_permutations = permutations(items)
# that are unique
unique_permutations = set(_permutations)
# for each sort order, split them into bins in that order
for permutation in unique_permutations:
# split items into bins
chunks = list(_get_chunks(items_per_bin, permutation))
# item sort order in bins doesn't matter, so let's always sort the items in each bin
normalized = [tuple(sorted(x)) for x in chunks]
yield normalized
get_result()
"""
aa,bb,bz
aa,bz,bb
ab,ab,bz
ab,az,bb
ab,bb,az
ab,bz,ab
az,ab,bb
az,bb,ab
bb,aa,bz
bb,ab,az
bb,az,ab
bb,bz,aa
bz,aa,bb
bz,ab,ab
bz,bb,aa
"""