I need to get permutations, excluding mirrored sequences, i.e. (1, 0, 4) should be included, but (4, 0, 1) should be excluded after. I've came up with the following function but is wondering whether there is simpler solution.
The function skips inverses basing on the fact their last element is the same as the first element of the corresponding sequence, which is already processed given lexicographical order.
def permutations_no_inverse(iterable):
"""Return the half of permutations, treating mirrored sequences as the same,
e.g. (0, 1, 2) and (2, 1, 0) are the same.
Assume itertools.permutations returns tuples
"""
all_perm = it.permutations(iterable)
cur_start = None
starts_processed = set()
for perm in all_perm:
new_start = perm[0]
if new_start != cur_start:
if cur_start != None:
starts_processed.add(cur_start)
cur_start = new_start
if perm[-1] in starts_processed:
continue
else:
yield perm