#!/usr/bin/python3 """Generate the 120 consonant-cluster transpositions of ‘cafesuchito’: cafesuchito cafesuticho cafechusito cafechutiso cafetusicho cafetuchiso casefuchito casefuticho casechufito casechutifo casetuficho casetuchifo cachefusito cachefutiso cachesufito cachesutifo cachetufiso cachetusifo catefusicho catefuchiso ... Or any other word, though Spanish words work better. Duplicate consonant clusters will result in duplicate permutations. """ import textwrap, re, sys def explode(s): syllable = re.compile('(?i)((?!$)[^aeiouáéíóú]*)([aeiouáéíóú]+|$)') return zip(*(mo.groups() for mo in syllable.finditer(s))) c, v = explode('cafesuchito') assert c == ('c', 'f', 's', 'ch', 't'), c assert v == ('a', 'e', 'u', 'i', 'o'), v def perms(xs): "Enumerate permutations." if not xs: return [[]] return ([xs[i]] + perm for i in range(len(xs)) for perm in perms(xs[:i] + xs[i+1:])) # Unit test assert list(perms('123')) == [ ['1', '2', '3'], ['1', '3', '2'], ['2', '1', '3'], ['2', '3', '1'], ['3', '1', '2'], ['3', '2', '1']] # Intercalate string lists intercal = lambda c, v: ''.join(x for t in zip(c, v) for x in t) # Insancalate consonant permutations icp = lambda c, v: (intercal(cp, v) for cp in perms(c)) assert len(list(icp(c, v))) == 120 if __name__ == '__main__': print('\n'.join(textwrap.wrap(' '.join(icp(c, v) if len(sys.argv) == 1 else icp(*explode(sys.argv[1]))))))