我想我是在尝试做一些非常基本和很简单的事情。出于这个原因,我确信Stack溢出已经有一个关于这个任务的帖子,但我想不是吗?也许这是一个无关紧要的概念?如果已经存在这样的帖子,请表示歉意。我找不到它。
下面是我想要完成的任务:给定列表长度n和最大元素值m,生成列表的所有排列,每个元素在0和m之间变化。
问题: 1.是否有递归的方法? 2.递归是否是最优的(在计算资源、O时间等方面)对于这个概念,还是迭代更好? 3.是否有更好的方法(不太复杂)来使用迭代(请参阅下面的代码)?
更多信息见下文
我已经编辑了我的代码和两个示例来生成和展示完整的解决方案
以下是两个例子:
例1: n= 3,m=2输出:
[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 1, 0]
[0, 1, 1]
[0, 1, 2]
[0, 2, 0]
[0, 2, 1]
[0, 2, 2]
[1, 0, 0]
[1, 0, 1]
[1, 0, 2]
[1, 1, 0]
[1, 1, 1]
[1, 1, 2]
[1, 2, 0]
[1, 2, 1]
[1, 2, 2]
[2, 0, 0]
[2, 0, 1]
[2, 0, 2]
[2, 1, 0]
[2, 1, 1]
[2, 1, 2]
[2, 2, 0]
[2, 2, 1]
[2, 2, 2]例1: n= 2,m=4输出:
[0, 0]
[0, 1]
[0, 2]
[0, 3]
[0, 4]
[1, 0]
[1, 1]
[1, 2]
[1, 3]
[1, 4]
[2, 0]
[2, 1]
[2, 2]
[2, 3]
[2, 4]
[3, 0]
[3, 1]
[3, 2]
[3, 3]
[3, 4]
[4, 0]
[4, 1]
[4, 2]
[4, 3]
[4, 4]我的直觉告诉我,这可以递归完成,但我想不出该如何做(我是一个初学者程序员)。目前,我有一个迭代实现这一目标的解决方案:
def permute(curr_permute,max_num,reset_flgs,reset_ind):
'''
Increment Logic Structure
'''
perm_ind = 0
max_val_flgs = [0]*len(curr_permute)
for c_i in range(len(curr_permute)):
if ((curr_permute[c_i] == max_num) and (c_i < (len(curr_permute)-1))):
if ((reset_ind == c_i) and (reset_flgs[c_i] == 1)):
reset_ind += 1
reset_flgs[c_i] = 0
max_val_flgs[c_i] = 1
continue
else:
perm_ind += 1
max_val_flgs[c_i] = 1
elif (c_i == (len(curr_permute)-1)):
if (curr_permute[c_i] == max_num):
perm_ind = c_i
max_val_flgs[c_i] = 1
else:
perm_ind = c_i
elif (curr_permute[c_i] < max_num):
perm_ind += 1
'''
Reverse the lists
'''
max_val_flgs.reverse()
curr_permute.reverse()
reset_flgs.reverse()
'''
Reset Logic Structure
'''
for n_i in range(len(curr_permute)):
if (max_val_flgs[n_i] == 0):
break
elif ((max_val_flgs[n_i] == 1) and (reset_flgs[n_i] == 1)):
curr_permute[n_i] = 0
perm_ind += -1
'''
Reverse the lists
'''
curr_permute.reverse()
reset_flgs.reverse()
'''
Apply the permutation increment
'''
curr_permute[perm_ind] += 1
return(curr_permute,reset_flgs,reset_ind)
def Permutation_Generation():
n = 2
m = 4
curr_permute = [0]*n
reset_flgs = [1]*n
reset_ind = 0
All_Permutations = [list(curr_permute)]
while (sum(curr_permute) < (n*m)):
print(curr_permute)
[curr_permute,reset_flgs,reset_ind] = permute(curr_permute,m,reset_flgs,reset_ind)
All_Permutations.append(list(curr_permute))
print(curr_permute)
return(All_Permutations)为垃圾代码道歉。一旦我想出了一种成功的方法,我就没有做太多的努力去清理它或者使它更有效率。我猜这段代码太复杂了,不适合我试图解决的概念。
发布于 2020-10-29 18:10:55
我不认为你的n和m的输出分别是3,2,真的很有意义。在第6行[0, 2, 0]之后,不应该后面跟着[0, 2, 1]而不是[1, 0, 0]吗?在第13排之后也发生了同样的事情。
总之,这里有一个递归的备选方案:
n = 3
m = 2
def permutation(n, m):
if n <= 0:
yield []
else:
for i in range(m+1):
for j in permutation(n-1, m):
yield [i] + j
# or even shorter
def permutation(n, m):
return [[i] + j for i in range(m + 1) for j in permutation(n - 1, m)] if n > 0 else []
for i in permutation(n, m):
print(i)输出:
[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], ..., [2, 1, 0], [2, 1, 1], [2, 1, 2], [2, 2, 0], [2, 2, 1], [2, 2, 2]]发布于 2020-10-29 16:04:20
3.你想得到所有的排列。给定n和m的排列数为(m+1)^n,因为您实际上想要全部打印,所以时间复杂度也是O((m+1)^n),这就是迭代时得到的结果。
1+2. --我不认为你应该用递归来做它,O((m+1)^n)是你能做的最好的时间,它是你使用迭代时得到的。递归也将占用更多的内存,因为它的内存堆栈。
发布于 2020-10-29 17:03:18
这个问题似乎得到了回答,但我想为迭代置换提供一个更简单的替代方案。在这里,我使用列表理解、格式化的字符串文本( f' string‘字符串)和eval内建方法。希望这个观点对你有帮助。
def get_permutations_list(array_size, max_value):
'''Returns a list of arrays that represent all permutations between zero
and max_value'''
array_member =''
for_loops=''
#Does a loop iteration for each member of the permutation list template
for i in range(array_size):
#adds a new member in each permutation
array_member += f'x{i}, '
#adds a new corresponding for loop
for_loops+=" for x{i} in range({value})".format(i=i,
value=max_value)
output = f'[[{array_member}] {for_loops}]' #combines it all together
return eval(output)
a = get_permutations_list(array_size=2, max_value=2)
print(a)
#Result: [[0,0],[0,1],[1,0],[1,1]]https://stackoverflow.com/questions/64594725
复制相似问题