2 回答

TA贡献1998条经验 获得超6个赞
def anagrams(s):
# if argument <s> is a blank String
if s == '':
# return a list is just <s> in it
return [s]
else:
# create a List
ans = []
# for each String character in calling this function recursively,
# but removing character 0!
for w in anagrams(s[1:]):
# for character position number starting at 0 and ending at
# the length of the returned value of the recursed function
# plus 1
for pos in range(len(w)+1):
# append a string with the following concatenation:
# - the returned value's string from position 0 to position <pos>
# - the character at position 0 of <s>
# - the returned value's string from position <pos> to the end
ans.append(w[:pos] + s[0] + w[pos:])
# return the list
return ans
添加回答
举报