问题:写一个函数all-permutations的归纳定义,它将一个数字列表作为输入,和 返回该数字列表的所有排列的集合,作为输出,表示为列表列表。
(apply append(map(lambda (i) (map (lambda (j)(cons i j))
(permute (remove i
lst))))lst)))))
我想出了问题的核心代码。但我需要用纯英语和数学符号来表达解决方案,没有变量或数据结构变异。
答案 0 :(得分:-1)
# Python program to print all permutations with
# duplicates allowed
def toString(List):
return ''.join(List)
# Function to print permutations of string
# This function takes three parameters:
# 1. String
# 2. Starting index of the string
# 3. Ending index of the string.
def permute(a, l, r):
if l==r:
print toString(a)
else:
for i in xrange(l,r+1):
a[l], a[i] = a[i], a[l]
permute(a, l+1, r)
a[l], a[i] = a[i], a[l] # backtrack
# Driver program to test the above function
string = "ABC"
n = len(string)
a = list(string)
permute(a, 0, n-1)
# This code is contributed by Bhavya Jain
这是在geekforgeeks
中找到的问题的python中完成的代码来源:Mathword(http://mathworld.wolfram.com/Permutation.html)