# permutations of a string done recursively # ECS 10, May 26, 2009 # Matt Bishop # function to compute permutations of a string # parameter: s, a string # returns: list of all permutations of the string def perm(s): # base case: empty string if s == "": return [ s ] # recursive case # start empty listperm = [] # for each string in permutations of remaining str for w in perm(s[1:]): # put the first char in each of the possible places in the string for p in range(len(w)+1): # append new string to list listperm.append(w[:p] + s[0] + w[p:]) # return list return listperm