def yield_all_permutations_of_sequence_in_orbit(sequence, permutation): '''Yields all permutations of `sequence` in orbit of `permutation`. :: >>> list(sequencetools.yield_all_permutations_of_sequence_in_orbit( ... (1, 2, 3, 4), [1, 2, 3, 0])) [(1, 2, 3, 4), (2, 3, 4, 1), (3, 4, 1, 2), (4, 1, 2, 3)] Returns permutations in lex order. Returns generator of `sequence` objects. ''' from abjad.tools import sequencetools if not sequencetools.Sequence(*permutation).is_permutation() or \ len(sequence) != len(permutation): args = (str(permutation), len(sequence)) message = '{!r} must be permutation of length {}.' message = message.format(permutation, len(sequence)) raise TypeError(message) # return identity first next_permutation = sequencetools.permute_sequence(sequence, range(len(sequence))) yield next_permutation # then return remaining permutations in orbit of permutation while True: next_permutation = sequencetools.permute_sequence(next_permutation, permutation) if next_permutation == sequence: break else: yield next_permutation
def yield_all_permutations_of_sequence_in_orbit(sequence, permutation): '''Yields all permutations of `sequence` in orbit of `permutation`. :: >>> list(sequencetools.yield_all_permutations_of_sequence_in_orbit( ... (1, 2, 3, 4), [1, 2, 3, 0])) [(1, 2, 3, 4), (2, 3, 4, 1), (3, 4, 1, 2), (4, 1, 2, 3)] Returns permutations in lex order. Returns generator of `sequence` objects. ''' from abjad.tools import sequencetools if not sequencetools.Sequence(*permutation).is_permutation() or \ len(sequence) != len(permutation): args = (str(permutation), len(sequence)) message = '{!r} must be permutation of length {}.' message = message.format(permutation, len(sequence)) raise TypeError(message) # return identity first next_permutation = sequencetools.permute_sequence(sequence, range(len(sequence))) yield next_permutation # then return remaining permutations in orbit of permutation while True: next_permutation = sequencetools.permute_sequence( next_permutation, permutation) if next_permutation == sequence: break else: yield next_permutation
def yield_all_permutations_of_sequence_in_orbit(sequence, permutation): '''Yield all permutations of `sequence` in orbit of `permutation` in lex order: :: >>> list(sequencetools.yield_all_permutations_of_sequence_in_orbit( ... (1, 2, 3, 4), [1, 2, 3, 0])) [(1, 2, 3, 4), (2, 3, 4, 1), (3, 4, 1, 2), (4, 1, 2, 3)] Returns generator of `sequence` objects. ''' from abjad.tools import sequencetools if not sequencetools.is_permutation(permutation, len(sequence)): args = (str(permutation), len(sequence)) raise TypeError('"%s" must be permutation of length %s.' % args) # return identity first # next_permutation = sequencetools.permute_sequence(sequence, range(len(sequence))) yield next_permutation # then return remaining permutations in orbit of permutation # while True: next_permutation = sequencetools.permute_sequence(next_permutation, permutation) if next_permutation == sequence: break else: yield next_permutation
def test_sequencetools_permute_sequence_03(): r'''Permute Abjad container. ''' container = Container("c'8 d'8 e'8") assert sequencetools.permute_sequence(container, [2, 0, 1]).lilypond_format == \ Container("e'8 c'8 d'8").lilypond_format
def yield_all_permutations_of_sequence(sequence): '''Yields all permutations of `sequence`. :: >>> list(sequencetools.yield_all_permutations_of_sequence((1, 2, 3))) [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] Returns permutations in lex order. Returns generator of `sequence` objects. ''' from abjad.tools import sequencetools for permutation in itertools.permutations(range(len(sequence))): yield sequencetools.permute_sequence(sequence, permutation)
def yield_all_permutations_of_sequence(sequence): '''Yields all permutations of `sequence`. :: >>> list(sequencetools.yield_all_permutations_of_sequence((1, 2, 3))) [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] Returns permutations in lex order. Returns generator of `sequence` objects. ''' from abjad.tools import sequencetools for permutation in itertools.permutations(tuple(range(len(sequence)))): yield sequencetools.permute_sequence(sequence, permutation)
def yield_all_permutations_of_sequence(sequence): '''Yields all permutations of `sequence`. :: >>> list(sequencetools.yield_all_permutations_of_sequence((1, 2, 3))) [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] Yields permutations in lex order. Returns generator. ''' from abjad.tools import sequencetools if not isinstance(sequence, collections.Sequence): message = 'must by sequence {!r}.' message = message.format(sequence) raise Exception(message) sequence_type = type(sequence) for permutation in itertools.permutations(tuple(range(len(sequence)))): yield sequencetools.permute_sequence(sequence, permutation)
def test_sequencetools_permute_sequence_01(): r'''Permute list. ''' assert sequencetools.permute_sequence([11, 12, 13, 14], [1, 0, 3, 2]) == [12, 11, 14, 13]
def test_sequencetools_permute_sequence_04(): r'''Permute string. ''' assert sequencetools.permute_sequence('heart', [4, 0, 1, 2, 3]) == 'thear'
def test_sequencetools_permute_sequence_02(): r'''Permute tuple. ''' assert sequencetools.permute_sequence((11, 12, 13, 14), [1, 0, 3, 2]) == (12, 11, 14, 13)