""" permutations.py Jim Mahoney | March 2019 | cs.marlboro.college | MIT License """ def permutations(chars): """ Return a list of the permutations. >>> permutations('abc') ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] """ if len(chars) == 1: return [chars] else: result = [] for i in range(len(chars)): char = chars[i] others = chars[:i] + chars[i+1:] subperm = perm(others) for s in subperm: result.append(char + s) return result if __name__ == '__main__': import doctest doctest.testmod()