""" jimmy_perm.py This idea works for the first few, but after that doesn't do enough rearranging on the 2..n characters. [''] ['a'] ['ab', 'ba'] ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] ['abcd', 'abcd', 'abdc', 'abdc', 'adbc', 'adbc', # OOPS - no 'ac...' 'bacd', 'bacd', 'bcad', 'bcad', 'bcda', 'bcda', 'cbda', 'cbda', 'cdab', 'cdab', 'cdba', 'cdba', 'dabc', 'dabc', 'dacb', 'dacb', 'dcab', 'dcab'] """ from math import factorial def perm(chars): """ Given a string of characters, return a list of all permutations >>> perm('abc') ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] """ result = [chars] n = factorial(len(chars)) if n < 2: return result while True: for i in range(2, len(chars)+1): new_chars = chars[1:i] + chars[0] + chars[i:] result.append(new_chars) if len(result) == n: return result chars = new_chars return result for i in range(5): print(sorted(perm('abcdefghijk'[:i])))