# permutations of a string done recursively # Matt Bishop, Mar. 8, 2012 # for ECS 10 Winter 2012 # 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